-
-
Save Codcore/166db389013ba1fd6c39dcb4b7069149 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
1.) | |
test_guru=# CREATE TABLE categories ( | |
test_guru(# id SERIAL PRIMARY KEY, | |
test_guru(# title TEXT | |
); | |
CREATE TABLE | |
test_guru=# CREATE TABLE tests | |
test_guru(# id SERIAL PRIMARY KEY, | |
test_guru(# title TEXT, | |
test_guru(# level INTEGER, | |
test_guru(# category_id INTEGER REFERENCES categories(id) | |
test_guru(# ); | |
CREATE TABLE | |
test_guru=# CREATE TABLE questions ( | |
test_guru(# id SERIAL PRIMARY KEY, | |
body TEXT, | |
test_id INTEGER REFERENCES test(id) | |
); | |
CREATE TABLE | |
---------------------------------------------------------------------- | |
2.) | |
test_guru=# INSERT INTO categories VALUES ('Ruby'); | |
INSERT 0 1 | |
test_guru=# INSERT INTO categories VALUES ('Rails'); | |
INSERT 0 1 | |
test_guru=# INSERT INTO categories VALUES ('Rspec'); | |
INSERT 0 1 | |
test_guru=# | |
test_guru=# INSERT INTO tests (title, level, category_id) VALUES ('Hashes', 1, 1); | |
INSERT 0 1 | |
test_guru=# INSERT INTO tests (title, level, category_id) VALUES ('Classes', 3, 1); | |
INSERT 0 1 | |
test_guru=# INSERT INTO tests (title, level, category_id) VALUES ('Metaprogramming', 6, 1); | |
INSERT 0 1 | |
test_guru=# INSERT INTO tests (title, level, category_id) VALUES ('Rails routing', 1, 2); | |
INSERT 0 1 | |
test_guru=# INSERT INTO tests (title, level, category_id) VALUES ('Rspec essentials', 2, 3); | |
INSERT 0 1 | |
test_guru=# | |
test_guru=# INSERT INTO questions (body, test_id) VALUES ('How to declare a Hash?', 1) | |
test_guru-# ; | |
INSERT 0 1 | |
test_guru=# INSERT INTO questions (body, test_id) VALUES ('How to declare a Class?', 2); | |
INSERT 0 1 | |
test_guru=# INSERT INTO questions (body, test_id) VALUES ('How to call private method outside of a class?', 3); | |
INSERT 0 1 | |
test_guru=# INSERT INTO questions (body, test_id) VALUES ('Where application routes is defined?', 4); | |
INSERT 0 1 | |
test_guru=# INSERT INTO questions (body, test_id) VALUES ('What is a RSpec?', 5); | |
INSERT 0 1 | |
test_guru=# | |
test_guru=# SELECT * FROM tests WHERE level IN (2, 3); | |
title | level | category_id | id | |
------------------+-------+-------------+---- | |
Classes | 3 | 1 | 2 | |
Rspec essentials | 2 | 3 | 5 | |
Work with Hashes | 2 | 1 | 1 | |
(3 rows) | |
test_guru=# SELECT * FROM questions WHERE test_id = 2 | |
test_guru-# ; | |
body | test_id | id | |
-------------------------+---------+---- | |
How to declare a Class? | 2 | 2 | |
(1 row) | |
test_guru=# UPDATE tests SET title = 'Work with Hashes', level = 2 WHERE title = 'Hashes'; | |
UPDATE 1 | |
test_guru=# | |
test_guru=# DELETE FROM questions WHERE test_id = 2; | |
DELETE 1 | |
test_guru=# | |
test_guru=# SELECT tests.title, categories.title AS category_title FROM tests INNER JOIN categories | |
ON tests.category_id = categories.id | |
; | |
title | category_title | |
------------------+---------------- | |
Classes | Ruby | |
Metaprogramming | Ruby | |
Rails routing | Rails | |
Rspec essentials | Rspec | |
Work with Hashes | Ruby | |
(5 rows) | |
test_guru=# SELECT questions.body AS question, tests.title AS test_title FROM questions INNER JOIN tests ON tests.id = questions.test_id | |
; | |
question | test_title | |
------------------------------------------------+------------------ | |
How to declare a Hash? | Work with Hashes | |
How to call private method outside of a class? | Metaprogramming | |
Where application routes is defined? | Rails routing | |
What is a RSpec? | Rspec essentials | |
(4 rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
тут в случае, если таблица будет использоваться как внешний ключ, стоит сразу это указать
category_id INTEGER REFERENCES categories(id)