MySQL phpMyAdmin:错误150(#1005)

时间:2015-07-06 03:41:43

标签: mysql sql

我真的不确定问题是什么,但我一直在我想要创建的Works_On表上收到错误。

CREATE TABLE Works_On(tid INT NOT NULL, startingDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, id INT, Primary Key(id), Foreign Key(tid) REFERENCES Team(tid),Foreign Key(id) REFERENCES Cases(id));

CREATE TABLE Cases(id INT NOT NULL AUTO_INCREMENT, priority INT, name VARCHAR(255) NOT NULL, description TEXT, completionDate datetime, status VARCHAR(20), cid INT, Foreign Key(cid) REFERENCES Contact(cid), Primary Key(id));

    CREATE TABLE Gives_Problem_To(tid INT NOT NULL, onDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, cid INT NOT NULL, Primary Key(tid), Foreign Key(tid) REFERENCES Team(tid),Foreign Key(cid) REFERENCES Contact(cid));

我得到的错误是:#1005 - 无法创建表

我试图找出这个错误是什么,我明白了:

Storage Engines
InnoDB Documentation

Supports transactions, row-level locking, and foreign keys 

所以这让我觉得我的外键存在问题,但我不知道问题究竟是什么。

编辑:我试图将案例的CREATE TABLE语句移到顶部,以防由于在引用之前没有创建Cases表而引起外键问题,现在我的phpAdmin加载...... ...

1 个答案:

答案 0 :(得分:0)

移动创建表格案例以解决问题。无论何时在表上创建外键约束,该表必须存在才能引用它。解决方案如下所示:

CREATE TABLE Cases(id INT NOT NULL AUTO_INCREMENT, priority INT, name VARCHAR(255) NOT NULL, description TEXT, completionDate datetime, status VARCHAR(20), cid INT, Foreign Key(cid) REFERENCES Contact(cid), Primary Key(id));

CREATE TABLE Gives_Problem_To(tid INT NOT NULL, onDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, cid INT NOT NULL, Primary Key(tid), Foreign Key(tid) REFERENCES Team(tid),Foreign Key(cid) REFERENCES Contact(cid));

CREATE TABLE Works_On(tid INT NOT NULL, startingDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, id INT, Primary Key(id), Foreign Key(tid) REFERENCES Team(tid),Foreign Key(id) REFERENCES Cases(id));