JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

MySQL Cascade Delete

Author:JIYIK Last Updated:2025/04/22 Views:

In MySQL parlance, a constraint is a set of rules enforced on a table to prevent certain anomalies.

These exceptions will result in error records in the table. Constraints can be of various types based on the behavior of the failure.

They are referential integrity, entity constraints, and semantic constraints. These have meaning and usage based on the type of failure and how to avoid inconsistent behavior.

Let us briefly understand the types of constraints that exist.

  1. Referential integrity is a constraint applied to other tables to enforce restrictions on data. The most commonly used constraint is the foreign key constraint.

    If there is modification in the parent table, the child table should remain consistent with the parent table. In case of deletion of records, the reference in the child record should not remain dangling and is a referential integrity constraint.

    example:

    create table report_card (stu_roll_number int, stu_marks int, stu_course varchar2(30) references student);
    

    In the above example, the transcript shows the department or course of the student in the parent table student. If referential integrity constraints are applied, then operations in one table will affect the work in the child table.

Under referential integrity constraints on delete cascadeis a referential integrity constraint which automatically deletes the child records if the parent record is deleted. It is a response to a referential action taken from a foreign key.

example:

CREATE TABLE Student (stu_id int(10) NOT NULL, name varchar(10) NOT NULL, birthdate date NOT NULL, PRIMARY KEY (stu_id));

The above query creates a table student with attributes or columns as student id, inttype and primary key as entity constraints for the columns. Other value attributes include name and birthday which have data types of varcharand date.

CREATE TABLE Department (dept_id int(20) PRIMARY KEY NOT NULL,stu_id int(10) NOT NULL, dept_name varchar(20) NOT NULL, FOREIGN KEY (stu_id) REFERENCES Student (stu_id) ON DELETE CASCADE);

In the above query, a department table is formed with the attributes id, department name and student id. Also, student id will act as a reference or foreign key in the parent table.

Here on delete cascadethe constraint says that when any deletion occurs to the student id, its entry from the department table will also be deleted. So, after the deletion there will be no entry corresponding to the student of that department.

Let's insert records into the table to see how the operation works here.

First, Studentinsert records into the table.

INSERT INTO Student (stu_id, name, birthdate) VALUES  
(101, 'John', '1995-11-12'),  
(102, 'Jian', '1983-07-19'),  
(103, 'Jackey', '1965-10-23');

The above query will Studentinsert three records into the table.

部门Insert a record into the table .

INSERT INTO Department (dept_id, stu_id, dept_name) VALUES   
(301, 101, 'Physics'),  
(302, 101, 'Science'),  
(303, 102,  'Maths');

The above query will insert three departments for each student ID in the table departments.

Now let us delete a record from the parent table.

DELETE FROM Student WHERE stu_id = 102; 

The end result will be to delete the third record of the table department because the reference to its parent table is deleted, leaving the third record dangling.

Therefore, it collapses the third record using on delete cascadethe attribute. This attribute is a referential integrity constraint attribute that deletes records in the child table when the parent table is affected.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:192 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:195 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Compare only MySQL timestamp dates to date parameters

Publish Date:2025/04/22 Views:64 Category:MySQL

In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi

Calculating Percentages in MySQL

Publish Date:2025/04/22 Views:66 Category:MySQL

We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re

Selecting multiple values using WHERE in MySQL

Publish Date:2025/04/22 Views:185 Category:MySQL

This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi

Changing the connection timeout in MySQL

Publish Date:2025/04/22 Views:59 Category:MySQL

We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti

MySQL fix Data Is Truncated for a Column error

Publish Date:2025/04/22 Views:101 Category:MySQL

This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca

MySQL Error Server PID File Could Not Be Found Solution

Publish Date:2025/04/22 Views:192 Category:MySQL

In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My

Get the last inserted ID using PHP MySQLi function

Publish Date:2025/04/22 Views:99 Category:MySQL

This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial