new: PostgreSQL WITH and INSERT ... RETURNING statements

This commit is contained in:
Marco D'Agostini 2020-11-13 13:41:01 -05:00
parent dedefa9d29
commit c7a070ece0
2 changed files with 61 additions and 0 deletions

25
INSERT ... RETURNING.md Normal file
View File

@ -0,0 +1,25 @@
---
tags: database, potsgresql, sql
---
# INSERT ... RETURNING
You can return any information about any inserted element in the `INSERT` statement by using the `RETURNING ...` clause
## Example
```sql
INSERT INTO roles (name)
VALUES ('master')
RETURNING role_id
```
So after `RETURNING` you choose all the columns you want to get
Very useful along [[The WITH statement]] to chain a bunch of insertions atomically without using transactions
---
## What led to learning this
- Started using the SQL language in PostgreSQL. Working on a [tiny family project](https://github.com/taionca/taionca/)

36
The WITH statement.md Normal file
View File

@ -0,0 +1,36 @@
---
tags: database, potsgresql, transaction, sql
---
# The WITH statement
Allows to save into variables tables results returned from a `SELECT` or [[INSERT ... RETURNING]] statement, and use them in following querys.
Also allows to chain multiple `INSERTS`, and if any of them fails, all previous `INSERTS` are rolledback.\
*Note: autoincrements still increase on failures, hence skipped.*
So It's excellent to avoid explicitly using transactions
## Example
```sql
WITH inserted_user as (
INSERT INTO users (username, name, password_hash)
VALUES ('master', 'master', '$argon2i$v=19$m=4096,t=10,p=1$l9mKHF/++OJO4Fzj5VvOxw$smezKrrynx74W2+7L4zyiKUXWFdQDqdKf2RBMU4p0JI')
RETURNING user_id
), inserted_role as (
INSERT INTO roles (name)
VALUES ('master')
RETURNING role_id
), t as (
INSERT INTO join_users_roles (user_id, role_id)
SELECT user_id, role_id
FROM inserted_user, inserted_role
)
INSERT INTO join_roles_permissions (role_id, permission_id)
SELECT role_id, permission_id FROM inserted_role, permissions;
```
## What led to learning this
- Needed to insert a bunch of data atomically accross many tables, see the [example](#example) above. Working on a [tiny family project](https://github.com/taionca/taionca/)