Skip to content

CASCADE

CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
ON DELETE CASCADE
);
DELETE FROM customers WHERE customer_id = 1;

The DELETE statement does not provide a visible output when executed. It does its job in the background.

In the given code, we created a table orders with a foreign key customer_id that references the customer_id in the customers table. The ON DELETE CASCADE clause specifies that when a customer record in the customers table is deleted, then all corresponding records in the orders table for that customer should also be deleted automatically. In the last command, we deleted a customer with customer_id = 1 from the customers table, which also deleted all orders related to this customer from the orders table thanks to the CASCADE option.