Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the Postgres foreign key 'on update' and 'on delete' options work?

Tags:

Can anyone provide a clear explanation / example of what these functions do, and when it's appropriate to use them?

like image 321
meleyal Avatar asked Oct 22 '08 14:10

meleyal


People also ask

Can foreign keys handle deletes and updates?

Foreign keys cannot handle deletes and updates. Explanation: A foreign key is the one which declares that an index in one table is related to that in another and place constraints. It is useful for handling deletes and updates along with row entries.

How does on delete cascade work PostgreSQL?

ON DELETE CASCADE option is to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behaviour of the database server prevents you from deleting data in a table if other tables reference it.

How does Postgres foreign key work?

The foreign key constraint helps maintain the referential integrity of data between the child and parent tables. A foreign key constraint indicates that values in a column or a group of columns in the child table equal the values in a column or a group of columns of the parent table.


1 Answers

Straight from the manual...

We know that the foreign keys disallow creation of orders that do not relate to any products. But what if a product is removed after an order is created that references it? SQL allows you to handle that as well. Intuitively, we have a few options:

Disallow deleting a referenced product

Delete the orders as well

Something else?

CREATE TABLE order_items (  product_no integer REFERENCES products ON DELETE RESTRICT,  order_id integer REFERENCES orders ON DELETE CASCADE,  quantity integer,  PRIMARY KEY (product_no, order_id) ); 

Restricting and cascading deletes are the two most common options. RESTRICT prevents deletion of a referenced row. NO ACTION means that if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything. (The essential difference between these two choices is that NO ACTION allows the check to be deferred until later in the transaction, whereas RESTRICT does not.) CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well. There are two other options: SET NULL and SET DEFAULT. These cause the referencing columns to be set to nulls or default values, respectively, when the referenced row is deleted. Note that these do not excuse you from observing any constraints. For example, if an action specifies SET DEFAULT but the default value would not satisfy the foreign key, the operation will fail.

Analogous to ON DELETE there is also ON UPDATE which is invoked when a referenced column is changed (updated). The possible actions are the same.

edit: You might want to take a look at this related question: When/Why to use Cascading in SQL Server?. The concepts behind the question/answers are the same.

like image 190
matt b Avatar answered Sep 19 '22 16:09

matt b