MySQL: Can't create table (errno: 150)

后端 未结 30 2635
误落风尘
误落风尘 2020-11-22 06:48

I am trying to import a .sql file and its failing on creating tables.

Here\'s the query that fails:

CREATE TABLE `data` (
`id` int(10) unsigned NOT NUL         


        
相关标签:
30条回答
  • 2020-11-22 07:19

    usually, the mismatch between foreign key & primary key causes the error:150.

    The foreign key must have the same datatype as the primary key. Also, if the primary key is unsigned then the foreign key must also be unsigned.

    0 讨论(0)
  • 2020-11-22 07:19

    I had a similar problem but mine was because i was adding a new field to an existing table that had data , and the new field was referencing another field from the parent table and also had the Defination of NOT NULL and without any DEFAULT VALUES. - I found out the reason things were not working was because

    1. My new field needed to autofill the blank fields with a value from the parent table on each record, before the constraint could be applied. Every time the constraint is applied it needs to leave the Integrity of the table data intact. Implementing the Constraint (Foreign Key) yet there were some database records that did not have the values from the parent table would mean the data is corrupt so MySQL would NEVER ENFORCE YOUR CONSTRAINT

    It is important to remember that under normal circumstances if you planned your database well ahead of time, and implemented constraints before data insertion this particular scenario would be avoided

    The easier Approach to avoid this gotcha is to

    • Save your database tables data
    • Truncate the table data (and table artifacts i.e indexes etc)
    • Apply the Constraints
    • Import Your Data

    I Hope this helps someone

    0 讨论(0)
  • 2020-11-22 07:22

    Sometimes MySQL is just super stupid - i can understand the reason cause of foreign-keys.. but in my case, i have just dropped the whole database, and i still get the error... why? i mean, there is no database anymore... and the sql-user i'm using has no access to any other db's on the server... i mean, the server is "empty" for the current user and i still get this error? Sorry but i guess MySQL is lying to me... but i can deal with it :) Just add these two lines of SQL around your fucky statement:

    SET FOREIGN_KEY_CHECKS = 0;
    # some code that gives you errno: 150
    SET FOREIGN_KEY_CHECKS = 1;
    

    Now the sql should be executed... If you really have a foreign-key problem, it would show up to you by the line where you will enable the checks again - this will fail then.. but my server is just quiet :)

    0 讨论(0)
  • 2020-11-22 07:23

    From the MySQL - FOREIGN KEY Constraints Documentation:

    If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the correct column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns Error 1005 and refers to Error 150 in the error message, which means that a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails due to Error 150, this means that a foreign key definition would be incorrectly formed for the altered table.

    0 讨论(0)
  • 2020-11-22 07:25

    Change the engines of your tables, only innoDB supports foreign keys

    0 讨论(0)
  • 2020-11-22 07:25

    Create the table without foreign key, then set the foreign key separately.

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