日期:2014-05-16  浏览次数:20722 次

mysql error 1005
create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);

create table department
(dept_name varchar(20), 
building varchar(15), 
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);

create table course
(course_id varchar(8), 
title varchar(50), 
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department
on delete set null
);


sql如上,前两个表能够创建成功,但是创建course表的时候提示error1005,网上查过可能有三点原因


1.外键和被引用外键类型不一样,比如integer和double 
2.找不到要被引用的列 
3.表的字符编码不一样  

对照一下,外键类型一样,都是varchar(20),被引用的列也存在啊,第三点不知道怎么回事
我也搞不清问题出在哪了,求解答!!

------解决方案--------------------
show create table department;

贴出来看一下。

create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department(dept_name )
on delete set null
);
------解决方案--------------------
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department(dept_name)
on delete set null
);