SQLの基礎(1)
基本操作
- データベース(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table list( no int, name text, birthday date);
- データを登録する
postgres=# insert into list(no,name,birthday) values(101,'田中','19910131');
- テーブル内容を確認する
postgres=# select * from list;
- データを変更する
postgres=# update list set name='木村';
- テーブル内容を確認する
postgres=# select * from list;
- データを削除する
postgres=# delete from list;
- テーブル内容を確認する
postgres=# select * from list;
- テーブルを削除する
postgres=# drop table list;
- データベース(PostgreSQL)を切断する
postgres=# \q
複数データ
- データベース(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table list( no int, name text, birthday date);
- データを登録する
postgres=# insert into list(no,name,birthday) values(1,'田中','19910131');
postgres=# insert into list(no,name,birthday) values(3,'小林','19860803');
postgres=# insert into list(no,name,birthday) values(6,'木村','19991231');
postgres=# insert into list(no,name,birthday) values(10,'鈴木','20021024');
postgres=# insert into list(no,name,birthday) values(15,'渡辺','19950619');
- テーブル内容を確認する
postgres=# select * from list order by no;
- データを変更する
postgres=# update list set no=9 where birthday='19950619';
postgres=# update list set name='吉田' where no=10;
postgres=# update list set birthday='20000101' where name='小林';
- テーブル内容を確認する
postgres=# select * from list order by no;
- データを削除する
postgres=# delete from list where no<10;
- テーブル内容を確認する
postgres=# select * from list order by no;
- テーブルを削除する
postgres=# drop table list;
- データベース(PostgreSQL)を切断する
postgres=# \q
複数テーブル結合
- データベース(PostgreSQL)と接続する
$ psql -U postgres
- メニューテーブルと売上テーブル作成する
postgres=# create table menu( item text, price int );
postgres=# create table bill( no int, item text, vol int );
- メニューを登録する
postgres=# insert into menu(item,price) values('ラーメン',500);
postgres=# insert into menu(item,price) values('塩ラーメン',500);
postgres=# insert into menu(item,price) values('味噌ラーメン',600);
postgres=# insert into menu(item,price) values('チャーハン',400);
postgres=# insert into menu(item,price) values('餃子',350);
- メニューを確認する
postgres=# select * from menu;
- 売上を登録する
postgres=# insert into bill(no,item,vol) values(1,'ラーメン',1);
postgres=# insert into bill(no,item,vol) values(1,'餃子',1);
postgres=# insert into bill(no,item,vol) values(2,'味噌ラーメン',3);
postgres=# insert into bill(no,item,vol) values(2,'チャーハン',2);
postgres=# insert into bill(no,item,vol) values(3,'塩ラーメン',1);
postgres=# insert into bill(no,item,vol) values(3,'チャーハン',1);
- 売上伝票を確認する
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=1;
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=2;
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=3;
- 売上テーブルを削除する
postgres=# drop table bill;
- データベース(PostgreSQL)を切断する
postgres=# \q
集計処理
- データベース(PostgreSQL)と接続する
$ psql -U postgres
- メニューを確認する
postgres=# select * from menu;
- 売上テーブル作成する
postgres=# create table bill( no int, item text, vol int, dt date );
- 売上を登録する
postgres=# insert into bill(no,item,vol) values(1,'ラーメン',1,'20220901');
postgres=# insert into bill(no,item,vol) values(1,'味噌ラーメン',1,'20220901');
postgres=# insert into bill(no,item,vol) values(1,'餃子',1,'20220901');
postgres=# insert into bill(no,item,vol) values(2,'味噌ラーメン',3,'20220901');
postgres=# insert into bill(no,item,vol) values(2,'チャーハン',2,'20220901');
postgres=# insert into bill(no,item,vol) values(2,'餃子',5,'20220901');
postgres=# insert into bill(no,item,vol) values(3,'塩ラーメン',1,'20220902');
postgres=# insert into bill(no,item,vol) values(3,'チャーハン',1,'20220902');
- 売上伝票を確認する
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=1;
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=2;
postgres=# select no as 伝票No,bill.item as 注文,vol as 数量,vol*price as 小計 from bill, menu where bill.item=menu.item and no=3;
- 売上伝票ごとに集計する
postgres=# select no as 売上No,sum(menu.price*bill.vol) as 売上 from bill,menu where bill.item=menu.item group by no,no order by no;
- 売上伝票を日付ごとに集計する
postgres=# select dt as 売上日,sum(menu.price*bill.vol) as 売上 from bill,menu where bill.item=menu.item group by dt order by dt;
- 売上テーブルを削除する
postgres=# truncate table bill;
- データベース(PostgreSQL)を切断する
postgres=# \q
|