Open In App

When to Use ON UPDATE CASCADE in SQLite

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

“ON UPDATE CASCADE” is an important feature in SQLite that simplifies data management and ensures data integrity by automatically propagating changes from a parent table to its related child tables. This feature plays a crucial role in maintaining consistency and reducing the need for manual updates in SQLite databases.

In this article, We will learn about When to use “ON UPDATE CASCADE” in SQLite by understanding with the help of examples and so on.

When to use “ON UPDATE CASCADE” in SQLite?

  • ON UPDATE CASCADE” is an SQLite feature.
  • It helps developers determine the actions to take when a child row is changed due to an update in a referenced row in a parent table.
  • This feature automatically spreads changes in the parent-referenced column to related child tables using the specified action.
  • This feature simplifies data management by automatically updating related records and maintaining data integrity.
  • It reduces the need for manual updates and helps prevent inconsistencies between related tables.
  • Developers can use “ON UPDATE CASCADE” to streamline database operations and improve the efficiency of data maintenance tasks.

Syntax:

The syntax for using “ON UPDATE CASCADE” in SQLite is as follows:

CREATE TABLE child_table (
child_column1 datatype,
child_column2 datatype,
FOREIGN KEY (child_column1) REFERENCES parent_table(parent_column)
ON UPDATE CASCADE
);

Explanation:

  • child_table: The name of the child table.
  • child_column1: The column in the child table that references the parent table.
  • parent_table: The name of the parent table.
  • parent_column: The column in the parent table that is being referenced.
  • ON UPDATE CASCADE: Specifies the action to be taken on related rows in the child table when the referenced column in the parent table is updated.

Example of When to use “ON UPDATE CASCADE” in SQLite

Consider a scenario having two tables: employees and departments. The employees table contains a foreign key constraint referencing the departments table on the department_id column. Let’s ensure that when the department_id of a department changes, the corresponding department_id of all associated employees is updated accordingly.

Step 1: Create Tables

CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
department_name TEXT
);

CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT,
department_id INTEGER,
FOREIGN KEY (department_id) REFERENCES departments(department_id) ON UPDATE CASCADE
);

Step 2: Insert Initial Data

-- Insert departments
INSERT INTO departments (department_id, department_name) VALUES (1, 'IT');
INSERT INTO departments (department_id, department_name) VALUES (2, 'HR');

-- Insert employees
INSERT INTO employees (employee_id, employee_name, department_id) VALUES (101, 'John Doe', 1);
INSERT INTO employees (employee_id, employee_name, department_id) VALUES (102, 'Jane Smith', 2);

Step 3: View Initial Data

Let’s take a look at the initial data in both tables:

Departments Table

depart

Departments table

Employees Table

employ

Employees table

Step 4: Update Department Information

Now, let’s update the name of the IT department from “IT” to “Information Technology“. Since we have enabled “ON UPDATE CASCADE”, this update will automatically reflect in the employees table.

UPDATE departments SET department_name = 'Information Technology' WHERE department_id = 1;

Step 5: View Updated Data

After the update, let’s examine the data in both tables again:

View-Updated-Data

updated departments table

Reflect Changes in the employees table as shown in below image:

Reflect-Changes

updated employees table

We can see that when the department name was changed it updated correctly in the departments table. Additionally, the employee ID of Jane Smith, who belongs to the IT department was automatically updated. This demonstrates how “ON UPDATE CASCADE” maintains consistency by updating related data across tables.

This illustration displays the actuality of “ON UPDATE CASCADE” clause in structuring SQLite databases, as it facilitates the solving of problems of data management, and monitoring data integrity safety standards.

Conclusion

Overall, “ON UPDATE CASCADE” is a feature in SQLite that makes the task of management of referential integrity and data integrity more efficient. The presence of cascaded update process within the related tables reduce the Dependence of the users of the database manager and minimizes the chance of database anomalies due to human error. However, it must be realized that the use of it should be done with great care ensuring that all factors that may arise by its use are to be kept in mind.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads