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

后端 未结 2 2345
轮回少年
轮回少年 2021-02-20 09:25

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

相关标签:
2条回答
  • 2021-02-20 10:16

    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.

    This look s to be a matter of case sensitivity in the table name Bill_Header (should be BILL_HEADER).
    From the MySQL docs on identifier case sensitivity:

    In MySQL, databases correspond to directories within the data directory. 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.

    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.

    0 讨论(0)
  • 2021-02-20 10:20

    Above answer is correct, but this error can also happen if the table your foreign key is referencing is MyISAM instead of innoDB.

    0 讨论(0)
提交回复
热议问题