Foreign Key Actions
With the sub-clauses ON UPDATE
and ON DELETE
it is possible to specify an action to be taken on the affected foreign key column(s) when referenced values in the master table are changed:
NO ACTION
-
(the default) — Nothing is done
CASCADE
-
The change in the master table is propagated to the corresponding row(s) in the child table.If a key value changes, the corresponding key in the child records changes to the new value;if the master row is deleted, the child records are deleted.
SET DEFAULT
-
The foreign key columns in the affected rows will be set to their default values as they were when the foreign key constraint was defined.
SET NULL
-
The foreign key columns in the affected rows will be set to
NULL
.
The specified action, or the default NO ACTION
, could cause a foreign key column to become invalid.For example, it could get a value that is not present in the master table.Such condition will cause the operation on the master table to fail with an error message.
...
CONSTRAINT FK_ORDERS_CUST
FOREIGN KEY (CUSTOMER) REFERENCES CUSTOMERS (ID)
ON UPDATE CASCADE ON DELETE SET NULL