PostgreSQL
NO ACTION
(default): If any referencing rows still exist when the constraint is checked, an error is raisedRESTRICT
: Prevents deletion of a referenced row. The essential difference between these two choices is thatNO ACTION
allows the check to be deferred until later in the transaction, whereasRESTRICT
does not.CASCADE
: When a referenced row is deleted, row(s) referencing it should be automatically deleted as well.SET NULL
: Causes the referencing columns to be set toNULL
when the referenced row is deleted.SET DEFAULT
: Causes the referencing columns to be set to their default values when the referenced row is deleted. Note that these do not excuse you from observing any constraints. For example, if an action specifiesSET DEFAULT
but the default value would not satisfy the foreign key, the operation will fail.
链接:https://www.postgresql.org/docs/13/ddl-constraints.html#DDL-CONSTRAINTS-FK
MySQL
-
CASCADE
: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. BothON DELETE CASCADE
andON UPDATE CASCADE
are supported. Between two tables, do not define severalON UPDATE CASCADE
clauses that act on the same column in the parent table or in the child table.If a
FOREIGN KEY
clause is defined on both tables in a foreign key relationship, making both tables a parent and child, anON UPDATE CASCADE
orON DELETE CASCADE
subclause defined for oneFOREIGN KEY
clause must be defined for the other in order for cascading operations to succeed. If anON UPDATE CASCADE
orON DELETE CASCADE
subclause is only defined for oneFOREIGN KEY
clause, cascading operations fail with an error.Note
Cascaded foreign key actions do not activate triggers.
-
SET NULL
: Delete or update the row from the parent table and set the foreign key column or columns in the child table toNULL
. BothON DELETE SET NULL
andON UPDATE SET NULL
clauses are supported.If you specify a
SET NULL
action, make sure that you have not declared the columns in the child table asNOT NULL
. -
RESTRICT
: Rejects the delete or update operation for the parent table. SpecifyingRESTRICT
(orNO ACTION
) is the same as omitting theON DELETE
orON UPDATE
clause. -
NO ACTION
: A keyword from standard SQL. In MySQL, equivalent toRESTRICT
. The MySQL Server rejects the delete or update operation for the parent table if there is a related foreign key value in the referenced table. Some database systems have deferred checks, andNO ACTION
is a deferred check. In MySQL, foreign key constraints are checked immediately, soNO ACTION
is the same asRESTRICT
. -
SET DEFAULT
: This action is recognized by the MySQL parser, but bothInnoDB
andNDB
reject table definitions containingON DELETE SET DEFAULT
orON UPDATE SET DEFAULT
clauses.