1

I am trying to run some basic CREATE TABLE statements for my Databases course project and am getting some strange errors.

enter image description here

When I create the table Manuf it runs fine, but when I try to create the next table, Order, using the same syntax, it does not work.

Also, when I try to create this table, Items, I get an errno: 150. I believe this has to do with my foreign key creation, but I am not exactly sure. Here is a screenshot of that.

enter image description here

I am fairly new to using MySQL so any advice would be greatly appreciated, thank you.

4

1 回答 1

1

表上的错误Order是由 ORDER 是保留字引起的。您可以使用反引号将其指定为“Order”,但最好选择完全不同的名称。

错误 150 与外键有关。密钥必须完全相同- 定义完全相同,否则 FK 将失败并出现错误 150。

此外,必须有一个具有该键定义的可用索引或一个兼容的索引(参见MySQL 手册页评论中的Kai Baku 示例)。以不同顺序索引的相同字段将失败。

首先,检查这些键是如何在原始表中定义的。例如:

 test1  varchar(50) not null
 test2  varchar(50)

兼容。我认为即使是不同的排序规则也足以让 FK 失去平衡(但我还没有检查过。其余的我可以肯定,根据我个人的痛苦经验)。

更新:我忘了提一下,如果你使用 InnoDB 表并发出SHOW ENGINE INNODB STATUS,那么出来的简介将包含一个更好的解释为什么 FK 失败,大约是顶部的三分之一。

于 2016-11-19T23:36:02.703 回答