Altering a table returns an error (errno: 150) if a foreign key definition is incorrectly formed for the altered table. Dropping an index required by a foreign key constraint. The foreign key constraint must be removed before dropping the index.
To solve 'MySQL ERROR 1005: Can't create table (errno: 150)' you likely just have to ensure that your foreign key has the exact same type as the primary key. Hope it helps.
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.
Error Code: 1005 -- there is a wrong primary key reference in your code
Usually it's due to a referenced foreign key field that does not exist. It might be you have a typo mistake, or check case it should be same, or there's a field-type mismatch. Foreign key-linked fields must match definitions exactly.
Some known causes may be:
INT(10)
the key field needs to be INT
as well and not BIGINT
or SMALLINT
or TINYINT
. You should also check that one is not SIGNED
and the other is UNSIGNED
. They both need to be exactly the same.MyISAM
table. In order to use foreign keys, the tables must both be InnoDB
. (Actually, if both tables are MyISAM
then you won’t get an error message - it just won’t create the key.) In Query Browser, you can specify the table type.ON
DELETE
SET
NULL
, but the relevant key field is set to NOT
NULL
. You can fix this by either changing your cascade or setting the field to allow NULL
values.ALTER
statement or you have mistyped one of the field names in the relationshipFor more details, refer to: MySQL Error Number 1005 Can’t create table
This could also happen when exporting your database from one server to another and the tables are listed in alphabetical order by default.
So, your first table could have a foreign key of another table that is yet to be created. In such cases, disable foreign_key_checks and create the database.
Just add the following to your script:
SET FOREIGN_KEY_CHECKS=0;
and it shall work.
Very often it happens when the foreign key and the reference key don't have the same type or same length.
Sometimes it is due to the master table is dropped (maybe by disabling foreign_key_checks), but the foreign key CONSTRAINT still exists in other tables. In my case I had dropped the table and tried to recreate it, but it was throwing the same error for me.
So try dropping all the foreign key CONSTRAINTs from all the tables if there are any and then update or create the table.
I had a similar error. The problem had to do with the child and parent table not having the same charset and collation. This can be fixed by appending ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `country` (`id` INT(11) NOT NULL AUTO_INCREMENT,...) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
... on the SQL statement means that there is some missing code.
The foreign key has to have the exact same type as the primary key that it references. For the example has the type “INT UNSIGNED NOT NULL” the foreing key also have to “INT UNSIGNED NOT NULL”
CREATE TABLE employees(
id_empl INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE offices(
id_office INT UNSIGNED NOT NULL AUTO_INCREMENT,
id_empl INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
CONSTRAINT `constraint1` FOREIGN KEY (`id_empl`) REFERENCES `employees` (`id_empl`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='my offices';
Error Code: 1005
I had a similar issue, so here are few things that I did try (not in any order, except for the solution :) )
Finally, I saw that I had two editors open. One that in PhpStorm (JetBrains) and the other MySQL workbench. It seems that the PhpStorm / MySQL Workbench creates some kind of edit lock.
I closed PhpStorm just to check if locking was the case (it could have been the other way around). This solved my problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With