카테고리 없음

[DB] Database, db table 생성

꿀딴지- 2022. 10. 23. 20:47

지난번 포스팅에서는 mysql 을 설치했으니, 실제로 사용할 데이터베이스도 생성해 보았습니다.

 

1. 데이터베이스 생성

mysql> CREATE DATABASE driving_log;

사용할 데이터 베이스를 선택합니다.

mysql> use [DB먕];

2. 테이블 생성하기

mysql> create table drive_hist(
	hist_seq int not null auto_increment primary key,
    	car_id int not null,
    	start_time DATETIME,
    	end_time DATETIME,
    	distance varchar(20),
    	user_id int,
    	foreign key (car_id) references car_info(car_id) on delete set default,
    	foreign key (user_id) references user_info(user_id) on delete set default
);

4. foreign key 제약조건으로 테이블 수정이안될 때는

잠시 foreign key 체크 조건을 껐다가 키면 된다.

foreign key로 참조되고 있어 테이블정보를 변경하지 못하는 상황

mysql> SET FOREIGN_KEY_CHECKS=0;
mysql> alter table car_info modify car_id int not null auto_increment;
mysql> SET FOREIGN_KEY_CHECKS=1;

4. 테이블 삭제하기

mysql> drop table [테이블명]

5. 데이터베이스 삭제하기

mysql> drop database [DB명];