diff --git a/INSERT ... RETURNING.md b/INSERT ... RETURNING.md new file mode 100644 index 0000000..88130ac --- /dev/null +++ b/INSERT ... RETURNING.md @@ -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/) diff --git a/The WITH statement.md b/The WITH statement.md new file mode 100644 index 0000000..fe74325 --- /dev/null +++ b/The WITH statement.md @@ -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/)