第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

MySQL ON DELETE CASCADE級聯(lián)刪除多張表的數(shù)據(jù)

標(biāo)簽:
MySQL

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

In the previous tutorial, you learned how to delete data from multiple related tables by 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.

We have two tables named buildings and rooms. Each building has one or more rooms. However, each room belongs to only one building. A room would not exist without a building.

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

MySQL On Delete Cascade example

When we delete a record from the buildings table, we want the records in the rooms table, which associates with the deleted building record to be removed e.g., when we delete a record with building no. 2 in the buildings table as the following query:

DELETE FROM buildings WHERE building_no = 2;

We want the records in the rooms table associated with the building number 2 to be removed as well.

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

Step 1. Create the buildings table:

CREATE TABLE buildings (   building_no int(11) NOT NULL AUTO_INCREMENT,   building_name varchar(255) NOT NULL,   address varchar(355) NOT NULL,   PRIMARY KEY (building_no) ) ENGINE=InnoDB;

Step 2. Create the rooms table:

CREATE TABLE rooms (   room_no int(11) NOT NULL AUTO_INCREMENT,   room_name varchar(255) NOT NULL,   building_no int(11) NOT NULL,   PRIMARY KEY (room_no),   KEY building_no (building_no),   CONSTRAINT rooms_ibfk_1    FOREIGN KEY (building_no)    REFERENCES buildings (building_no)    ON DELETE CASCADE ) ENGINE=InnoDB;

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

Step 3. Insert data into the buildings table:

INSERT INTO buildings(building_name,address) VALUES('ACME Headquaters','3950 North 1st Street CA 95134'), ('ACME Sales','5000 North 1st Street CA 95134')

Step 4. Query data from the buildings table:

SELECT * FROM buildings;

buildings table

We have two records in the buildings table.

Step 5. Insert data into the rooms table:

INSERT INTO rooms(room_name,building_no) VALUES('Amazon',1), ('War Room',1), ('Office of CEO',1), ('Marketing',2), ('Showroom',2)

Step 6. Query data from the rooms table:

SELECT * FROM rooms

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:

DELETE FROM buildings WHERE building_no = 2

Step 8. Query data from rooms table:

SELECT * FROM rooms

rooms table after delete

As you see, all the records that refer to building_no 2 were deleted.

Notice that ON DELETE CASCADE works only with tables whose 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 affect by the MySQL ON DELETE CASCADE referential action when you delete data from the parent table. You can query this data from the referential_constraints in the information_schema database as follows:

USE information_schema; SELECT table_name FROM referential_constraints WHERE constraint_schema = 'database_name' AND       referenced_table_name = 'parent_table' AND       delete_rule = 'CASCADE'

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

USE information_schema; SELECT table_name FROM referential_constraints WHERE constraint_schema = 'classicmodels' AND       referenced_table_name = 'buildings' AND       delete_rule = 'CASCADE'

Find tables ON DELETE CASCADE

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.

原文链接:http://outofmemory.cn/mysql/mysql-on-delete-cascade

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消