简体   繁体   English

外键错误 - 错误 1005 (HY000) … 无法创建表 … `stored_on` (errno: 150)

[英]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收到此错误,但shelflib_nameFOREIGN KEY声明会自动在其上创建索引。

Demo on dbfiddle dbfiddle 上的演示

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.

相关问题 外键问题:错误1005(HY000):无法创建表(错误号:150) - Foreign key issue:ERROR 1005 (HY000): Can't create table (errno: 150) SQLSTATE[HY000]: 一般错误: 1005 Can't create table `test`.`members` (errno: 150 “Foreign key constraint is misformed”) - SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`members` (errno: 150 “Foreign key constraint is incorrectly formed”) SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms") - SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") 错误1005(HY000):无法创建表“登录”(错误号:150) - ERROR 1005 (HY000): Can't create table 'login' (errno: 150) 错误1005(HY000):无法创建表“ ./quotes/actions.frm”(错误号:150) - ERROR 1005 (HY000): Can't create table './quotes/actions.frm' (errno: 150) 错误1005(HY000):无法创建表“ project2.fulltime”(错误号:150) - ERROR 1005 (HY000): Can't create table 'project2.fulltime' (errno: 150) 错误1005(HY000):无法创建表“ test.sports”(错误号:150) - ERROR 1005 (HY000): Can't create table 'test.sports' (errno: 150) 错误1005(HY000):无法创建表“ CBDB.Subsystem”(错误号:150) - ERROR 1005 (HY000): Can't create table 'CBDB.Subsystem' (errno: 150) 错误1005(HY000):无法创建表GSRS.ussr(错误号:150) - ERROR 1005 (HY000): Can't create table 'GSRS.ussr' (errno: 150) 错误 1005 (hy000) 无法在 mysql 中创建表 (errno 150) - error 1005 (hy000) can't create table (errno 150) in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM