Guest User

Untitled

a guest
Oct 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. elnur@elnur-home:~$ psql
  2. psql (9.0.4)
  3. Type "help" for help.
  4.  
  5. elnur=# CREATE TABLE category(
  6. id bigserial PRIMARY KEY,
  7. name varchar);
  8. NOTICE: CREATE TABLE will create implicit sequence "category_id_seq" for serial column "category.id"
  9. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "category_pkey" for table "category"
  10. CREATE TABLE
  11. elnur=# CREATE TABLE post(
  12. elnur(# id bigserial PRIMARY KEY,
  13. elnur(# category_id bigint REFERENCES category,
  14. elnur(# title varchar,
  15. elnur(# body text);
  16. NOTICE: CREATE TABLE will create implicit sequence "post_id_seq" for serial column "post.id"
  17. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "post_pkey" for table "post"
  18. CREATE TABLE
  19. elnur=# \dt
  20. List of relations
  21. Schema | Name | Type | Owner
  22. --------+----------+-------+-------
  23. public | category | table | elnur
  24. public | post | table | elnur
  25. (2 rows)
  26.  
  27. elnur=# insert into category (name) values('Используемая категория'),('Неиспользуемая категория);
  28. elnur'# \q
  29. elnur@elnur-home:~$ psql
  30. psql (9.0.4)
  31. Type "help" for help.
  32.  
  33. elnur=# insert into category (name) value('Используемая категория'), ('Неиспользуемая категория');
  34. ERROR: syntax error at or near "value"
  35. LINE 1: insert into category (name) value('Используемая категория'),...
  36. ^
  37. elnur=# insert into category (name) values('Используемая категория'), ('Неиспользуемая категория');
  38. INSERT 0 2
  39. elnur=# select * from category;
  40. id | name
  41. ----+--------------------------
  42. 1 | Используемая категория
  43. 2 | Неиспользуемая категория
  44. (2 rows)
  45.  
  46. elnur=# insert into post (category_id, title) values(1, 'Крутой пост');
  47. INSERT 0 1
  48. elnur=# select * from post;
  49. id | category_id | title | body
  50. ----+-------------+-------------+------
  51. 1 | 1 | Крутой пост |
  52. (1 row)
  53.  
  54. elnur=# select c.* from category c
  55. elnur-# left join post p
  56. elnur-# on c.id = p.category_id;
  57. id | name
  58. ----+--------------------------
  59. 1 | Используемая категория
  60. 2 | Неиспользуемая категория
  61. (2 rows)
  62.  
  63. elnur=# select c.* from category c
  64. left join post p
  65. on c.id = p.category_id
  66. elnur-# where p.id IS NULL;
  67. id | name
  68. ----+--------------------------
  69. 2 | Неиспользуемая категория
  70. (1 row)
  71.  
  72. elnur=#
Add Comment
Please, Sign In to add comment