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

后端 未结 30 2676
误落风尘
误落风尘 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:26

    I think all these answers while correct are misleading to the question.

    The actual answer is this before you start a restore, if you're restoring a dump file with foreign keys:

    SET FOREIGN_KEY_CHECKS=0;
    

    because naturally the restore will be creating some constraints before the foreign table even exists.

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

    If the PK table is created in one CHARSET and then you create FK table in another CHARSET..then also you might get this error...I too got this error but after changing the charset to PK charset then it got executed without errors

    create table users
    (
    ------------
    -------------
    )DEFAULT CHARSET=latin1;
    
    
    create table Emp
    (
    ---------
    ---------
    ---------
    FOREIGN KEY (userid) REFERENCES users(id) on update cascade on delete cascade)ENGINE=InnoDB, DEFAULT CHARSET=latin1;
    
    0 讨论(0)
  • 2020-11-22 07:27

    I've corrected the problem by making the variable accept null

    ALTER TABLE `ajout_norme` 
    CHANGE `type_norme_code` `type_norme_code` VARCHAR( 2 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
    
    0 讨论(0)
  • 2020-11-22 07:28

    The column of PARENT table to which you are referring to from child table has to be unique. If it is not, cause an error no 150.

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

    Data types must match exactly. If you are dealing with varchar types, the tables must use the same collation.

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

    There are quite a few things that can cause errno 150, so for people searching this topic, here is what I think is a close to exhaustive list (source Causes of Errno 150):

    For errno 150 or errno 121, simply typing in SHOW ENGINE INNODB STATUS, there is a section called "LATEST FOREIGN KEY ERROR". Under that it will give you a very helpful error message, which typically will tell you right away what is the matter. You need SUPER privileges to run it, so if you don't have that, you'll just have to test out the following scenarios.

    1) Data Types Don't Match: The types of the columns have to be the same

    2) Parent Columns Not Indexed (Or Indexed in Wrong Order)

    3) Column Collations Don't Match

    4) Using SET NULL on a NOT NULL Column

    5) Table Collations Don't Match: even if the column collations match, on some MySQL versions this can be a problem.

    6) Parent Column Doesn't Actually Exist In Parent Table. Check spelling (and perhaps a space at the beginning or end of column)

    7) One of the indexes on one of the columns is incomplete, or the column is too long for a complete index. Note that MySQL (unless you tweak it) has a maximum single column key length of 767 bytes (this corresponds to a varchar(255) UTF column)

    In case you get an errno 121, here are a couple of causes:

    1) The constraint name you chose is already taken

    2) On some systems if there is a case difference in your statement and table names. This can bite you if you go from one server to another that have different case handling rules.

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