我有一个MySQL表,其定义如下:
CREATE TABLE `guestbook` ( `Id` int(10) unsigned NOT NULL, `ThreadId` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`), KEY `ThreadId` (`ThreadId`), CONSTRAINT `guestbook_ibfk_1` FOREIGN KEY (`ThreadId`) REFERENCES `guestbook` (`Id`) ) ENGINE=InnoDB;
目前表格中只有1列:
mysql> select * from guestbook; +-----+----------+ | Id | ThreadId | +-----+----------+ | 211 | 211 | +-----+----------+
问题是没有删除约束就无法删除该行的方法。
mysql> delete from guestBook; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`polaris`.`guestbook`, CONSTRAINT `guestbook_ibfk_1` FOREIGN KEY (`ThreadId`) REFERENCES `guestbook` (`Id`))
由于ThreadId列的定义不为null,因此也无法将ThreadId临时设置为其他值以删除该行。有没有一种方法可以删除行而不更改表的定义或删除整个表?
您可以使用以下查询临时禁用外键约束:
SET foreign_key_checks = 0;