
create table course
(
Cno char(2) not null,
Cname varchar(20),
Cpno char(2),
Credit smallint,
primary key (Cno),
foreign key (Cpno) references Student(Sno)
)ENGINE = InnoDB;
create table sc
(
Sno char(7) not null,
Cno char(2) not null,
grade smallint null check (grade is null or (grade between 0and 100)),
primary key (Sno, Cno),
foreign key (Sno) referencesStudent(Sno),
foreign key (Cno) referencescourse(Cno)
)ENGINE = InnoDB;
表建立完后,我对course表进行了插入语句:insert into course values ('1', '数据库','2', '12');
结果报错为:
ERROR 1452 (23000): Cannot add orupdate a child row: a foreign key constraint fails(`student`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY(`Cpno`) REFERENCES `course` (`Cno`))
意思就是不能进行添加和更新在一个孩子行。