This is another stupid error. It has to do with trying to successfully set foreign keys in MySQL.
ERROR 1005: Can’t create table (errno: 150)
Great, that’s fantastic. Here’s an example of where this error will occur.
CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);
So I’m trying to make the table “other” reference the table “main” through the foreign key “main_id” and, if you try it, it’ll throw an Error 150.
Want the solution?
The foreign key “main_id” has to have the exact same type as the primary key that it references. In the example, “main_id” in the table “other” has the type INT NOT NULL while “id” in the table “main” has the type “INT UNSIGNED NOT NULL” and also AUTO_INCREMENT, but that isn’t something we have to worry about. To make things incredibly clear, here’s the working example.
CREATE TABLE main(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE other(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
main_id INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(main_id) REFERENCES main(id)
);
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.
The database engine is not innodb
This might be another answer, though I haven’t tested it. If so, you can make sure your tables are INNODB by specifying in the CREATE TABLE statement:
ENGINE=InnoDB;
Or you can alter existing tables with this:
ALTER TABLE my_table ENGINE = InnoDB;
Here is an extensive list of different reasons that you can get errno: 150, along with other MySQL foreign key errors:
http://eliacom.com/wpErrNo150.php
Awesome, thanks :)
worked
Thanks very much!!! It worked fine!!!!
Thank you, worked like a charm!
Thanks! Awesome. =D
Espero esto le sirva a alguien, investigando sobre este error se produce por dibersas causas las principales que no cohinciden la llave primaria con la foranea, es decir tienen tipos diferentes, es decir int varchar etc, o tienen atributos diferentes como por ejemplo el unsigned o binary etc, si es el caso anterior se soluciona poniendo tanto llave primaria como foranea del mismo tipo las dos y con los mismos atributos, tambien es recomendado que sean de la misma longitud ya que si bien es sierto que la longitud no es tomada encuenta al momento de relacionar puede causar problemas al momento de pasar a la incercion de datos y demas dml.
Otro motivo por el cual ocurre este error es por que esta repetido el nombre de campo
es decir puede que en la bd ya haya un campo con el nombre id por lo que la solucion seria simplemente cambiar el nombre a id1 por ejemplo.
otro motivo por el cual ocurre este error es el tipo demotor usado en las tablas es decir puede ser que una sea innodb y la otra myisam la solucion es que las dos deben ser del mismo tipo de motor.
Por ultimo el motivo puede ser tambien el constrainter puede que este configurado como on delete set null y el campo noadmite valores nulos es decir es not null
Como ya lo habia dicho espero le sirva a alguien esta info y si es asi pueden pegar el comentario en algun otro foro para ayudar a mas gente, saludos
Thanks for writing such a good post to motivate the world to do some better things. Everyone have a cheap and but comfort zone where they feel relaxed. But there are rare people in the world accept the challenges and give something different what people looking for. I am sure people will enjoy your tips.
Pingback: Google
Pingback: URL
Pingback: Google
Pingback: Google
Pingback: Google
Pingback: Google
Pingback: tottally free dating sites
Pingback: Google
Pingback: Top 7 Error 1005 Hy000 Can T Create Table The 27 Latest Answer
Pingback: Mysql Errno 150? Top 7 Best Answers - Budget-template.com
Pingback: Mysql Error 1005? Top 7 Best Answers - Budget-template.com
Pingback: online pharmacies
Pingback: madridbet
Pingback: madridbet
Pingback: meritking
Pingback: sex
Pingback: fuvk google
Pingback: fuck google
Pingback: madridbet
Pingback: Ошибка ссылку foreign key невозможно добавить 1005
Pingback: Error 1005 mysql
Pingback: Error code 1005 sql
Pingback: madridbet
Pingback: meritking giriş
Pingback: Error code 150 in mysql
Pingback: Sql create table error 150
Pingback: Sql general error 1005
Pingback: Sql error 1005 foreign key constraint
Pingback: Ошибка 1005 mysql — Решение и исправление самых разных ошибок
Pingback: Ошибка ссылку foreign key невозможно добавить 1005 — Большая энциклопедия ошибок и их решений