[英]Foreign Key Error - ERROR 1005 (HY000) … Can't create table … `stored_on` (errno: 150)
I'm creating two tables in mariadb.我在 mariadb 中创建了两个表。 The stored_on relation ties a shelf and a book together.
stored_on 关系将书架和书籍联系在一起。 Everything works fine except I added lib_floor to the stored on relation, and this error is suddenly appearing.
一切正常,除了我将 lib_floor 添加到存储的关系中,这个错误突然出现了。 When I remove lib_floor, the foreign key definition for lib_floor, and lib_floor from stored on primary key, there are no issues.
当我从存储在主键上的 lib_floor、lib_floor 的外键定义和 lib_floor 中删除时,没有问题。 Any ideas?
有任何想法吗? The data type is the same (INT).
数据类型相同(INT)。
CREATE TABLE shelf (
shelf_number INT NOT NULL,
lib_name VARCHAR(50) NOT NULL,
lib_floor INT NOT NULL,
FOREIGN KEY (lib_name) REFERENCES library (lib_name) ON DELETE CASCADE,
PRIMARY KEY (shelf_number,lib_name,lib_floor)
);
CREATE TABLE stored_on (
shelf_number INT NOT NULL,
lib_name VARCHAR(50) NOT NULL,
lib_floor INT NOT NULL,
isbn VARCHAR(20) NOT NULL,
total_copies INT NOT NULL,
FOREIGN KEY (shelf_number) REFERENCES shelf (shelf_number) ON DELETE CASCADE,
FOREIGN KEY (lib_name) REFERENCES shelf (lib_name) ON DELETE CASCADE,
FOREIGN KEY (lib_floor) REFERENCES shelf (lib_floor) ON DELETE CASCADE,
FOREIGN KEY (isbn) REFERENCES book (isbn) ON DELETE CASCADE,
PRIMARY KEY (shelf_number,lib_name,lib_floor,isbn)
);
Warning 1:警告 1:
|
| Warning |
警告 | 150 |
150 | Create table... .
创建表......
stored_on
with foreign key constraint failed.带有外键约束的
stored_on
失败。 There is no index in the referenced table where the referenced columns appear as the first columns near 'FOREIGN KEY (lib_floor) REFERENCES shelf (lib_floor) ON DELETE CASCADE, FOREIGN KEY (isbn) REFERENCES book (isbn) ON DELETE CASCADE, PRIMARY KEY (shelf_number,lib_name,lib_floor,isbn) )'.引用表中没有索引,其中引用的列显示为 'FOREIGN KEY (lib_floor) REFERENCES Shelf (lib_floor) ON DELETE CASCADE, FOREIGN KEY (isbn) REFERENCES book (isbn) ON DELETE CASCADE, PRIMARY KEY (货架编号,lib_name,lib_floor,isbn))'。 |
|
Error 1:错误一:
|
| Error |
错误 | 1005 |
1005 | Can't create table...
stored_on
(errno: 150 "Foreign key constraint is incorrectly formed")无法创建表...
stored_on
(错误号:150“外键约束格式不正确”)
Warning 2:警告 2:
Warning |
警告 | 1215 |
1215 | Cannot add foreign key constraint for
stored_on
无法为
stored_on
添加外键约束
The error message is fairly self-explanatory, shelf
needs to have an INDEX
in which lib_floor
is the first field mentioned.错误消息是不言自明的,
shelf
需要有一个INDEX
,其中lib_floor
是提到的第一个字段。 You can simply add an INDEX
(it doesn't need to be UNIQUE
):您可以简单地添加一个
INDEX
(它不需要是UNIQUE
):
CREATE TABLE shelf (
shelf_number INT NOT NULL,
lib_name VARCHAR(50) NOT NULL,
lib_floor INT NOT NULL,
FOREIGN KEY (lib_name) REFERENCES library (lib_name) ON DELETE CASCADE,
PRIMARY KEY (shelf_number,lib_name,lib_floor),
INDEX (lib_floor)
);
Note that you would also get this error from lib_name
, but the FOREIGN KEY
declaration for lib_name
in shelf
automatically creates an index on it.请注意,您也会从
lib_name
收到此错误,但shelf
中lib_name
的FOREIGN KEY
声明会自动在其上创建索引。
Foreign keys are tricky.外键很棘手。 There are several workarounds for err 150 (and others)
err 150(和其他)有几种解决方法
Disable FKs, CREATE TABLEs, re-enable FKs禁用 FK、创建表、重新启用 FK
Apply the FKs via ALTER after doing the relevant CREATE TABLEs在执行相关的 CREATE TABLEs 之后通过 ALTER 应用 FK
(others?) (其他?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.