Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions content/sql/create-table-foreign-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ github: https://github.com/zernonia

Add foreign key to product_id using `references`.

`on delete cascade` When a referenced foreign key is deleted or updated, all rows referencing that key are deleted or updated, respectively.
`on delete cascade` When a referenced foreign key is deleted, all rows referencing that key are deleted, respectively.

`on update cascade` When a referenced foreign key is updated, all rows referencing that key are updated, respectively.

```sql
create table table_name (
id bigint generated by default as identity primary key,
product_id uuid references products (id) on delete cascade,
product_id uuid references products (id) on delete cascade on update cascade,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better if on update cascade is placed on another sql script below, instead of appending it to the end.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use on delete cascade on update cascade to make it more optimal and avoid errors

data jsonb,
name text
);
Expand Down