简体   繁体   English

MySQL外键问题

[英]MySQL foreign key problem

A client of mine recently formatted his machine and re-installed MySQL Server and my application. 我的一个客户最近格式化了他的计算机,然后重新安装了MySQL Server和我的应用程序。 He complained that deleting records from master table is not affecting the child tables. 他抱怨说,从主表中删除记录不会影响子表。 I requested him to send the backup of the database. 我要求他发送数据库备份。 When I restored the database, I found that the Table Engine has changed to MyISAM whereas they were set to InnoDB. 还原数据库时,我发现表引擎已更改为MyISAM,而将它们设置为InnoDB。

I deleted the records from the child table that were absent in the primary table. 我从子表中删除了主表中不存在的记录。 After this when I am not re-setting the Foreign Key Index, it displays error: "Foreign key contraint failed. Error 1005" and sometimes error: 150. 此后,当我不重新设置外键索引时,它会显示错误:“外键约束失败。错误1005”,有时还会出现错误:150。

I have double checked the rows that might be left in either the primary table or in the child table, but nothing seems to be working. 我已经仔细检查了主表或子表中可能剩余的行,但似乎没有任何作用。

The primary table has two columns that combinedly form a Primary Key. 主表有两列,它们共同构成一个主键。 The columns are: BillNo, BillDate. 这些列是:BillNo,BillDate。

Please assist. 请协助。

This is a widely known MySQL pitfall; 这是众所周知的MySQL陷阱。 I have hit this problem a few times myself. 我本人几次遇到这个问题。 They probably had some problem with InnoDB, and restored their database from backups. 他们可能在使用InnoDB时遇到了问题,并从备份还原了数据库。 Since InnoDB wasn't working, it fell back to the MyISAM storage engine which doesn't support integrity constraints (like foreign keys). 由于InnoDB无法正常工作,因此它只能使用MyISAM存储引擎,该引擎不支持完整性约束(例如外键)。

Basically the problem is that, if the InnoDB engine fails to start for whatever reason (usually configuration problems) -- then MySQL silently falls back to the MyISAM engine. 基本上,问题在于,如果InnoDB引擎由于某种原因而无法启动(通常是配置问题),则MySQL会默默地退回到MyISAM引擎。 Even if your statement says: 即使您的陈述说:

CREATE TABLE () ENGINE=InnoDB

then, if InnoDB isn't active, MySQL will happily create a MyISAM table without even warning you. 然后,如果InnoDB处于非活动状态,MySQL将愉快地创建MyISAM表,甚至不会警告您。 Bye-bye data integrity! 再见数据完整性! :) :)

You can run SHOW ENGINES to see which engines are active. 您可以运行SHOW ENGINES来查看哪些引擎处于活动状态。 See this MySQL bug report for more details. 有关更多详细信息,请参见此MySQL错误报告

  1. Check that you're using InnoDB engine for both tables. 检查两个表是否都使用InnoDB引擎。

  2. Check that both fields are of the same type and that they are indexed. 检查两个字段是否具有相同的类型并对其进行索引。

From http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html : http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. 如果重新创建已删除的表,则该表必须具有符合引用该表的外键约束的定义。 It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. 如前所述,它必须具有正确的列名和类型,并且必须在引用的键上具有索引。 If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message. 如果不满足这些条件,MySQL将返回错误号1005,并在错误消息中引用错误150。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM