Skip to content

Instantly share code, notes, and snippets.

@Codcore
Last active February 25, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Codcore/166db389013ba1fd6c39dcb4b7069149 to your computer and use it in GitHub Desktop.
Save Codcore/166db389013ba1fd6c39dcb4b7069149 to your computer and use it in GitHub Desktop.
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)
@BubuntuClu
Copy link

тут в случае, если таблица будет использоваться как внешний ключ, стоит сразу это указать category_id INTEGER REFERENCES categories(id)

@BubuntuClu
Copy link

тут level in (2,3)

@BubuntuClu
Copy link

тут необходимо выбрать только названия тестов и категорий. так же посмотри про alias, т.к. у тебя эти столбцы имеют одинаковое название
тут здесь тоже должна быть не полная выборка

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