Last active
October 2, 2019 08:29
-
-
Save fatwebdev/141039c26a51d03c7588edc23e9c98a6 to your computer and use it in GitHub Desktop.
This file contains 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
-- Создайте базу данных test_guru | |
CREATE DATABASE test_guru; | |
-- Таблицу categories с атрибутом title | |
CREATE TABLE categories( | |
id serial PRIMARY KEY, | |
title varchar(25) NOT NULL | |
); | |
-- Таблицу tests в которой должны быть атрибуты title, level, внешний ключ к таблице categories | |
CREATE TABLE tests( | |
id serial PRIMARY KEY, | |
title varchar(50) NOT NULL, | |
level int DEFAULT 1, | |
category_id int REFERENCES categories(id) | |
); | |
-- Таблицу questions в которой должен быть атрибут body и внешний ключ к таблице tests | |
CREATE TABLE questions( | |
id serial PRIMARY KEY, | |
body varchar(200) NOT NULL, | |
test_id int REFERENCES tests(id) | |
); | |
-- Создайте 3 строки в таблице categories | |
INSERT INTO categories(title) VALUES | |
('HTML'), | |
('CSS'), | |
('JS'); | |
-- Создайте 5 строк в таблице tests (хотя бы 3 из них должны иметь отличное от NULL значение в атрибуте внешнего ключа к таблице categories) | |
INSERT INTO tests(title, level, category_id) VALUES | |
('HTML5 feature', 1, 1), | |
('semantic tags', 2, 1), | |
('CSS3 feature', 1, 2), | |
('arrow functions', 2, 3), | |
('observers', 3, 3); | |
-- Создайте 5 строк в таблице questions | |
INSERT INTO questions(body, test_id) VALUES | |
('Can you offer a use case for the new arrow => function syntax? How does this new syntax differ from other functions?', 4), | |
('What advantage is there for using the arrow syntax for a method in a constructor?', 4), | |
('What interface provides the ability to watch for changes being made to the DOM tree?', 5), | |
('What property using for creating shadow?', 3), | |
('What difference between b and strong tags?', 2); | |
-- Выберите все тесты с уровнем 2 и 3 | |
SELECT * | |
FROM tests | |
WHERE level IN (2,3); | |
-- Выберите все вопросы для определённого теста | |
SELECT * | |
FROM questions | |
WHERE test_id = 4; | |
-- Обновите атрибуты title и level для строки из таблицы tests с помощью одного запроса | |
UPDATE tests | |
SET title = 'HTML5', level = 2 | |
WHERE id = 1; | |
-- Удалите все вопросы для конкретного теста с помощью одного запроса | |
DELETE | |
FROM questions | |
WHERE test_id = 4; | |
-- С помощью JOIN выберите названия всех тестов и названия их категорий | |
SELECT tests.title AS test, categories.title AS category | |
FROM tests | |
JOIN categories | |
ON tests.category_id = categories.id; | |
-- С помощью JOIN выберите содержание всех вопросов (атрибут body) и названия связанных с ними тестов | |
SELECT body, title | |
FROM questions | |
JOIN tests | |
ON questions.test_id = tests.id; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/fatwebdev/141039c26a51d03c7588edc23e9c98a6#file-thinknetica_sql-sql-L15 и https://gist.github.com/fatwebdev/141039c26a51d03c7588edc23e9c98a6#file-thinknetica_sql-sql-L22 надо указать что это внешние ключи на другие таблица
https://gist.github.com/fatwebdev/141039c26a51d03c7588edc23e9c98a6#file-thinknetica_sql-sql-L69 при совпадении имени столбцов лучше задавать им alias для понятия где кто
https://gist.github.com/fatwebdev/141039c26a51d03c7588edc23e9c98a6#file-thinknetica_sql-sql-L50 `level in (2,3)