![]() |
VOOZH | about |
In MySQL, the ON UPDATE CASCADE option in foreign key constraints indicates that anytime an update is made to a row in the parent table, matching rows in child tables are correspondingly updated.
This feature providesassurance for referential integrity and consistency of related data in a related set of tables without having to manage it manually. Knowing when and how to use ON UPDATE CASCADE is important in managing table relationships effectively and facilitating data maintenance.
Syntax:
The syntax for defining a foreign key with ON UPDATE CASCADE is:
CREATE TABLE child_table (
child_id INT,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table(parent_id)
ON UPDATE CASCADE
);
Consider a database with two tables: departments and employees, where the latter is associated with the former by way of a foreign key reference.
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
ON UPDATE CASCADE
);
INSERT INTO departments (department_id, department_name) VALUES (1, 'HR'), (2, 'IT');
INSERT INTO employees (employee_id, employee_name, department_id) VALUES (101, 'Alice', 1), (102, 'Bob', 2);
Outputs of departments Table Before Update:
department_id | department_name |
|---|---|
1 | HR |
2 | IT |
Outputs of employees Table Before Update:
employee_id | employee_name | department_id |
|---|---|---|
101 | Alice | 1 |
102 | Bob | 2 |
If the department_id in the departments table is updated:
UPDATE departments SET department_id = 3 WHERE department_name = 'HR';After Update:
departments Table:
department_id | department_name |
|---|---|
3 | HR |
2 | IT |
employees Table:
employee_id | employee_name | department_id |
|---|---|---|
101 | Alice | 3 |
102 | Bob | 2 |
In MySQL, when one has a foreign key relationship and uses the ON DELETE CASCADE action, the affected table is the child table.
Example:
Consider the following data in the parent and child tables:
The result would be if ON DELETE CASCADE was defined in the orders table with the foreign key:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE
);
Customers get deleted from the customers table, the records found in the orders table, which are associated with the deleted customer_id, get automatically deleted in MySQL.
Determining the Affected Table:
Table affected: Deletion in the customers table affects the child table, which is the orders table.
Scenario:
What happens if we remove a customer from the customers table:
DELETE FROM customers WHERE customer_id = 1;In MySQL, ON UPDATE CASCADE is an element of a foreign key constraint that updates the foreign key in the child table whenever the primary key in the parent table has been changed.
In detail, the ON UPDATE CASCADE option seeks to maintain the very referential integrity between the two tables in case there is a change in the primary key in the parent table. This option prevents the situation in which an update of a primary key may be executed while the foreign keys of the child table remain pointed at a nonexisting row, hence breaking the relationship.
It means that when ON UPDATE CASCADE is applied and an update is made to a row in the parent table, all foreign key values in the child table that match the updated one will also change accordingly.
Example
Let us create these tables with the ON UPDATE CASCADE option.
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES departments(dept_id) ON UPDATE CASCADE
);
Suppose you have a record in the departments table:
INSERT INTO departments (dept_id, dept_name) VALUES (1, 'Sales');And several records in the employees table referencing the dept_id:
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (101, 'Alice', 1);
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (102, 'Bob', 1);
If you decide to update the dept_id in the departments table:
UPDATE departments SET dept_id = 2 WHERE dept_id = 1;With ON UPDATE CASCADE, MySQL will automatically update the dept_id in the employees table as well.
The employees table will now have:
Output:
emp_id | emp_name | dept_id |
|---|---|---|
101 | Alice | 2 |
102 | Bob | 2 |
The ON DELETE CASCADE OPTION on the other hand guarantees that when a row from the parent table is deleted then all the rows in the child table that relate to the deleted row in the parent table are automatically deleted too.
This feature is useful in scenarios whereby the existence of a record in the child table is contingent on the existence of a record in the parent table. For instance, if you had a customers table and an orders table, deleting the record for a customer would result in their orders all being deleted, too. The use of ON DELETE CASCADE ensures that no child records are 'orphaned'.
The ON UPDATE CASCADE in MySQL is very vital in the maintenance of the integrity of foreign key relationships within your data. It does this by changing the matching records of the child tables whenever a primary key is changed in the parent table. This helps to ease the management of your data and prevents integrity problems. Proper understanding and application of the concept of ON UPDATE CASCADE will help in making your database consistent and reliable, especially in large and complex relational schemas.