简体   繁体   English

MySQL外键,无法创建表(错误号:150)

[英]MySQL Foreign Key, Can't create table (errno: 150)

I am trying to build the database and tables for my system. 我正在尝试为我的系统构建数据库和表。 But I found that if I don't add the foreign key in the codes. 但我发现如果我不在代码中添加外键。 There is no error. 没有错误。 I've used many method try to make the codes works, but it still have error. 我已经使用了很多方法尝试使代码工作,但它仍然有错误。

I am using MySQL 5.5.31, and the codes here: CREATE DATABASE TOS; 我使用MySQL 5.5.31,代码在这里:CREATE DATABASE TOS;

DROP TABLE TOS.USER CASCADE;
DROP TABLE TOS.BILL_HEADER CASCADE;
DROP TABLE TOS.TOY CASCADE;


CREATE TABLE TOS.USER
(User Char(8),
Name Char(10),
Type Char(1),
Password Char(12),
PRIMARY KEY(User));

CREATE TABLE TOS.BILL_HEADER
(Bill_No Char(10),
CTime DateTime,
No_Of INTEGER,
Cus_No Char(5),
DTime DateTime,
PRIMARY KEY(Bill_No));

CREATE TABLE TOS.TOY
(Toy_Id Char(10),
FullN Char(50),
ShortN Char(20),
Descrip Char(20),
Price DECIMAL,
Avail Char(1),
Cat Char(1),
PRIMARY KEY(Toy_Id));

CREATE TABLE TOS.BILL_ITEM
(Bill_No Char(10),
BSeq_No INTEGER,
Toy_Id Char(10),
OTime DateTime,
Quan INT,
DCondition Char(1),
PRIMARY KEY(Bill_No,BSeq_No),
FOREIGN KEY(Bill_No) REFERENCES TOS.Bill_Header(Bill_No),
FOREIGN KEY(Toy_Id) REFERENCES TOS.TOY(Toy_Id));

Error: 错误:

1005 - Can't create table 'TOS.BILL_ITEM' (errno: 150) 1005 - 无法创建表'TOS.BILL_ITEM'(错误号:150)

Any help would be greatly appreciated. 任何帮助将不胜感激。

The non-descript error 150 is usually related to foreign key data type or length mismatches, or a missing index on the parent table's column. 非描述性错误150通常与外键数据类型或长度不匹配或父表列上的缺失索引有关。

This look s to be a matter of case sensitivity in the table name Bill_Header (should be BILL_HEADER ). 这看起来是表名Bill_Header的区分大小写(应该是BILL_HEADER )。
From the MySQL docs on identifier case sensitivity: 来自MySQL文档的标识符区分大小写:

In MySQL, databases correspond to directories within the data directory. 在MySQL中,数据库对应于数据目录中的目录。 Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). 数据库中的每个表对应于数据库目录中的至少一个文件(并且可能更多,取决于存储引擎)。 Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. 因此,底层操作系统的区分大小写在数据库和表名称的区分大小写中起作用。 This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. 这意味着数据库和表名在Windows中不区分大小写,并且在大多数Unix中区分大小写。

Fix the case and it should work: 修复案例,它应该工作:

CREATE TABLE TOS.BILL_ITEM
(Bill_No Char(10),
BSeq_No INTEGER,
Toy_Id Char(10),
OTime DateTime,
Quan INT,
DCondition Char(1),
PRIMARY KEY(Bill_No,BSeq_No),
FOREIGN KEY(Bill_No) REFERENCES TOS.BILL_HEADER(Bill_No),
# Here-----------------------------^^^^^^^^^^^^^^
FOREIGN KEY(Toy_Id) REFERENCES TOS.TOY(Toy_Id));

Since your code worked as is at SQLFiddle.com ( http://sqlfiddle.com/#!2/08d1e ) the underlying platform there must not be case-sensitive. 由于您的代码在SQLFiddle.com( http://sqlfiddle.com/#!!/08d1e )上工作,因此底层平台不得区分大小写。

上面的答案是正确的,但如果您的外键引用的表是MyISAM而不是innoDB,也会发生此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM