Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- elnur@elnur-home:~$ psql
- psql (9.0.4)
- Type "help" for help.
- elnur=# CREATE TABLE category(
- id bigserial PRIMARY KEY,
- name varchar);
- NOTICE: CREATE TABLE will create implicit sequence "category_id_seq" for serial column "category.id"
- NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "category_pkey" for table "category"
- CREATE TABLE
- elnur=# CREATE TABLE post(
- elnur(# id bigserial PRIMARY KEY,
- elnur(# category_id bigint REFERENCES category,
- elnur(# title varchar,
- elnur(# body text);
- NOTICE: CREATE TABLE will create implicit sequence "post_id_seq" for serial column "post.id"
- NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "post_pkey" for table "post"
- CREATE TABLE
- elnur=# \dt
- List of relations
- Schema | Name | Type | Owner
- --------+----------+-------+-------
- public | category | table | elnur
- public | post | table | elnur
- (2 rows)
- elnur=# insert into category (name) values('Используемая категория'),('Неиспользуемая категория);
- elnur'# \q
- elnur@elnur-home:~$ psql
- psql (9.0.4)
- Type "help" for help.
- elnur=# insert into category (name) value('Используемая категория'), ('Неиспользуемая категория');
- ERROR: syntax error at or near "value"
- LINE 1: insert into category (name) value('Используемая категория'),...
- ^
- elnur=# insert into category (name) values('Используемая категория'), ('Неиспользуемая категория');
- INSERT 0 2
- elnur=# select * from category;
- id | name
- ----+--------------------------
- 1 | Используемая категория
- 2 | Неиспользуемая категория
- (2 rows)
- elnur=# insert into post (category_id, title) values(1, 'Крутой пост');
- INSERT 0 1
- elnur=# select * from post;
- id | category_id | title | body
- ----+-------------+-------------+------
- 1 | 1 | Крутой пост |
- (1 row)
- elnur=# select c.* from category c
- elnur-# left join post p
- elnur-# on c.id = p.category_id;
- id | name
- ----+--------------------------
- 1 | Используемая категория
- 2 | Неиспользуемая категория
- (2 rows)
- elnur=# select c.* from category c
- left join post p
- on c.id = p.category_id
- elnur-# where p.id IS NULL;
- id | name
- ----+--------------------------
- 2 | Неиспользуемая категория
- (1 row)
- elnur=#
Add Comment
Please, Sign In to add comment