1

首先感谢您的帮助和支持的话。当我从网上学习 MySQL 时,我知道我会犯很多错误,并提前感谢您的耐心和理解。

我正在制作基于 Java 的项目,然后创建一个 MySQL 数据库和表。通常它进展顺利,直到我遇到这个问题。到目前为止,它给了我最大的头痛。我阅读了很多关于错误 150 的文章。我在 MySQL 网站上阅读了错误 150,我认为我已经满足了发生错误 150 的十个原因。

我正在调用的是创建每个表。以下是一些涵盖我遇到的问题的表格。

这是工作日历表

    private String workCalandar = "CREATE TABLE WorkCalendar ("
        + "WorkCalendarIdNo INT(64) NOT NULL AUTO_INCREMENT,"
        + "WorkCalendarDate Date,"
        + "WorkCalendarDayCount INT(64),"
        + "WorkDayTypeIdNo INT(64),"
        + "PRIMARY KEY(WorkCalendarIdNo),"
        + "FOREIGN KEY(WorkDayTypeIdNo) REFERENCES WorkDayType(WorkDayTypeIdNo)"
        + ")";

这是部门表

    private String department = "CREATE TABLE Department ("
        + "DeptIdNo INT(64) NOT NULL AUTO_INCREMENT,"
        + "DeptName VARCHAR(25),"
        + "PRIMARY KEY(DeptIdNo)"
        + ")";

这是专家表

    private String specialist = "CREATE TABLE Specialist ("
        + "SpecIdNo INT(64) NOT NULL AUTO_INCREMENT,"
        + "PrefixIdNo INT(64),"
        + "SpecFirstName VARCHAR(30),"
        + "SpecSurname VARCHAR(35),"
        + "SpecDisplayName VARCHAR(72),"
        + "DeptIdNo INT(64),"
        + "PRIMARY KEY(SpecIdNo),"
        + "FOREIGN KEY(PrefixIdNo) REFERENCES Prefix(PrefixIdNo),"
        + "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo)"
        + ")";

这是新推荐表

    private String newReferral = "CREATE TABLE NewReferral("
        + "NewReferralIdNo INT(64) NOT NULL AUTO_INCREMENT,"
        + "PatientNumber VARCHAR(12),"
        + "NewReferralDate Date,"
        + "DeptIdNo INT(64),"
        + "SpecIdNo INT(64),"
        + "NewReferralMatched BOOLEAN,"
        + "WorkCalendarIdNo INT(64),"
        + "PRIMARY KEY(NewReferralIdNo),"
        + "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo),"
        + "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
        + "FOREIGN KEY(WorkCalendarIdNo) REFERENCES WorkCalendar(WorkCalendarIdNo)"
        + ")";

错误是 Can't create table 'basque30.newreferral' (errno: 150)

我已将错误跟踪到以下行

    + "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"

有人可以指出我的方法的错误并建议我如何解决它。

4

2 回答 2

1
Error no: 150 -- There is a wrong primary key reference in your code.

It's due to a reference FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo)
field does not exist.   

正如你提到的

I have tracked the error down to the following line

+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
                                      ↑  

确保表Specialist存在以便在另一个表中引用为外键。
检查表的创建顺序。


不相关,但将来可能会有所帮助。

如果您在服务器上拥有管理员权限,您可能希望从运行 MySQL 命令开始

SHOW INNODB STATUS  

对于 MySQL 5.5

SHOW ENGINE INNODB STATUS

收到错误后立即。此命令显示日志信息和错误详细信息

于 2013-10-19T13:02:13.127 回答
1

确保引用的表已经存在。

于 2013-10-19T00:05:16.597 回答