Created
August 5, 2019 16:36
-
-
Save Sophiliya/0cf4bdddaa41597f50efccd635ef9e72 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
CREATE DATABASE test_guru; | |
CREATE TABLE categories ( | |
id serial PRIMARY KEY, | |
title varchar(50) | |
); | |
CREATE TABLE tests ( | |
id serial PRIMARY KEY, | |
title varchar(50), | |
level int, | |
category_id int | |
); | |
CREATE TABLE questions ( | |
id serial PRIMARY KEY, | |
body varchar(200), | |
test_id int | |
); |
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
INSERT INTO categories (title) VALUES | |
('Frontend'), | |
('Backend'), | |
('Machine learning'); | |
INSERT INTO tests(title, level, category_id) VALUES | |
('Ruby', 1, 2), | |
('Ruby on Rails', 2, 2), | |
('JavaScript', 1, 1), | |
('Python', 3, 2), | |
('CSS', 1, NULL); | |
INSERT INTO questions(body, test_id) VALUES | |
('What is Ruby programming language?', 1), | |
('Why Ruby is known as a language of flexibility?', 1), | |
('Explain some differences between Ruby and Python.', 4), | |
('What are JavaScript Data Types?', 3), | |
('What is the use of isNaN function?', 3); | |
postgres=# SELECT * FROM tests WHERE level in (2, 3); | |
id | title | level | category_id | |
----+---------------+-------+------------- | |
2 | Ruby on Rails | 2 | 2 | |
4 | Python | 3 | 2 | |
(2 rows) | |
postgres=# SELECT q.body FROM tests t | |
postgres-# JOIN questions q ON t.id = q.test_id | |
postgres-# WHERE t.id = 1; | |
body | |
------------------------------------------------- | |
What is Ruby programming language? | |
Why Ruby is known as a language of flexibility? | |
(2 rows) | |
postgres=# UPDATE tests SET title = 'RoR', level = 3 WHERE id = 7; | |
UPDATE 1 | |
postgres=# DELETE FROM questions WHERE test_id = 3; | |
DELETE 2 | |
postgres=# SELECT t.title as test, c.title as category FROM tests t | |
postgres-# JOIN categories c ON c.id = t.category_id; | |
test | category | |
------------+---------- | |
Ruby | Backend | |
JavaScript | Frontend | |
Python | Backend | |
RoR | Backend | |
(4 rows) | |
postgres=# SELECT t.title as test, q.body as question FROM tests t | |
JOIN questions q ON t.id = q.test_id; | |
test | question | |
------------+--------------------------------------------------- | |
Ruby | Why Ruby is known as a language of flexibility? | |
JavaScript | What is the use of isNaN function? | |
JavaScript | What are JavaScript Data Types? | |
Python | Explain some differences between Ruby and Python. | |
(4 rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Sophiliya/0cf4bdddaa41597f50efccd635ef9e72#file-1-sql-ddl-L12 и https://gist.github.com/Sophiliya/0cf4bdddaa41597f50efccd635ef9e72#file-1-sql-ddl-L18 надо указать что эти колонки являются внешними ключами