如何在 mysql 中使用删除级联?

How do I use on delete cascade in mysql?(如何在 mysql 中使用删除级联?)
本文介绍了如何在 mysql 中使用删除级联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个组件数据库.每个组件都属于特定类型.这意味着组件和类型之间存在多对一的关系.当我删除一个类型时,我想删除所有具有该类型外键的组件.但是如果我没记错的话,级联删除会在删除组件时删除类型.有什么办法可以做到我所描述的吗?

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
)

请记住,您需要使用 InnoDB 存储引擎:默认的 MyISAM 存储引擎不支持外键.

Just remember that you need to use the InnoDB storage engine: the default MyISAM storage engine doesn't support foreign keys.

这篇关于如何在 mysql 中使用删除级联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)