Postgresには独自の命令文が多数用意されています。
blogapp=# select name, length(name) from users; name | length --------+-------- yamada | 6 satou | 5 sasaki | 6 yamada | 6 satou | 5 sasaki | 6 yamada | 6 (7 行)
抽選システム
blogapp=# select * from users order by random() limit 1; id | name | score | team ----+--------+-------+------- 6 | sasaki | 7.6 | green (1 行)
更新
blogapp=# update users set score = 5.8 where name = 'yamada'; UPDATE 3
削除
blogapp=# delete from users where id = 7; DELETE 1
テーブルの変更
blogapp=# alter table users add fullname varchar(255); ALTER TABLE blogapp=# select * from users; id | name | score | team | fullname ----+--------+-------+-------+---------- 2 | satou | 4.3 | green | 3 | sasaki | 8.2 | green | 5 | satou | 4.2 | blue | 6 | sasaki | 7.6 | green | 1 | yamada | 5.8 | red | 4 | yamada | 5.8 | red | (6 行) blogapp=# alter table users alter name type varchar(32); ALTER TABLE
複数のユーザを突き合わせ
select users.name, post.title from users, posts wher users.id = posts.id;
複雑なselect文はviewを作成
blogapp=# create view yamada_posts as blogapp-# select users.name, post.title from users, posts wher users.id = posts.id;
beginとcommitで囲むと、トランザクションという処理ができるようになります。変更の取り消しはrollbackです。