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

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

    This error can occur if two tables have a reference, for example, one table is Student and another table is Education, and we want the Education table to have a foreign key reference of Student table. In this instance the column data type for both tables should be same, otherwise it will generate an error.

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

    Error 150 means you have a problem with your foreign key. Possibly the key on the foreign table isn't the exact same type?

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

    After cruising through the answers above, and experimenting a bit, this is an effective way to solve Foreign Key errors in MySQL (1005 - error 150).

    For the foreign key to be properly created, all MySQL asks for is:

    • All referenced keys MUST have either PRIMARY or UNIQUE index.
    • Referencing Column again MUST have identical data type to the Referenced column.

    Satisfy these requirements and all will be well.

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

    I faced this kind of issue while creating DB from the textfile.

    mysql -uroot -padmin < E:\important\sampdb\createdb.sql
    mysql -uroot -padmin sampdb < E:\important\sampdb\create_student.sql
    mysql -uroot -padmin sampdb < E:\important\sampdb\create_absence.sql
    
    mysql -uroot -padmin sampdb < E:\important\sampdb\insert_student.sql
    mysql -uroot -padmin sampdb < E:\important\sampdb\insert_absence.sql
    
    mysql -uroot -padmin sampdb < E:\important\sampdb\load_student.sql
    mysql -uroot -padmin sampdb < E:\important\sampdb\load_absence.sql 
    

    I just wrote the above lines in Create.batand run the bat file.

    My mistake is in the sequence order of execution in my sql files. I tried to create table with primary key and also foreign key. While its running it will search for the reference table but tables are not there. So it will return those kind of error.

    If you creating tables with foreign key then check the reference tables were present or not. And also check the name of the reference tables and fields.

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

    I experienced this error when have ported Windows application to Linux. In Windows, database table names are case-insensitive, and in Linux they are case-sensitive, probably because of file system difference. So, on Windows table Table1 is the same as table1, and in REFERENCES both table1 and Table1 works. On Linux, when application used table1 instead of Table1 when it created database structure I saw error #150; when I made correct character case in Table1 references, it started to work on Linux too. So, if nothing else helps, make you sure that in REFERENCES you use correct character case in table name when you on Linux.

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

    I had the same error. In my case the reason for the error was that I had a ON DELETE SET NULL statement in the constraint while the field on which I put the constraint in its definition had a NOT NULL statement. Allowing NULL in the field solved the problem.

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