我如何使用删除级联在MySQL? [英] How do I use on delete cascade in mysql?
本文介绍了我如何使用删除级联在MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
以下是组件表中的内容。
$ b $ $ $ $ $ $ $ $ $ $ $ $ $'$ $'$ $ $'$'$'$' (10)unsigned NOT NULL,
`moreInfo` VARCHAR(32),
- etc
PRIMARY KEY(`id`),
KEY`type`(`typeId` )
CONSTRAINT`myForeignKey` FOREIGN KEY(`typeId`)
REFERENCES`types`(`id`)ON DELETE CASCADE ON UPDATE CASCADE
)
请记住,您需要使用InnoDB存储引擎:默认的MyISAM存储引擎不支持外键。 b
I have a database of components. Each component is of a specific type. That means there is a many-to-one relationship between a component and a type. When I delete a type, I would like to delete all the components which has a foreign key of that type. But if I'm not mistaken, cascade delete will delete the type when the component is deleted. Is there any way to do what I described?
解决方案
Here's what you'd include in your components table.
CREATE TABLE `components` (
`id` int(10) unsigned NOT NULL auto_increment,
`typeId` int(10) unsigned NOT NULL,
`moreInfo` VARCHAR(32),
-- etc
PRIMARY KEY (`id`),
KEY `type` (`typeId`)
CONSTRAINT `myForeignKey` FOREIGN KEY (`typeId`)
REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
Just remember that you need to use the InnoDB storage engine: the default MyISAM storage engine doesn't support foreign keys.
这篇关于我如何使用删除级联在MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文