首页 > 代码库 > can't add foreign key in mysql?
can't add foreign key in mysql?
1 create table department 2 (dept_name varchar(20), 3 building varchar(15), 4 budget numeric(12,2) check (budget > 0), 5 primary key (dept_name) 6 ); 7 8 create table course 9 (course_id varchar(8), 10 title varchar(50), 11 dept_name varchar(20),12 credits numeric(2,0) check (credits > 0),13 primary key (course_id),14 foreign key (dept_name) references department15 on delete set null16 );
显示出错can‘t add foreign key 。
版本:MySQL5.6
但在MySQL5.1上能运行
在网上各种找原因,都搞不定。最后是这样搞定的,太坑了:
1 create table department 2 (dept_name varchar(20), 3 building varchar(15), 4 budget numeric(12,2) check (budget > 0), 5 primary key (dept_name) 6 ); 7 8 create table course 9 (course_id varchar(8), 10 title varchar(50), 11 dept_name varchar(20),12 credits numeric(2,0) check (credits > 0),13 primary key (course_id),14 foreign key (dept_name) references department(dept_name)15 on delete set null16 );
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。