Difference Between ON DELETE CASCADE and ON DELETE SET NULL in DBMS
Last Updated :
12 Dec, 2024
ON DELETE CASCADE and ON DELETE SET NULL are two important options in SQL foreign key constraints that define how the database handles related records in a child table when a record in the parent table is deleted. These options are crucial for maintaining referential integrity and ensuring a consistent database structure. This article will explain these concepts in detail with examples, syntax, advantages, disadvantages, and key differences.
Difference Between ON DELETE CASCADE and ON DELETE SET NULL
The following table lists all the major differences between ON DELETE CASCADE and ON DELETE SET NULL:
BEHAVIOR | ON DELETE CASCADE | ON DELETE SET NULL |
---|
Effect on child records | Automatically deletes child records | 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 queries, as child records are deleted | Requires additional handling for NULL values. |
Impact on Database Size | Reduces size by deleting child records. | Retains child records, increasing 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 |
What is ON DELETE CASCADE?
The "ON DELETE CASCADE" for a foreign key constraint 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 of ON DELETE CASCADE
Step 1: Create the Parent Table
CREATE TABLE parent_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
Step 2: Create the Child Table with ON DELETE CASCADE
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT,
detail VARCHAR(100),
FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE CASCADE
);
Step 3: Insert Sample Data
-- Insert data into parent table
INSERT INTO parent_table (id, name) VALUES (1, 'Parent 1'), (2, 'Parent 2');
-- Insert data into child table
INSERT INTO child_table (id, parent_id, detail)
VALUES (1, 1, 'Child of Parent 1'), (2, 1, 'Another Child of Parent 1'), (3, 2, 'Child of Parent 2');
Step 4: View the Data
SELECT * FROM parent_table;
SELECT * FROM child_table;
Output for child_table
:
id | parent_id | detail |
---|
1 | 1 | Child of Parent 1 |
2 | 1 | Another Child of Parent 1 |
3 | 2 | Child of Parent 2 |
Step 5: Delete a Record from Parent Table
DELETE FROM parent_table WHERE id = 1;
Step 6: Check the Child Table
SELECT * FROM child_table;
Output
id | parent_id | detail |
---|
3 | 2 | Child of Parent 2 |
Explanation:
The rows in child_table
with parent_id
= 1
were automatically deleted because of the ON DELETE CASCADE constraint.
What is ON DELETE SET NULL?
The ON DELETE SET NULL option updates the foreign key column in the child table to NULL
when the corresponding parent record is deleted. This approach preserves the child record while removing its reference to the parent. 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 of ON DELETE SET NULL
Step 1: Create the Parent Table
CREATE TABLE parent_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
Step 2: Create the Child Table with ON DELETE SET NULL
CREATE TABLE child_table (
id INT PRIMARY KEY,
parent_id INT,
detail VARCHAR(100),
FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE SET NULL
);
Step 3: Insert Sample Data
-- Insert data into parent table
INSERT INTO parent_table (id, name) VALUES (1, 'Parent 1'), (2, 'Parent 2');
-- Insert data into child table
INSERT INTO child_table (id, parent_id, detail)
VALUES (1, 1, 'Child of Parent 1'), (2, 1, 'Another Child of Parent 1'), (3, 2, 'Child of Parent 2');
Step 4: Delete a Record from Parent Table
DELETE FROM parent_table WHERE id = 1;
Step 5: Check the Child Table
SELECT * FROM child_table;
Output:
id | parent_id | detail |
---|
1 | NULL | Child of Parent 1 |
2 | NULL | Another Child of Parent 1 |
3 | 2 | Child of Parent 2 |
Explanation:
The parent_id
for the affected rows in child_table
is set to NULL
, preserving the child records.
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. ON DELETE CASCADE is ideal for maintaining strict referential integrity, while ON DELETE SET NULL is suitable for preserving child records without parent references
Similar Reads
Difference Between DELETE and DROP in SQL In SQL, the DELETE and DROP commands are essential for managing data in a database, but they serve different purposes. While both are used to remove data, their functionality varies significantly. The DELETE command is designed to remove specific rows (tuples) or all rows from a table while preservi
4 min read
Difference between DELETE and TRUNCATE When managing large datasets in SQL, it's essential to understand the differences between various commands used for removing data. Two commonly used SQL commands for data removal are DELETE and TRUNCATE. While both serve the purpose of removing rows from a table, they have distinct features and use
5 min read
Difference between DELETE, DROP and TRUNCATE In SQL, understanding the DELETE, DROP, and TRUNCATE commands is important for efficient data management. While these commands are all used to remove data, they differ significantly in functionality, usage, and performance. Knowing when and how to use each command can improve the efficiency and inte
4 min read
Difference between Cursor and Trigger in DBMS A cursor can be referred to as a pointer to the context. The context area is a memory area that Oracle creates when processing the SQL statement. The cursor is thus responsible for holding the rows that the SQL statement has returned. Therefore the PL/SQL controls the context area with the help of t
4 min read
Difference between Lossless and Lossy Join Decomposition The process of breaking up a relation into smaller sub-relations is called Decomposition. Decomposition is required in DBMS to convert a relation into a specific normal form which further reduces redundancy, anomalies, and inconsistency in the relation. There are mainly two types of decompositions i
5 min read