当外键存在的MySQL 5.5外键约束失败当外键存在的MySQL 5.5外键约束失败(MySQL 5

2019-05-12 05:35发布

刚安装了MySQL 5.5在Mac OS X 10.6和我对许多表一个奇怪的问题。 下面是一个例子。 插入行失败,一个外键约束时,它不应该。 外键引用它确实存在。 有任何想法吗?

mysql> show create table Language;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Language | CREATE TABLE `Language` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Code` varchar(2) NOT NULL,
  `Name` varchar(63) CHARACTER SET utf8 DEFAULT NULL,
  `Variant` varchar(63) CHARACTER SET utf8 DEFAULT NULL,
  `Country_Id` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Code` (`Code`,`Country_Id`,`Variant`),
  KEY `FKA3ACF7789C1796EB` (`Country_Id`),
  CONSTRAINT `FKA3ACF7789C1796EB` FOREIGN KEY (`Country_Id`) REFERENCES `Country` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table Language_Phrases;
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                    |
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Language_Phrases | CREATE TABLE `Language_Phrases` (
  `Language_Id` int(11) NOT NULL,
  `Phrase` varchar(255) DEFAULT NULL,
  `Label` varchar(255) NOT NULL,
  PRIMARY KEY (`Language_Id`,`Label`),
  KEY `FK8B4876F3AEC1DBE9` (`Language_Id`),
  CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from Language;
+----+------+----------+---------+------------+
| Id | Code | Name     | Variant | Country_Id |
+----+------+----------+---------+------------+
|  1 | en   | English  |         |        235 |
|  2 | ro   | Romanian |         |        181 |
+----+------+----------+---------+------------+
2 rows in set (0.00 sec)

mysql> select * from Language_Phrases;
Empty set (0.00 sec)

mysql> INSERT INTO Language_Phrases (Language_Id, Label, Phrase) VALUES (1, 'exampleLabel', 'Some phrase');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`dev`.`language_phrases`, CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`))
mysql> 

更新 :删除和重建数据库几次后,我做了一个show engine innodb status上面的失败后插入,并得到了这个令人惊讶的结果。 父语言表中没有找到! 这似乎很奇怪......任何想法?

------------------------
LATEST FOREIGN KEY ERROR
------------------------
110406  9:55:49 Transaction:
TRANSACTION CA3B, ACTIVE 0 sec, OS thread id 4494462976 inserting
mysql tables in use 1, locked 1
1 lock struct(s), heap size 376, 0 row lock(s)
MySQL thread id 25, query id 50720 localhost root update
INSERT INTO Language_Phrases (Language_Id, Label, Phrase) VALUES (1, 'exampleLabel', 'Some phrase')
Foreign key constraint fails for table `dev`.`language_phrases`:
,
  CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`)
Trying to add to index `PRIMARY` tuple:
DATA TUPLE: 5 fields;
 0: len 4; hex 80000001; asc     ;;
 1: len 17; hex 747970654d69736d617463682e79656172; asc exampleLabel;;
 2: len 6; hex 00000000ca3b; asc      ;;;
 3: len 7; hex 00000000000000; asc        ;;
 4: len 21; hex 59656172206d7573742062652061206e756d626572; asc Some phrase;;

But the parent table `dev`.`Language`
or its .ibd file does not currently exist!

更新2:原来,这简直是在MySQL中一个巨大的错误。 显然,最新的MySQL版本不在Mac OS X 10.6完全工作(也许更早版本吗?)。 降级到5.5.8似乎工作。 非常令人惊讶的。

Answer 1:

但这似乎是在Mac OS X从MySQL 5.5.9引入了一个错误: http://bugs.mysql.com/bug.php?id=60309

它被标记为固定在5.5.13(5月31日发布),并在发行说明中提到: http://dev.mysql.com/doc/refman/5.5/en/news-5-5-13.html

另外,还有在我已经验证了5.5.10及以下转载的bug报告中列出的解决方法:


[20 Mar 11:29] Harald Neiss

I also received a new MBP and reinstalled MySQL (mysql-5.5.10-osx10.6-x86_64). Finally I
came across the same problem as described above. So here is the query result and what I
did to solve it.

mysql> show variables like 'lower%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | ON    |
| lower_case_table_names | 2     |
+------------------------+-------+
2 rows in set (0.00 sec)

Dropped database, created the file /etc/my.cnf with the following content:

[mysqld]
lower_case_table_names=1

Restarted the MySQL daemon and repeated the query:

mysql> show variables like 'lower%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| lower_case_file_system | ON    |
| lower_case_table_names | 1     |
+------------------------+-------+
2 rows in set (0.00 sec)

I recreated the tables and everything works fine.



Answer 2:

不奇怪恕我直言。 我已经在MySQL中发现许多错误。 例如运行具有where子句如“WHERE some_tinyint_column = 0”将产生没有数据时,它应该,但重写子句“WHERE(NOT some_tinyint_column = 1)”的查询产生的结果。 经过一番研究,我发现,这是被认为已经修正了一个错误,但我用的版本中,错误仍然存​​在。

结论:当事情在MySQL使得完全没有意义的,我通常会发现它是安全的假设这是一个错误,并开始研究用于沿着这些线路的信息。



Answer 3:

检查的数字类型属性Language_Phrases (Language_Id)LanguageId

两者应该是未签约ZEROFILL签字



Answer 4:

* MySQL的> INSERT INTO Language_Phrases(LANGUAGE_ID,标签,词组)VALUES(1, 'exampleLabel', '部分短语'); ERROR 1452(23000):不能添加或更新子行:... *

您正在试图插入1为LANGUAGE_ID,但表语言具有财产AUTO_INCREMENT = 3。 在这种情况下,你应该使用3或更高。



Answer 5:

今天我同样的错误。 就我而言,我用一个脚本来重建他们的所有记录了几桌。

事实上,我已经意识到,“发动机”型是我的表之间的不同:一个是MyISAM和第二个(FK的的引用)是InnoDB的。 我已经改变了我的所有表InnoDB和现在一切正常。

这个脚本会产生一个更新脚本文件( 参考 )

mysql -u DB_USER -pDB_PASSWORD --default-character-set=utf8  DATABASE_NAME -e "SELECT CONCAT('ALTER TABLE ', table_name, ' ENGINE=InnoDB;') AS sql_statements FROM information_schema.tables AS tb WHERE table_schema = database() AND ENGINE = 'MyISAM' AND TABLE_TYPE = 'BASE TABLE' ORDER BY table_name DESC;" > ./alter_InnoDb.sql

您必须删除第一行包含行文本“sql_statements”在“alter_InnoDb.sql”。

在此之后,您可以在数据库中执行脚本来更正此错误:

mysql -u DB_USER -pDB_PASSWORD --default-character-set=utf8  DATABASE_NAME < ./ alter_InnoDb.sql


文章来源: MySQL 5.5 foreign key constraint fails when foreign key exists