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?,布布扣,bubuko.com

can't add foreign key in mysql?

上一篇:Sharepoint2013商务智能学习笔记之部署AdventureWorksDW2012数据库(三)


下一篇:是数据库设置主键自增的时候添加也可以自定义主键值