본문 바로가기

DB

mysql 외래키 설정

1. foreign key 추가

alter table '추가할 테이블 명' add constraint '제약조건명' foreign key '컬럼명' references '부모 테이블 명' ('참조하는 PK 컬럼명') [ON DELETE CASCADE/ ON UPDATE CASCADE];

2. foreing key 삭제

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

3. 테이블에서의 외래키 확인방법

select*from information_schema.table_constraints where table_name='테이블명';
 

4. 데이터베이스(스키마)에서의 외래키 확인방법

select * from information_schema.table_constraints where constraint_schema = '데이터베이스명';