Thoughts on PostgreSQL's UPDATE and DELETE foreign key properties

The default property when a foreign key is created is MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION. Here you need to think deeply about the NO ACTION part.

First of all, there are three kinds of parameters in this part:

1) NO ACTION - deletes/updates are not allowed, but errors are reported late in the transaction.

2) RESTRICT - delete/update is not allowed, and an error is reported directly.

3) CASCADE - allows cascading deletes/updates.

 

Here, CASCADE is best understood, which means cascading. For example, when the data of the parent table is deleted, the associated data of the child table also exists, and it will be deleted in cascade.

Both NO ACTION and RESTRICT will check the operation. If it does not meet the constraints, it will report ERROR and exit, and the data will remain unchanged. The only difference is that NO ACTION can set constraints to take effect in a delayed transaction, while RESTRICT does not allow it. Personal understanding should be based on the consideration of transaction performance. Assuming that in a large transaction, RESTRICT is defined, then every statement is executed to go back to verify. If there is no associated deletion error in the entire transaction, it will inevitably lead to a waste of time. ; However, if NO ACTION is used at this time, the entire transaction process is smoothly processed. At the final commit, if there is a cascading constraint, it will report an error and exit. If there is no long time, it will be the most time-saving to submit the entire transaction directly. In addition, the transaction is atomic, and it is also said in the past to do this processing for a transaction.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324377634&siteId=291194637