Bobcares

Postgres Delete Cascade | Guide

by | Jun 26, 2024

Postgres DELETE CASCADE Feature maintains data consistency and referential integrity. In this article, we’ll see more about using this feature. Bobcares, as a part of our PostgreSQL Support offers solutions to every query that comes our way.

Overview
  1. More on Postgres DELETE CASCADE Feature
  2. Steps to Use the Feature
  3. Benefits of the Feature

More on Postgres DELETE CASCADE Feature

The DELETE CASCADE feature is a powerful database feature in RDBMS to ensure integrity of references between tables. This rule on a foreign key constraint ensures that when a row in the parent table (the table with the primary key) is removed, all linked rows in the child table (the table with the foreign key) are also deleted. This is especially useful for tables that links through foreign key constraints.

Steps to Use the Feature

1. First, create two tables: authors (the parent table) and books (the child table). The books table has a foreign key that references the authors table with the ON DELETE CASCADE option.

CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE books (
book_id SERIAL PRIMARY KEY,
title VARCHAR(100),
author_id INT,
FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE
);

2. Next, insert some sample data into the authors and books tables.

INSERT INTO authors (name) VALUES ('Author One'), ('Author Two');
INSERT INTO books (title, author_id) VALUES ('Book One', 1), ('Book Two', 1), ('Book Three', 2);

3. Now, delete an author from the authors table. Because we specify DELETE CASCADE, all books by that author will no longer available.

DELETE FROM authors WHERE author_id = 1;

4. Check the contents of the books table to ensure that the related records have been deleted.

SELECT * FROM books;

The output should show only the book(s) by the remaining author(s):

book_id | title | author_id
---------+--------------+-----------
3 | Book Three | 2
(1 row)

In this example, deleting the author with author_id = 1 automatically deleted the books with author_id = 1, showing the DELETE CASCADE feature.

Benefits of the Feature

The DELETE CASCADE feature in relational databases offers several benefits that enhance data management and integrity:

1. It automatically deletes child records after deleting parent record, preventing orphaned records.

2. Reduces the need for extra code or manual deletion of related records, making tasks easier.

3. Applies consistent deletion rules across related tables, ensuring all related data is deleted together.

4. Shifts the responsibility of cascading deletes from the application to the database, simplifying application logic and reducing errors.

5. Prevents leftover related data after a parent record is deleted, avoiding data anomalies.

6. Makes it easy to remove all related data, useful for purging or archiving.

7. Handles complex delete operations with a single command, reducing database round-trips.

8. Ensures cascading deletions happen within a single transaction.

[Need to know more? Get in touch with us if you have any further inquiries.]

Conclusion

PostgreSQL will automatically remove associated records from child table when a parent table is deleted using the DELETE CASCADE. The article explains about the feature with an example from our Tech team.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.