Guest User

Untitled

a guest
Jul 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. postgres=# CREATE TABLE categories (
  2. postgres(# id serial primary key,
  3. postgres(# title varchar(50)
  4. postgres(# );
  5. CREATE TABLE
  6.  
  7.  
  8. postgres=# CREATE TABLE tests (
  9. postgres(# id serial primary key,
  10. postgres(# title varchar(50),
  11. postgres(# level int,
  12. postgres(# category_id serial references categories(id)
  13. postgres(# );
  14. CREATE TABLE
  15.  
  16. postgres=# CREATE TABLE questions (
  17. postgres(# id serial primary key,
  18. postgres(# body varchar(50),
  19. postgres(# test_id serial references tests(id)
  20. postgres(# );
  21. CREATE TABLE
  22.  
  23. postgres=# INSERT INTO categories(title) VALUES
  24. postgres-# ('geography'),
  25. postgres-# ('math'),
  26. postgres-# ('history');
  27. INSERT 0 3
  28.  
  29. postgres=# INSERT INTO tests (title, level, category_id) VALUES
  30. postgres-# ('first test', 5, 2),
  31. postgres-# ('second test', 3, 2),
  32. postgres-# ('third test', 2, 3),
  33. postgres-# ('fourth test', 9, 3),
  34. postgres-# ('fifth test', 0, 1);
  35. INSERT 0 5
  36.  
  37. postgres=# INSERT INTO questions (body, test_id) VALUES
  38. postgres-# ('who is the creater of Ruby?', 3),
  39. postgres-# ('what is 2+2?', 2),
  40. postgres-# ('when was the Kennedy killed?', 4),
  41. postgres-# ('who is the current world football champion?', 4),
  42. postgres-# ('what is the capital of Australia?', 5);
  43. INSERT 0 5
  44.  
  45. postgres=# SELECT *
  46. postgres-# FROM tests
  47. postgres-# WHERE level = 3 ;
  48. id | title | level | category_id
  49. ----+-------------+-------+-------------
  50. 2 | second test | 3 | 2
  51. (1 row)
  52.  
  53. postgres=# SELECT *
  54. postgres-# FROM tests
  55. postgres-# WHERE level = 2;
  56. id | title | level | category_id
  57. ----+------------+-------+-------------
  58. 3 | third test | 2 | 3
  59. (1 row)
  60.  
  61. postgres=# SELECT *
  62. postgres-# FROM questions
  63. postgres-# WHERE test_id = 4;
  64. id | body | test_id
  65. ----+---------------------------------------------+---------
  66. 3 | when was the Kennedy killed? | 4
  67. 4 | who is the current world football champion? | 4
  68. (2 rows)
  69.  
  70. postgres=# UPDATE tests
  71. postgres-# SET title = 'sixth test', level = 78
  72. postgres-# WHERE title = 'first test';
  73. UPDATE 1
  74.  
  75. postgres=# DELETE FROM questions
  76. postgres-# WHERE test_id = 4;
  77. DELETE 2
  78.  
  79. postgres=# SELECT tests.title, categories.title
  80. postgres-# FROM tests INNER JOIN categories ON tests.category_id = categories.id;
  81. title | title
  82. -------------+-----------
  83. second test | math
  84. third test | history
  85. fourth test | history
  86. fifth test | geography
  87. sixth test | math
  88. (5 rows)
  89.  
  90. postgres=# SELECT questions.body, tests.title
  91. postgres-# FROM questions INNER JOIN tests ON tests.id = questions.test_id ;
  92. body | title
  93. -----------------------------------+-------------
  94. who is the creater of Ruby? | third test
  95. what is 2+2? | second test
  96. what is the capital of Australia? | fifth test
  97. (3 rows)
Add Comment
Please, Sign In to add comment