Oct 16, 2024
ON DELETE
clause in SQL.ON DELETE SET NULL
and ON DELETE CASCADE
.NULL
.transactions
with a foreign key customer_id
.customers
table, the corresponding customer_id
in transactions
can be set to NULL
.customers
table.ON DELETE SET NULL
in the foreign key clause.ALTER TABLE transactions DROP FOREIGN KEY FK_customer_id
.ALTER TABLE transactions ADD CONSTRAINT FK_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE SET NULL
.customer_id
in transactions
becomes NULL
.transactions
and customers
tables.transactions
is removed.customers
table.ALTER TABLE transactions DROP FOREIGN KEY FK_customer_id
.ALTER TABLE transactions ADD CONSTRAINT FK_customer_id FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE CASCADE
.transactions
table to reflect the change, then delete the customer.transactions
linked to the deleted customer ID will be removed.ON DELETE
clause options:
SET NULL
: Sets the foreign key value to NULL
.CASCADE
: Deletes the entire row.SET NULL
and CASCADE
based on application requirements.