Skip to content

Instantly share code, notes, and snippets.

@infectedfate
Last active December 11, 2018 09:39
Show Gist options
  • Select an option

  • Save infectedfate/2fb1b408a76f0d124fcc24bca355c748 to your computer and use it in GitHub Desktop.

Select an option

Save infectedfate/2fb1b408a76f0d124fcc24bca355c748 to your computer and use it in GitHub Desktop.
DataBase
mordecai=# CREATE TABLE categories (
id serial PRIMARY KEY,
title varchar(50));
CREATE TABLE
mordecai=# CREATE TABLE tests ( id serial PRIMARY KEY,
mordecai(# title varchar(25),
mordecai(# level int,
mordecai(# category_id int);
CREATE TABLE
mordecai=# CREATE TABLE questions (
mordecai(# id serial PRIMARY KEY,
mordecai(# body varchar(50),
mordecai(# test_id int);
CREATE TABLE
mordecai=# INSERT INTO categories(title) VALUES
mordecai-# ('Programming')
mordecai-# ,
mordecai-# ('Design'),
mordecai-# ('AI');
INSERT 0 3
mordecai=# INSERT INTO tests(title, level, category_id) VALUES
mordecai-# ('Ruby', 3, 1),
mordecai-# ('HTML', 1, 1),
mordecai-# ('CSS', 2, 1),
mordecai-# ('Rails', 4, 1),
mordecai-# ('Python', 2, 1);
INSERT 0 5
mordecai=# INSERT INTO questions(body, test_id) VALUES
mordecai-# (NULL, 1),
mordecai-# (NULL, 1),
mordecai-# (NULL, 1),
mordecai-# (NULL, 2),
mordecai-# (NULL, 2);
INSERT 0 5
mordecai=# SELECT * FROM tests WHERE level=2 OR level=3;
id | title | level | category_id
----+--------+-------+-------------
1 | Ruby | 3 | 1
3 | CSS | 2 | 1
5 | Python | 2 | 1
(3 rows)
mordecai=# SELECT * FROM questions WHERE test_id=1;
id | body | test_id
----+------+---------
1 | | 1
2 | | 1
3 | | 1
(3 rows)
mordecai=# ALTER TABLE tests
mordecai-# ALTER COLUMN title SET NOT NULL;
ALTER TABLE
mordecai=# UPDATE tests SET title = 'qwerty', level=3 WHERE id=1;
UPDATE 1
mordecai=# DELETE FROM questions WHERE test_id=1;
DELETE 3
mordecai=# SELECT tests.title, categories.title FROM tests
JOIN categories
ON tests.category_id = categories.id;
title | title
--------+-------------
HTML | Programming
CSS | Programming
Rails | Programming
Python | Programming
qwerty | Programming
(5 rows)
mordecai=# SELECT questions.body, tests.title FROM questions JOIN tests ON questions.test_id = tests.id;
body | title
------+-------
| HTML
| HTML
(2 rows)
@psylone

psylone commented Dec 10, 2018

Copy link
Copy Markdown

https://gist.github.com/infectedfate/2fb1b408a76f0d124fcc24bca355c748#file-gistfile1-txt-L49 не совсем понятно, откуда берутся данные, если выше добавляются кортежи в отношение вопросов со значениями NULL.

@psylone

psylone commented Dec 10, 2018

Copy link
Copy Markdown

https://gist.github.com/infectedfate/2fb1b408a76f0d124fcc24bca355c748#file-gistfile1-txt-L57 стоит на самом деле добавить ограничение NOT NULL для атрибутов, поскольку тест без названия не имеет особого смысла.

@psylone

psylone commented Dec 10, 2018

Copy link
Copy Markdown

https://gist.github.com/infectedfate/2fb1b408a76f0d124fcc24bca355c748#file-gistfile1-txt-L63 необходимо выбрать только названия. Также будет здорово использовать алиасы атрибутов, чтобы в итоговом отношении было ясно где чей заголовок.

@psylone

psylone commented Dec 10, 2018

Copy link
Copy Markdown

https://gist.github.com/infectedfate/2fb1b408a76f0d124fcc24bca355c748#file-gistfile1-txt-L75 здесь также необходимо выбрать конкретные атрибуты.

@psylone

psylone commented Dec 11, 2018

Copy link
Copy Markdown

Все гуд, единственный момент: https://gist.github.com/infectedfate/2fb1b408a76f0d124fcc24bca355c748#file-gistfile1-txt-L70 будет здорово заиспользовать алиасы для атрибутов чтобы было понятно где чей заголовок.

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