# Cascade Delete

## Working Sample

Consider a scenario in which there is a database containing tables named "customer\_details" and "order\_details". The "order\_details" table is connected to the "customer\_details" table by means of a foreign key restriction. In the event that a user wishes to remove a customer entry from the "customer\_details" table, they may also want to eliminate all corresponding orders linked to that customer from the "order\_details" table in order to maintain the organization and cleanliness of the database. This is where the DELETE CASCADE functionality in Postgres proves to be highly advantageous.

To begin with, we will establish a table named "customer\_details" consisting of two columns: "cust\_id" and "cust\_name."

```plsql
CREATE TABLE customer_details (
cust_id INTEGER PRIMARY KEY,
cust_name TEXT NOT NULL
);
```

Generate an additional table titled "order\_details" containing two standard columns alongside a foreign key.

```plsql
CREATE TABLE order_details(
order_id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customer_details (cust_id) ON DELETE CASCADE,
order_date DATE
);
```

A new table called "order\_details" has been constructed, which includes a foreign key named "customer\_id" that has been configured to utilize the DELETE CASCADE functionality.

Let us insert records into the tables.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FS91SHUzCodohI0e9wxEF%2Fimage.png?alt=media&#x26;token=2aec6d76-95a9-4a00-b96c-dc25ce0e5969" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FnH7kHCqUzD7MjdfuEpVg%2Fimage.png?alt=media&#x26;token=9305368d-b675-4fcf-9b19-1d38a9a2bed5" alt="" width="375"><figcaption></figcaption></figure>

The "order\_details" will hold this data now.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FE1zXLBkFxyJjb8XRObnE%2Fimage.png?alt=media&#x26;token=6c71b850-6177-46df-98a5-2ddae0e1e6fe" alt="" width="256"><figcaption></figcaption></figure>

Due to the activation of the DELETE CASCADE setting during the foreign key creation process, the removal of a record from the "customer\_details" table will consequently result in the deletion of the associated record from the "order\_details" table as well.

```plsql
DELETE FROM customer_details 
WHERE cust_id = 1;
```

This deleted the customer from "customer\_details" table and also removed the corresponding orders from the "order\_details" table.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2F9wUkWm3XFynlKZvVDUGe%2Fimage.png?alt=media&#x26;token=142eb32f-5e41-4c3a-a6f3-f77d479ba401" alt="" width="171"><figcaption></figcaption></figure>

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FOM1dLulGhfT8D8uWQ0RF%2Fimage.png?alt=media&#x26;token=310f4a36-43b0-415f-8b7c-a411bae1f049" alt="" width="256"><figcaption></figcaption></figure>

## Types

There are five possible choices available for the deletion of foreign key constraints.

* [ ] **`CASCADE`**: The deletion of a row in the main table results in the deletion of all associated rows in the dependent tables.
* [ ] **`RESTRICT`**: Upon deletion of a row from the parent table, the delete process is prevented from proceeding if there exist any associated rows in the child tables.
* [ ] **`SET NULL`**: When a record is removed from the primary table, the foreign key columns in the related tables are updated to reflect NULL values.
* [ ] **`SET DEFAULT`**: When a row is removed from the primary table, the foreign key columns in the subordinate tables are updated to default values.
* [ ] **`NO ACTION`**: This feature is comparable to the RESTRICT option, but it includes the capability to be "deferred" until the conclusion of a transaction. It allows other cascading deletes to be executed first, with the delete constraint only triggering an error if there is referenced data remaining at the conclusion of the transaction.
