foreign key 외래키
- 테이블과 테이블을 연결할 때 쓴다.
- 만약 학생 정보 table과 시험 성적 table이 있다면, 기본적으로 회원 정보 table에 학생 정보가 있고 학번이 있을 것이다. 그런데 시험 성적 table에 data를 입력하려고 보니 없는 학번이 있으면 안 될 것이다. 이를 연결해주고, 시험 성적에서 학번이 어떤 학생인지를 알려주려 연결하는 것이 외래키이다.
이때 학생 정보 table이 참조 table이 되고 외래키로 지정되는 것이 시험 성적 table이다.
사용
참조 table의 column은 primary key여야 한다.
foreign key는 참조값이나 null만 허용한다. 그 외에 없는 값은 안 된다.
alter table 외래키table명 add [constraint constraint명] foreign key(외래column명) references 참조table명(참조column명);
ex) alter table exam add foreign key(시험학번) references member(학번) ;
error 1005 : Foreign key constraint is incorrectly formed 오류 해결법
'DB > MariaDB' 카테고리의 다른 글
[MariaDB] MariaDB ERROR 2002 (HY000): Can't connect to local server through socket '/tmp/mysql.sock' (2) (0) | 2022.12.20 |
---|---|
[MariaDB] 14.primary key (0) | 2022.12.20 |
[MariaDB] 13.auto_increment (0) | 2022.12.20 |
[MariaDB] 12.union (0) | 2022.12.20 |
[MariaDB] 11. join (0) | 2022.12.20 |
댓글