2014-01-10 4 views
0

이것은 내가 사용중인 일부 테이블의 예입니다.데이터를 삽입 할 때 외래 키가 다른 테이블의 기본 키로 존재하도록하려면 어떻게해야합니까?

create TABLE People(
peopleID int not null, 
name varchar(40) not null, 
primary key (peopleID) 
); 


create table car(
carID int not null, 
peopleID int not null, 
primary key (carID), 
foreign key (peopleID) references People(peopleID) 
); 

어떻게 내가 '차'에 삽입 할 때 'peopleID'외래 키가 테이블 '사람'의 기본 키로 존재하는지 확인 할.

예를 들어, 나는 오류가 발생하는 다음과 같은 성명을 원하는 것 :

INSERT INTO car VALUES (123, 343); 

... 비어로 343, 'PeopleID'에 존재하지 않기 때문입니다.

고마워요.

답변

1

의견이 약간 깁니다.

당신은 foreign key 제약 을 무엇을 설명했다 :

For storage engines supporting foreign keys, MySQL rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.

제약이 here을 설명한다.

+0

고맙습니다. 문제를 해결했습니다. – user2891805

관련 문제