DB/명령프롬프트 - mysql

[MySQL] foreign key가 설정되어 있는 값의 설정변경

congs 2023. 3. 29. 16:20

 

1. 외래키 테이블 락 걸기

lock table 참조테이블명 write, 내테이블명 write; //foreign key가 걸려있는 내테이블과 참조테이블에 lock

lock table course write, attend write;

 

2. 외래키 지우기

alter table 내테이블명 drop foreign key 내테이블명_ibfk_2; //내테이블에서 참조데이블을 끊기!

alter table attend drop foreign key attend_ibfk_2;

 

3. 속성 넣기

alter table course modify co_num int not null auto_increment; //참조테이블의 속성 설정 다시하기

alter table 참조테이블명 modify 칼럼명 int not null auto_increment;

 

3. 외래키 설정

alter table 내테이블명 add foreign key(내칼럼명) referenses 참조테이블명(참조칼럼명); //외래키 재설정

alter table attend add foreign key(at_co_num) referenses course(co_num);

 

4. 락 해제

unlock tables;