MySQL“错误1005(HY000):无法创build表'foo。#sql-12c_4'(errno:150)”

我正在创build数据库foo一些表,但每次我最终与errno 150有关的外键。 首先,这是我创build表的代码:

 CREATE TABLE Clients ( client_id CHAR(10) NOT NULL , client_name CHAR(50) NOT NULL , provisional_license_num CHAR(50) NOT NULL , client_address CHAR(50) NULL , client_city CHAR(50) NULL , client_county CHAR(50) NULL , client_zip CHAR(10) NULL , client_phone INT NULL , client_email CHAR(255) NULL , client_dob DATETIME NULL , test_attempts INT NULL ); CREATE TABLE Applications ( application_id CHAR(10) NOT NULL , office_id INT NOT NULL , client_id CHAR(10) NOT NULL , instructor_id CHAR(10) NOT NULL , car_id CHAR(10) NOT NULL , application_date DATETIME NULL ); CREATE TABLE Instructors ( instructor_id CHAR(10) NOT NULL , office_id INT NOT NULL , instructor_name CHAR(50) NOT NULL , instructor_address CHAR(50) NULL , instructor_city CHAR(50) NULL , instructor_county CHAR(50) NULL , instructor_zip CHAR(10) NULL , instructor_phone INT NULL , instructor_email CHAR(255) NULL , instructor_dob DATETIME NULL , lessons_given INT NULL ); CREATE TABLE Cars ( car_id CHAR(10) NOT NULL , office_id INT NOT NULL , engine_serial_num CHAR(10) NULL , registration_num CHAR(10) NULL , car_make CHAR(50) NULL , car_model CHAR(50) NULL ); CREATE TABLE Offices ( office_id INT NOT NULL , office_address CHAR(50) NULL , office_city CHAR(50) NULL , office_County CHAR(50) NULL , office_zip CHAR(10) NULL , office_phone INT NULL , office_email CHAR(255) NULL ); CREATE TABLE Lessons ( lesson_num INT NOT NULL , client_id CHAR(10) NOT NULL , date DATETIME NOT NULL , time DATETIME NOT NULL , milegage_used DECIMAL(5, 2) NULL , progress CHAR(50) NULL ); CREATE TABLE DrivingTests ( test_num INT NOT NULL , client_id CHAR(10) NOT NULL , test_date DATETIME NOT NULL , seat_num INT NOT NULL , score INT NULL , test_notes CHAR(255) NULL ); ALTER TABLE Clients ADD PRIMARY KEY (client_id); ALTER TABLE Applications ADD PRIMARY KEY (application_id); ALTER TABLE Instructors ADD PRIMARY KEY (instructor_id); ALTER TABLE Offices ADD PRIMARY KEY (office_id); ALTER TABLE Lessons ADD PRIMARY KEY (lesson_num); ALTER TABLE DrivingTests ADD PRIMARY KEY (test_num); ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id); ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id); ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Instructors FOREIGN KEY (instructor_id) REFERENCES Instructors (instructor_id); ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY (car_id) REFERENCES Cars (car_id); ALTER TABLE Lessons ADD CONSTRAINT FK_Lessons_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id); ALTER TABLE Cars ADD CONSTRAINT FK_Cars_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id); ALTER TABLE Clients ADD CONSTRAINT FK_DrivingTests_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id); 

这些是我得到的错误:

 mysql> ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY (car_id) REFERENCES Cars (car_id); ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150) 

我运行了SHOW ENGINE INNODB STATUS ,它给出了更详细的错误描述:

 ------------------------ LATEST FOREIGN KEY ERROR ------------------------ 100509 20:59:49 Error in foreign key constraint of table foo/#sql-12c_4: FOREIGN KEY (car_id) REFERENCES Cars (car_id): Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables. See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html for correct foreign key definition. ------------ 

我search了StackOverflow和其他地方的在线 – 在这里find一个有用的博客post,指出如何解决这个错误 – 但我不明白什么是错的。 任何帮助,将不胜感激!

你应该让car_id成为汽车的主钥匙。

注:我有同样的问题,这是因为引用的字段在2个不同的表(他们有完全相同的types)不同的sorting规则。

确保所有引用的字段具有相同的types和相同的sorting规则!

