Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- postgres=# CREATE TABLE categories (
- postgres(# id serial primary key,
- postgres(# title varchar(50)
- postgres(# );
- CREATE TABLE
- postgres=# CREATE TABLE tests (
- postgres(# id serial primary key,
- postgres(# title varchar(50),
- postgres(# level int,
- postgres(# category_id serial references categories(id)
- postgres(# );
- CREATE TABLE
- postgres=# CREATE TABLE questions (
- postgres(# id serial primary key,
- postgres(# body varchar(50),
- postgres(# test_id serial references tests(id)
- postgres(# );
- CREATE TABLE
- postgres=# INSERT INTO categories(title) VALUES
- postgres-# ('geography'),
- postgres-# ('math'),
- postgres-# ('history');
- INSERT 0 3
- postgres=# INSERT INTO tests (title, level, category_id) VALUES
- postgres-# ('first test', 5, 2),
- postgres-# ('second test', 3, 2),
- postgres-# ('third test', 2, 3),
- postgres-# ('fourth test', 9, 3),
- postgres-# ('fifth test', 0, 1);
- INSERT 0 5
- postgres=# INSERT INTO questions (body, test_id) VALUES
- postgres-# ('who is the creater of Ruby?', 3),
- postgres-# ('what is 2+2?', 2),
- postgres-# ('when was the Kennedy killed?', 4),
- postgres-# ('who is the current world football champion?', 4),
- postgres-# ('what is the capital of Australia?', 5);
- INSERT 0 5
- postgres=# SELECT *
- postgres-# FROM tests
- postgres-# WHERE level = 3 ;
- id | title | level | category_id
- ----+-------------+-------+-------------
- 2 | second test | 3 | 2
- (1 row)
- postgres=# SELECT *
- postgres-# FROM tests
- postgres-# WHERE level = 2;
- id | title | level | category_id
- ----+------------+-------+-------------
- 3 | third test | 2 | 3
- (1 row)
- postgres=# SELECT *
- postgres-# FROM questions
- postgres-# WHERE test_id = 4;
- id | body | test_id
- ----+---------------------------------------------+---------
- 3 | when was the Kennedy killed? | 4
- 4 | who is the current world football champion? | 4
- (2 rows)
- postgres=# UPDATE tests
- postgres-# SET title = 'sixth test', level = 78
- postgres-# WHERE title = 'first test';
- UPDATE 1
- postgres=# DELETE FROM questions
- postgres-# WHERE test_id = 4;
- DELETE 2
- postgres=# SELECT tests.title, categories.title
- postgres-# FROM tests INNER JOIN categories ON tests.category_id = categories.id;
- title | title
- -------------+-----------
- second test | math
- third test | history
- fourth test | history
- fifth test | geography
- sixth test | math
- (5 rows)
- postgres=# SELECT questions.body, tests.title
- postgres-# FROM questions INNER JOIN tests ON tests.id = questions.test_id ;
- body | title
- -----------------------------------+-------------
- who is the creater of Ruby? | third test
- what is 2+2? | second test
- what is the capital of Australia? | fifth test
- (3 rows)
Add Comment
Please, Sign In to add comment