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;
這是否適用於MySQL的? –
是的,所有的sql引擎 – FaddishWorm