Skip to content

Instantly share code, notes, and snippets.

@Sandstorm750
Created October 11, 2019 19:02
Show Gist options
  • Save Sandstorm750/8f7c3f058a97c903a8119c311223b377 to your computer and use it in GitHub Desktop.
Save Sandstorm750/8f7c3f058a97c903a8119c311223b377 to your computer and use it in GitHub Desktop.
postgres=# CREATE DATABASE test_guru;
CREATE DATABASE
postgres=# CREATE TABLE categories (
postgres(# id serial PRIMARY KEY,
postgres(# title varchar(40)
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE tests (
postgres(# id serial PRIMARY KEY,
postgres(# title varchar(40),
postgres(# level int,
postgres(# category_id int
postgres(# );
CREATE TABLE
postgres=# CREATE TABLE questions (
postgres(# id serial PRIMARY KEY,
postgres(# body varchar(50),
postgres(# test_id int
postgres(# );
CREATE TABLE
postgres=# INSERT INTO categories(title) VALUES
postgres-# ('Frontend'),
postgres-# ('Backend'),
postgres-# ('Fullstack');
INSERT 0 3
postgres=# SELECT*
postgres-# FROM categories;
id | title
----+-----------
1 | Frontend
2 | Backend
3 | Fullstack
(3 строки)
postgres=# INSERT INTO tests(title, level, category_id) VALUES
postgres-# ('Ruby', 2, 2),
postgres-# ('HTML', 1, 1),
postgres-# ('CSS', 2, 1),
postgres-# ('SQL', 3, 2),
postgres-# ('RoR', 3, 3);
INSERT 0 5
postgres=# SELECT*
postgres-# FROM tests;
id | title | level | category_id
----+-------+-------+-------------
1 | Ruby | 2 | 2
2 | HTML | 1 | 1
3 | CSS | 2 | 1
4 | SQL | 3 | 2
5 | RoR | 3 | 3
(5 строк)
postgres=# INSERT INTO questions(body, test_id) VALUES
postgres-# ('Why?', 2),
postgres-# ('Why not?', 1),
postgres-# ('Where?', 4),
postgres-# ('Who?', 2),
postgres-# ('How?', 3);
INSERT 0 5
postgres=# SELECT*
postgres-# FROM questions;
id | body | test_id
----+----------+---------
1 | Why? | 2
2 | Why not? | 1
3 | Where? | 4
4 | Who? | 2
5 | How? | 3
(5 строк)
postgres=# SELECT* FROM tests WHERE level>1;
id | title | level | category_id
----+-------+-------+-------------
1 | Ruby | 2 | 2
3 | CSS | 2 | 1
4 | SQL | 3 | 2
5 | RoR | 3 | 3
(4 строки)
postgres=# SELECT*
postgres-# FROM questions
postgres-# WHERE test_id = 2;
id | body | test_id
----+------+---------
1 | Why? | 2
4 | Who? | 2
(2 строки)
postgres=# UPDATE tests
postgres-# SET title = 'HAML', level = 3
postgres-# WHERE title = 'HTML';
UPDATE 1
postgres=# SELECT*
postgres-# FROM tests;
id | title | level | category_id
----+-------+-------+-------------
1 | Ruby | 2 | 2
3 | CSS | 2 | 1
4 | SQL | 3 | 2
5 | RoR | 3 | 3
2 | HAML | 3 | 1
(5 строк)
postgres=# DELETE
postgres-# FROM questions
postgres-# WHERE test_id = 4;
DELETE 1
postgres=# SELECT*
postgres-# FROM questions;
id | body | test_id
----+----------+---------
1 | Why? | 2
2 | Why not? | 1
4 | Who? | 2
5 | How? | 3
(4 строки)
postgres=# SELECT tests.title, categories.title
postgres-# FROM tests
postgres-# JOIN categories
postgres-# ON tests.category_id = categories.id;
title | title
-------+-----------
Ruby | Backend
CSS | Frontend
SQL | Backend
RoR | Fullstack
HAML | Frontend
(5 строк)
postgres=# SELECT questions.body, tests.title
postgres-# FROM questions
postgres-# JOIN tests
postgres-# ON questions.test_id = tests.id;
body | title
----------+-------
Why? | HAML
Why not? | Ruby
Who? | HAML
How? | CSS
(4 строки)
@psylone
Copy link

psylone commented Oct 12, 2019

https://gist.github.com/Sandstorm750/8f7c3f058a97c903a8119c311223b377#file-sql2-L118
Здесь удобно использовать алиасы, чтобы было ясно где чей заголовок.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment