Last active
October 22, 2018 14:55
-
-
Save DADUSHKA/046a7adb5cbb4fd851a6066cab39c2c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| postgres=# CREATE DATABASE test_guru; | |
| CREATE DATABASE | |
| postgres-# \c test_guru | |
| test_guru=# CREATE TABLE categories(id serial PRIMARY KEY,title varchar(25)); | |
| CREATE TABLE | |
| test_guru=# \dt | |
| List of relations | |
| Schema | Name | Type | Owner | |
| --------+------------+-------+---------- | |
| public | categories | table | postgres | |
| (1 row) | |
| test_guru=# CREATE TABLE tests(id serial PRIMARY KEY,title varchar(100),level int,category_id int); | |
| CREATE TABLE | |
| test_guru=# \dt | |
| List of relations | |
| Schema | Name | Type | Owner | |
| --------+------------+-------+---------- | |
| public | categories | table | postgres | |
| public | tests | table | postgres | |
| (2 rows) | |
| test_guru=# CREATE TABLE questions(id serial PRIMARY KEY,body varchar(100),test_id int); | |
| CREATE TABLE | |
| test_guru=# \dt | |
| List of relations | |
| Schema | Name | Type | Owner | |
| --------+------------+-------+---------- | |
| public | categories | table | postgres | |
| public | questions | table | postgres | |
| public | tests | table | postgres | |
| (3 rows) | |
| test_guru=# INSERT INTO categories(title) values ('category_one'), | |
| test_guru-# ('category_two'),('category_three'); | |
| INSERT 0 3 | |
| test_guru=# INSERT INTO tests(title, level, category_id) VALUES | |
| test_guru-# ('tests_one', 1, 1), ('tests_two', 2, 2),('tests_three', 1, 3), | |
| test_guru-# ('tests_fore', 2, 1),('tests_five', 3, 3); | |
| INSERT 0 5 | |
| test_guru=# INSERT INTO questions(body, test_id) VALUES | |
| test_guru-# ('question_one', 5),('question_two', 4), | |
| test_guru-# ('question_three', 3),('question_fore', 2), | |
| test_guru-# ('question_five', 1); | |
| INSERT 0 5 | |
| test_guru=# SELECT * FROM tests WHERE level IN (2,3); | |
| id | title | level | categories_id | |
| ----+------------+-------+--------------- | |
| 2 | tests_two | 2 | 2 | |
| 4 | tests_fore | 2 | 1 | |
| 5 | tests_five | 3 | 3 | |
| (3 rows) | |
| test_guru=# SELECT * FROM questions WHERE test_id = 1; | |
| id | body | test_id | |
| ----+---------------+--------- | |
| 5 | question_five | 1 | |
| (1 row) | |
| test_guru=# UPDATE tests | |
| test_guru-# SET title = 'tests_update', | |
| test_guru-# level = level + 1 | |
| test_guru-# WHERE title = 'tests_fore'; | |
| UPDATE 1 | |
| test_guru=# DELETE FROM questions WHERE test_id = 1; | |
| DELETE 1 | |
| test_guru=# SELECT t.title, c.title FROM categories c INNER JOIN tests t ON t.category_id = c.id; | |
| title | title | |
| --------------+---------------- | |
| tests_one | category_one | |
| tests_two | category_two | |
| tests_three | category_three | |
| tests_five | category_three | |
| tests_update | category_one | |
| (5 rows) | |
| test_guru=# SELECT q.body, t.title FROM tests t INNER JOIN questions q ON q.test_id = t.id; | |
| body | title | |
| ----------------+-------------- | |
| question_one | tests_five | |
| question_two | tests_update | |
| question_three | tests_three | |
| question_fore | tests_two | |
| (4 rows) |
здесь по аналогии с test_id стоит заменить на единственное число имя колонки
алиасы для столбцов не задал, Евгений писал про это
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Лучше указывать внешний ключ в единственном числе
test_id, поскольку данный кортеж ссылается на конкретный кортеж другого отношения: https://gist.github.com/DADUSHKA/046a7adb5cbb4fd851a6066cab39c2c5#file-gistfile1-txt-L17Здесь нужно в одном запросе выбрать тесты с уровнем 2 или 3: https://gist.github.com/DADUSHKA/046a7adb5cbb4fd851a6066cab39c2c5#file-gistfile1-txt-L49 То есть все тесты у которых уровень либо 2, либо 3.
Нужно выбрать только названия тестов и категорий: https://gist.github.com/DADUSHKA/046a7adb5cbb4fd851a6066cab39c2c5#file-gistfile1-txt-L71 Также будет полезно использовать алиасы атрибутов, чтобы в итоговом отношении было ясно где чей заголовок.
Здесь соответственно нужно выбрать только атрибут содержания вопросов и атрибут названия тестов: https://gist.github.com/DADUSHKA/046a7adb5cbb4fd851a6066cab39c2c5#file-gistfile1-txt-L82