Open In App

Difference Between ON DELETE CASCADE and ON DELETE SET NULL in DBMS

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The phrases “ON DELETE CASCADE” and “ON DELETE SET NULL” refer to options that can be specified when defining foreign key constraints in relational databases. These options determine the behavior of the database system when a record in the parent table (referenced table) is deleted. Let’s explore each option:

ON DELETE CASCADE

When you specify an “ON DELETE CASCADE” for a foreign key constraint, it means that if a record in the parent table (referenced table) is deleted then all related records in the child table (referencing table) will be automatically deleted. This ensures the referential integrity by removing dependent records when the referenced record is removed.

Example SQL Syntax

CREATE TBALE parent_table_p (
id INT PRIMARY KEY
)
CREATE_TABLE child_table_c(
id INT PRIMARY KEY,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE CASCADE
)

ON DELETE SET NULL

The phrase “ON DELETE SET NULL” is not a standard option in most relational databases. If you made the child table (referencing table) “ON DELETE SET NULL” this means if a record in the parent table (referenced table) is deleted then the corresponding entry or values in the child table (referencing table) will be set to NULL. This is another way to maintain reference integrity, allowing the child records to exist but with the NULL reference if the parent record is deleted.

Example SQL Syntax

CREATE TBALE parent_table_p (
id INT PRIMARY KEY
)
CREATE_TABLE child_table_c(
id INT PRIMARY KEY,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE SET NULL
)

Difference Between ON DELETE CASCADE and ON DELETE SET NULL in DBMS

BEHAVIOR

ON DELETE CASCADE

ON DELETE SET NULL

Effect on child records

Child records are automatically deleted.

Foreign key values in child records are set to NULL

Referential integrity

Ensures referential integrity by removing dependent records.

Ensures referential integrity by maintaining child records with NULL references.

Query Complexity

Simplifies query, as there is no need to separately handle child deletions.

Requires additional consideration when querying to handle NULL foreign key values.

Database Size Impact

May lead to a more significant reduction in database size due to cascading deletions.

Retains child records, potentially leading to a smaller reduction in database size.

Syntax

FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE CASCADE

FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE CASCADE

Conclusion

In conclusion, the choice between ‘ON DELETE CASCADE’ and ‘ON DELETE SET NULL’ in database design depends on the specific requirements of the application and the desired behavior when dealing with parent and child records. Each option has distinct implications for data integrity, query complexity, and database size.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads