Delete actions: what happens to the rows left behind
Step 1 of 110%
Concept · step 1 of 11
What a delete does to the rows that point at it
A foreign key also guards the other side: deleting a customer who still has orders. What happens then is the key's delete action.
- The default,
NO ACTIONthe delete is refused while anything still points at the row. ON DELETE CASCADEthe rows that point at it are deleted along with it.ON DELETE SET NULLthey stay, and their reference is emptied.
customer_id BIGINT NOT NULL REFERENCES customers (id) ON DELETE CASCADEThere is no right answer for every key, only for each relationship: whether the child can outlive its parent, and whether the business can afford to lose it.
PostgreSQL docs: Foreign Keys ↗Starting a real database in your browser
It is called PostgreSQL. The first time it downloads about 5 MB; after that it is kept, and starts faster.
PostgreSQL running in your browser