cascading operations for MySQL

Source: Internet
Author: User

1. CREATE Table A

CREATE Table A (

Name char (not NULL),

ID char (NOT NULL primary key);

2. CREATE TABLE B

CREATE TABLE B (

B_name char (not NULL),

b_id char (not NULL),

Constraint foreign KEY (b_id) references a (ID) on DELETE cascade);

3. After the table is built, insert the data

Insert into a values ("111", "1");

Insert into a values ("222", "2");

Insert the following data into B values ("1", "1") in table B Display insert succeeded.

Insert the following data into B values ("1", "3") in table B Show Insert failed. This is because the foreign key is set and the ID in table A is not (id== "3") data.

Execute delete from a where id= "1"; The data in table B will also be deleted. The cascading deletion is applied here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.