zoukankan      html  css  js  c++  java
  • A2-02-31.DML- MySQL ON DELETE CASCADE: Deleting Data From Multiple Related Tables

    转载自:http://www.mysqltutorial.org/mysql-on-delete-cascade/

    MySQL ON DELETE CASCADE: Deleting Data From Multiple Related Tables

     

    Summary: in this tutorial, you will learn how to use MySQL ON DELETE CASCADE referential action for a foreign key to delete data from multiple related tables.

    In the previous tutorial, you learned how to delete data from multiple related tables using a single DELETE statement. However, MySQL provides a more effective way called ON DELETE CASCADE referential action for a foreign key that allows you to delete data from child tables automatically when you delete the data from the parent table.

    MySQL ON DELETE CASCADE example

    Let’s take a look at an example of using MySQL ON DELETE CASCADE .

    Suppose we have two tables:buildings and rooms . In this database model, each building has one or more rooms. However, each room belongs to one only one building. A room would not exist without a building.

    The relationship between the buildings and rooms tables is one-to-many (1:N) as illustrated in the following database diagram:

    MySQL ON DELETE CASCADE - tables demo

    When we delete a row from the buildings table, we  also want to delete the rows in the rooms  table that references to the rows in the buildings table. For example,  when we delete a row with building no. 2 in the buildings  table as the following query:

    We want the rows in the rooms table that refers to building number 2 will be also removed.

    The following are steps that demonstrate how MySQL ON DELETE CASCADE  referential action works.

    Step 1. Create the buildings table:

    Step 2. Create the rooms table:

    Notice that we add the ON DELETE CASCADE  clause at the end of the foreign key constraint definition.

    Step 3. Insert data into the buildings table:

    Step 4. Query data from the buildings table:

    MySQL ON DELETE CASCADE buildings table

    We have two rows in the buildings table.

    Step 5. Insert data into the rooms table:

    Step 6. Query data from the rooms table:

    MySQL ON DELETE CASCADE - rooms table

    We have 3 rooms that belong to building 1 and 2 rooms that belong to the building 2.

    Step 7. Delete the building with building no. 2:

    Step 8. Query data from rooms table:

    MySQL ON DELETE CASCADE - rooms table after delete

    As you can see, all the rows that reference to building_no 2 were deleted.

    Notice that ON DELETE CASCADE  works only with tables with the storage engines support foreign keys e.g., InnoDB. Some table types do not support foreign keys such as MyISAM so you should choose appropriate storage engines for the tables that you plan to use the MySQL ON DELETE CASCADE  referential action.

    Tips to find tables affected by MySQL ON DELETE CASCADE action

    Sometimes, it is useful to know which table is affected by the MySQL ON DELETE CASCADE  referential action when you delete data from a table. You can query this data from the referential_constraints in the information_schema  database as follows:

    For example, to find tables that associated with the buildings table with the CASCADE  deletion rule  in the classicmodels database, you use the following query:

    MySQL ON DELETE CASCADE tips

    In this tutorial, we have shown you step by step how to use the MySQL ON DELETE CASCADE  referential action for a foreign key to delete data automatically from the child tables when you delete data from the parent table.

  • 相关阅读:
    linux使用rm-rf删除之文件恢复
    性能测试常见瓶颈分析及调优方法
    性能压测QPS 和 TPS区别
    Linux查看系统性能
    Python中split()和split(" ")的区别
    Python-标准库之functools
    Python-内置函数_@propery、@classmethod、@staticmethod详解
    Python-类的内置方法中__repr__和__str__的区别
    Python-标准库之运算符替代函数Operator
    Python-标准库之迭代器-itertools
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9519596.html
Copyright © 2011-2022 走看看