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 department
15         on delete set null
16     );

显示出错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 null
16     );

can't add foreign key in mysql?,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。