1

我一直在阅读有关此问题的 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)
4

3 回答 3

2

删除 facId 定义中的 NOT NULL

于 2013-09-26T05:47:01.063 回答
0

您是否先创建了教师表。检查这个链接

Inno 数据库错误

这表示如果你得到 1005 错误 150 这意味着

未正确形成外键约束

于 2013-09-26T05:43:14.210 回答
0

这是由于外键错误而发生的。要获取有关外键错误的更多详细信息,请运行 SHOW ENGINE INNODB STATUS\G 并查看“LATEST FOREIGN KEY ERROR”部分。

我认为它会告诉外键无效,因为 Faculty.facid 上没有唯一索引或主键索引。

于 2013-09-26T05:45:54.023 回答