CREATE TABLE `recipe` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ethnicityID` int(25) NOT NULL,
`recipename` varchar(225) COLLATE utf8_unicode_ci NOT NULL,
`recipedescription` text COLLATE utf8_unicode_ci NOT NULL,
`recipeprocedure` text COLLATE utf8_unicode_ci NOT NULL,
`recipepremium` enum('YES','NO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
`recipeusercreator` enum('YES','NO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
`recipecreatorname` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`recipeapprovedby` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`recipeapproveddate` timestamp NULL DEFAULT NULL,
`recipestatus` enum('YES','NO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
`reciperequiresAdultSupervision` enum('YES','NO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
`category_id` int(12) DEFAULT NULL,
`facebookID` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`recipetype` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
`language` varchar(225) COLLATE utf8_unicode_ci NOT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
KEY `fbID` (`facebookID`),
KEY `foreignkey_PerCategory` (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
这是我的MySQL表的表格语法。
当我尝试运行此查询时
DELETE FROM `recipe` WHERE `ID` IN ('12','13','14','15','16','17','18');
我收到以下错误
Cannot delete or update a parent row: a foreign key constraint fails (`desifoodapp`.`recipe_image`, CONSTRAINT `recipe_image_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipe` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE)
我想知道如何更正我的表格,以便删除配方时,也会从recipe_image表中删除它的图像,但不会删除配方所属的类别。它没有受到影响。
下面的recipe_image表
CREATE TABLE `recipe_image` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`recipeimage` varchar(225) DEFAULT NULL,
`recipe_id` int(12) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `recipe_id` (`recipe_id`),
CONSTRAINT `recipe_image_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipe` (`ID`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
下面的类别表
CREATE TABLE `category` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`categoryname` varchar(225) NOT NULL,
`categoryimage` varchar(225) NOT NULL,
`facebookID` int(25) NOT NULL,
`added_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
答案 0 :(得分:2)
也可以将其删除CASCADE并尝试
假设你有食谱表作为主,其他表只是喜欢。 当您进行外键约束时,只需将它们设置为CASCADE Delete和CASCADE update 因此,无论何时在主表中进行更改,都将更新所有其他表。或者如果您删除主表中的一条记录,并检查所有外键链接并在所有客户记录中删除它们
在其他表定义中设置这些行
ON DELETE CASCADE ON UPDATE CASCADE
例如:recipre_image
CONSTRAINT `recipe_image_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipe` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
答案 1 :(得分:0)
外键约束阻止您从表中删除内容。您必须进行级联删除或类似的操作:
SQL Server: Deleting Rows with Foreign Key Constraints: Can Transactions override the constraints?
答案 2 :(得分:0)
当您要删除的表中的any field
在另一个表中引用为foreign key
并且引用的表中有任何条目时,您无法进行删除操作。
相反,您可以使用cascade
删除操作删除表和字段引用表中的所有记录。或者您可以在要删除的表格上使用rule
有关级联删除操作,请参阅以下链接: Cascade Delete