错误1005(HY000):无法创建表' test.sports' (错误:150)

时间:2016-11-25 02:46:44

标签: mysql foreign-keys

table: records

抛出错误:

  

错误1005(HY000):无法创建表格' test.sports' (错误:150)。

我需要帮助才能解决它。

 CREATE TABLE sports(
-> interest text,
-> prize_money int,
-> sp_id int NOT NULL,
-> CONSTRAINT fk_sports 
-> FOREIGN KEY(sp_id)
-> REFERENCES records(id)
-> ON DELETE CASCADE
-> ON UPDATE CASCADE
-> ) ENGINE=INNODB;

1 个答案:

答案 0 :(得分:0)

您的records表没有名为id的列。相反,它的主键列是student_id,因此您可能希望从sports中的外键引用它。试试这个定义:

CREATE TABLE sports(
    interest text,
    prize_money int,
    sp_id int NOT NULL,
    CONSTRAINT fk_sports 
    FOREIGN KEY(sp_id)
    REFERENCES records(student_id)
    ON DELETE CASCADE
    ON UPDATE CASCADE
) ENGINE=INNODB;