Open In App

When to Use “ON UPDATE CASCADE” in PostgreSQL?

In PostgreSQL, the ON UPDATE CASCADE clause in a foreign key constraint allows for a cascading update behavior between linked tables. This feature automatically updates all matching values in the referencing columns of child tables when a value is changed in the referenced column of the parent table.

In this article, We will explore about ON UPDATE CASCADE in maintaining referential integrity and ensuring consistency in relational databases.



What is ON UPDATE CASCADE in PostgreSQL?

Let’s consider several situations in which ON UPDATE CASCADE may be useful:

1. Update Primary Key

Example:



Before updating the changes

--Create Table--
CREATE TABLE parent (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);

CREATE TABLE child (
id SERIAL PRIMARY KEY,
parent_id INT REFERENCES parent(id) ON UPDATE CASCADE,
child_name VARCHAR(255)
);

-- Insert values--
INSERT INTO parent (name) VALUES ('Parent 1');
INSERT INTO child (parent_id, child_name) VALUES (1, 'Child 1');

-- Check child table--
SELECT * FROM child;

Output:

Before updating the changes

After Updating the changes

-- Update parent primary key--
UPDATE parent SET id = 100 WHERE id = 1;

--Check child table--
SELECT *FROM child;

Output:

After Updating the changes

Explanation: If we update the id of the parent table, the parent_id in the child table will be automatically updated to maintain the referential integrity.

2. By Upholding Relationships

Making sure that the rows in the child table continue to be associated to the appropriate rows in the parent table.

Example:

-- Create parent table--

CREATE TABLE parent (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

-- Create child table with foreign key referencing parent table--

CREATE TABLE child (
id SERIAL PRIMARY KEY,
parent_id INT REFERENCES parent(id) ON UPDATE CASCADE,
child_name VARCHAR(255) NOT NULL
);

-- Insert values--

INSERT INTO parent (name) VALUES ('Parent 1');
INSERT INTO child (parent_id, child_name) VALUES (1, 'Child 1');
INSERT INTO child (parent_id, child_name) VALUES (1, 'Child 2');

-- Check the child table--
SELECT * FROM child;

Output:

Before updating the changes

After Updating Parent’s Table

-- Update the parent table's primary key--

UPDATE parent SET id = 100 WHERE id = 1;

-- Check the child table--

SELECT * FROM child;

Output:

After Updating Parent’s Table

Explanation: In this example, both rows in the child table that were associated to the parent table’s row are updated to reflect the new id when the parent table’s id is changed from 1 to 100.

3. Update Foreign Key

Suppose We want to update the corresponding foreign key values in child tables when a foreign key value in the parent table is updated. This comes in handy when there’s a requirement to dynamically adjust the relationship between tables.

Example:

--Create Parent table--

CREATE TABLE parent (
id SERIAL PRIMARY KEY,
name VARCHAR(255)
);

--Create Child table--

CREATE TABLE child (
id SERIAL PRIMARY KEY,
parent_id INT REFERENCES parent(id) ON UPDATE CASCADE,
child_name VARCHAR(255)
);

-- Insert values--

INSERT INTO parent (name) VALUES ('Parent 1');
INSERT INTO child (parent_id, child_name) VALUES (1, 'Child 1');

-- Check child table--

SELECT * FROM child;

Output:

Before updating

After Updating Changes

-- Update parent foreign key--

UPDATE parent SET id = 100 WHERE id = 1;

-- Check child table--

SELECT * FROM child;

Output:

After Updating Changes

Explanation: Referential integrity will be preserved by automatically updating the parent_id in the child_table from 1 to 100 following the update.

Conclusion

Overall, The ON UPDATE CASCADE feature in PostgreSQL is a powerful tool for managing related data in relational databases. By automatically updating referencing columns in child tables when changes occur in the parent table, ON UPDATE CASCADE helps maintain referential integrity and data consistency.


Article Tags :