MySql Select, Insert, Update, Delete 하기

YU NA Joe·2022년 7월 30일

현재 테이블들

데이터 insert하기 전에 삽질한것들..ㅎ

## 오류1

12:05:41	Insert Into person (Person_Name, Birthday, Address, Rented_Book_Id) VALUES ("yuna", 19890620, "경기도 부천시", "ISBN12090")	Error Code: 1366. Incorrect integer value: 'ISBN12090' for column 'Rented_Book_Id' at row 1	0.000 sec

##  Rendted_Book_Id 컬럼의 데이터 타입을 바꾸자


ALTER TABLE [테이블 이름] MODIFY [컬럼명] [바꾸고자 하는 데이터타입];

ALTER TABLE person modify Rented_Book_Id varchar(45);


======================================================


## 오류2

12:19:02	Insert INTO person (Person_Name, Gender, Birthday, Address, Rented_Book_id) VALUES ("yuna", "Female", 19890620, "경기도 부천시", "ISBN123123")	Error Code: 1364. Field 'Person_Id' doesn't have a default value	0.000 sec
>> primary key에 auto_increment가 있는줄 알았는데 아니었댜.. 
>> auto_increment를 넣어주쟈 
>> 우선 PrimaryKey를 drop해줘야 한다. 
ALTER TABLE person DROP Person_Id;
ALTER TABLE person add Person_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY;

======================================================
## 컬럼 Person_ID를 처음으로 이동시켜주자 

alter table person modify column Person_ID INT FIRST;

======================================================
## 그런데도 안됨 .. 아래처럼 했더니 됨ㅎ 
alter table person modify Person_ID INT auto_increment; 

person table에 데이터가 없다

데이터 INSERT (하나의데이터)

person table
Insert INTO person (Person_Name, Gender, Birthday, Address, Rented_Book_id)
VALUES ("yuna", "Female", 19890620, "경기도 부천시", "ISBN123123");

데이터 INSERT (여러개의데이터)

books table 
insert into books (Book_Genre,Book_Title, Person_Rented_Id, Library_Name)
values ("romance", "last love",1,"서울시립도서관"), 
("novel", "our story",1,"서울시립도서관"), 

데이터 UPDATE

update person
set Address = "서울시 합정동"
where Person_Id = 2; 
select * from person;

데이터 DELETE

use bookrent;
select * from books;
Delete from books
where Book_Id=3;

select * from books where Book_Genre = "novel";

0개의 댓글