#在mysql中添加外键约束时出现ERROR 1215 (HY000): Cannot add foreign key constraint,原因时 在被参照表中没有设置主键(primary key),使得参照表不能添加外键。给teacher 表中tid设置为primary key就可以添加外键约束了
create table course
(
cid int(3),
cname varchar(20),
tid int(3),
constraint c_t_tid foreign key (tid) references teacher(tid)
);
create table teacher
(
tid int(3) primary key,
tname varchar(20),
tcid int(3) ,
constraint c_tc_tcid foreign key (tcid) references teacherCard(tcid)
);