首页 文章

ERROR 1005(HY000):'t create table ' db.grades'(错误号:150)

提问于
浏览
1

我在mysql中创建了四个表 . 但它在创建第4个时显示错误 .

ERROR 1005(HY000):无法创建表'db.grades'(错误号:150)

我检查了名称和类型是否适合外键 . 有什么建议?谢谢 .

CREATE TABLE students
(

id int unsigned not null,
first_name VARCHAR(50),
last_name VARCHAR(50),
email_address VARCHAR(50),
primary key (id)

);



-------
CREATE TABLE departments 
(
school_code enum('L', 'B', 'A', 'F', 'E', 'T', 'I', 'W', 'S', 'U', 'M' ),
dept_id int unsigned,
abbreviation VARCHAR(9),
dept_name VARCHAR(200),
PRIMARY KEY (school_code,dept_id)
);


----

create table courses(
school_code enum('L', 'B', 'A', 'F', 'E', 'T', 'I', 'W', 'S', 'U', 'M' ),
dept_id int unsigned,
course_code char(5),
name varchar(150),
primary key (school_code,course_code,dept_id)
);


-----

create table grades(
pk_grade_ID int unsigned auto_increment,
student_id int unsigned not null,
grade decimal,
school_code enum('L', 'B', 'A', 'F', 'E', 'T', 'I', 'W', 'S', 'U', 'M' ),
dept_id int unsigned,
course_code char(5), 
name varchar(150),
primary key (pk_grade_ID),
foreign key (student_id) references students(id),
foreign key (school_code,course_code,name) references courses (school_code,course_code,name)
);

----

1 回答

  • 1

    您确定're last foreign key in the Grades table shouldn'引用了Courses表中的主键(包括 dept_id 而不是 name ):

    foreign key (school_code,course_code,dept_id)
         references courses (school_code,course_code,dept_id)
    

    这工作 - http://sqlfiddle.com/#!2/451d1

    BTW - 如果可能,请考虑完全删除枚举列 . 改为创建一个查找表,并将其用作外键 .

相关问题