数据库已存在表classroom(building,room_no,capacity),
情况一:
在创建表section;
- create table section
- (course_id varchar(20),
- sec_id varchar(20),
- semester varchar(20),
- year varchar(20),
- building varchar(20),
- room_no varchar(20),
- time_slot_id varchar(20),
- primary key (course_id,sec_id,semester,year),
- foreign key (building,room_no) references classroom
- );
就出现ERROR 1215 (HY000): Cannot add foreign key constraint 的错误;
原因很简单,设置外键的语法应该是这样的 foreign key (a,b,...) references table_name(a,b,...)
只需将以上语句( foreign key (building,room_no) references classroom)改成 foreign key (building,room_no) references classroom(building,room_no)这样既可!!
情况二:同样是错误:ERROR 1215 (HY000): Cannot add foreign key constraint问题;
已存在表classroom(building,room_no,capacity)的主键是由building,room_no组成,
如果另外一张表A外联表classroom,外键的设置不能单独的building或room_no,只能设置为classroom和room_no