检查两个表具有相同的引擎。 例如,如果你有:

 CREATE Table FOO (); 

和:

 CREATE Table BAR () ENGINE=INNODB; 

如果您尝试从表BAR到表FOO创build一个约束,它将不适用于某些MySQL版本。

通过以下方式解决问题:

 CREATE Table FOO () ENGINE=INNODB; 

微妙的,但是这个错误让我感到遗憾,因为我忘记声明一个smallint列作为unsigned来匹配被引用的现有的“smallint unsigned”表。 有一个无符号,一个没有无符号导致MySQL阻止在新表上创build外键。

 id smallint(3) not null 

不匹配,为了外键的缘故,

 id smallint(3) unsigned not null 

当我试图:我得到了这个完全没有价值的信息错误

 ALTER TABLE `comments` ADD CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE; 

我的问题是在我的评论表中,user_id被定义为:

 `user_id` int(10) unsigned NOT NULL 

所以…在我的情况下,问题是与NOT NULL和ON DELETE SET NULL之间的冲突。

我使用Ubuntu的Linux,在我的情况下,错误是由不正确的语句语法(我发现通过在terminal键入perror 150,造成的

MySQL错误代码150:外键约束错误地形成

从中更改查询的语法

 alter table scale add constraint foreign key (year_id) references year.id; 

 alter table scale add constraint foreign key (year_id) references year(id); 

修复。

而且两个表都需要有相同的字符集。

例如

 CREATE TABLE1 ( FIELD1 VARCHAR(100) NOT NULL PRIMARY KEY, FIELD2 VARCHAR(100) NOT NULL )ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_bin; 

 CREATE TABLE2 ( Field3 varchar(64) NOT NULL PRIMARY KEY, Field4 varchar(64) NOT NULL, CONSTRAINT FORIGEN KEY (Field3) REFERENCES TABLE1(FIELD1) ) ENGINE=InnoDB; 

会失败,因为他们有不同的字符集。 这是另一个微妙的失败,其中mysql返回相同的错误。

引用的字段必须是引用表中的“密钥”,而不一定是主键。 所以“car_id”应该是主键,或者在“Cars”表中用NOT NULL和UNIQUE约束来定义。

此外,这两个领域必须是相同的types和整理。

在尝试导入整个数据库(〜800 MB)时,我还收到了这个错误(对于多个表)以及约束错误和MySQL连接和断开连接。 我的问题是MySQL服务器最大允许数据包太低的结果。 要解决这个问题(在Mac上):

  • 打开/private/etc/my.conf
  • MySQL服务器下 ,将max_allowed_packet从1M更改为4M(您可能需要尝试使用此值。)
  • 重新启动MySQL

之后导入数据库。

注意我正在为Mac OS X(x86 64位)运行MySQL 5.5.12。

检查使您所引用的字段是与外键完全匹配,在我的情况下,一个是无符号的,另一个是签名,所以我只是改变他们匹配,这工作

ALTER TABLE customer_information ADD CONSTRAINT fk_customer_information1 FOREIGN KEY( user_id )REFERENCES usersid )ON DELETE CASCADE ON UPDATE CASCADE

解决了:

检查以确保Primary_Key和Foreign_Key与数据types完全匹配。 如果一个签名另一个无符号,它将失败。 好的做法是确保两者都是无符号整数。

所有,我解决了一个问题,并想分享它:

我有这个错误<>这个问题是在我的声明:

alter table system_registro_de_modificacion add foreign key(usuariomodificador_id)REFERENCES Usuario(id)On delete restrict;

我错误地写了CASING:它在Windows WAMP中工作,但在Linux MySQL中对CASING更为严格,所以写了“Usuario”而不是“usuario”(确切的shell),产生了错误,套pipe。

我正在使用重复的外键名称。

重命名FK名称解决了我的问题。

澄清:

两个表都有一个叫PK1,FK1等的约束。重新命名它们/使得名称唯一解决了问题。

被引用的列必须是单个列的索引或多列索引中的第一列,以及相同的types和相同的sorting规则。

我的两个表格有不同的sorting规则。 它可以通过发出show table状态(如table_name来显示,并且可以通过将alter table table_name convert转换为字符集utf8来更改sorting规则。