我一直在阅读有关此问题的 StackOverflow 和各种论坛,但我似乎无法弄清楚。当尝试运行下面的“CREATE TABLE Class”和“CREATE TABLE Enroll”命令时,我得到“ERROR 1005: Can't create table university.class (errno: 150)”。我使用 InnoDB 作为我的存储引擎。前两个“CREATE”语句工作正常。
我需要进行哪些更改才能使“CREATE TABLE 类和 CREATE TABLE Enroll”部分起作用?
CREATE TABLE Student (
stuId VARCHAR(6),
lastName VARCHAR(20) NOT NULL,
firstName VARCHAR(20) NOT NULL,
major VARCHAR(10),
credits FLOAT(3) DEFAULT 0,
CONSTRAINT Student_stuId_pk PRIMARY KEY (stuId),
CONSTRAINT Student_credits_cc CHECK ((credits>=0) AND (credits < 150)));
CREATE TABLE Faculty (
facId VARCHAR(6),
name VARCHAR(20) NOT NULL,
department VARCHAR(20),
rank VARCHAR(10),
CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));
CREATE TABLE Class (
classNumber VARCHAR(8),
facId VARCHAR(6) NOT NULL,
schedule VARCHAR(8),
room VARCHAR(6),
CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber),
CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty
(facId) ON DELETE SET NULL,
CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
CREATE TABLE Enroll (
stuId VARCHAR(6),
classNumber VARCHAR(8),
grade VARCHAR(2),
CONSTRAINT Enroll_classNumber_stuId_pk PRIMARY KEY
(classNumber, stuId),
CONSTRAINT Enroll_classNumber_fk FOREIGN KEY (classNumber)
REFERENCES Class (classNumber) ON DELETE CASCADE,
CONSTRAINT Enroll_stuId_fk FOREIGN KEY (stuId) REFERENCES Student
(stuId)ON DELETE CASCADE);
这是完整的命令和错误:
mysql> CREATE TABLE Class (classNumber VARCHAR(8), facId VARCHAR(6) NOT NULL, schedule VARCHAR(8), room VARCHAR(6), CONSTRAINT Class_classNumber_pk PRIMARY KEY (classNumber), CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty (facId) ON DELETE SET NULL, CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room));
ERROR 1005 (HY000): Can't create table 'university.Class' (errno: 150)