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 );