Programming/Database

[MySQL] foreign key(외래키) 추가, 삭제, 확인 방법

Allg 2017. 9. 8. 09:42

[MySQL] foreign key(외래키) 추가, 삭제, 확인 방법

1.foreign key 추가

alter table [추가할테이블명] add constraint [제약조건명] foreign key(컬럼명)
references [부모테이블명] (PK컬럼명) [ON DELETE CASCADE / ON UPDATE CASECADE];
  • ON DELETE CASCADE
    외래 키에서 참조하는 키가 포함된 행을 삭제하려고 하면 해당 외래 키가 포함되어 있는 모든 행도 삭제

  • ON UPDATE CASCADE
    외래 키에서 참조하는 키 값이 포함된 행에서 키 값을 업데이트 하면 해당 외래 키를 구성하는 모든 값도 키에 지정된 새 값으로 업데이트되도록 지정

2. foreign key 삭제

alter table [테이블명] drop foreign key [제약조건명];

3. foreign key 확인 방법

3.1 테이블 기준 확인
select * from information_schema.table_constraints where table_name = '테이블명';
3.2 데이터베이스 기준 확인
select * from information_schema.table_constraints where constraint_schema = '데이터베이스명';