JR中央線・三鷹駅
南口 徒歩3分

[ 地図 ]
TOP 学習環境 SQL C言語 Java

 SQLの基礎(1)
    初心者コースでは、SQLの基礎から学習します
基本操作
  1. データベース(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. テーブルを作成する
  4. postgres=# create table list( no int, name text, birthday date);

  5. データを登録する
  6. postgres=# insert into list(no,name,birthday) values(101,'田中','19910131');

  7. テーブル内容を確認する
  8. postgres=# select * from list;

  9. データを変更する
  10. postgres=# update list set name='木村';

  11. テーブル内容を確認する
  12. postgres=# select * from list;

  13. データを削除する
  14. postgres=# delete from list;

  15. テーブル内容を確認する
  16. postgres=# select * from list;

  17. テーブルを削除する
  18. postgres=# drop table list;

  19. データベース(PostgreSQL)を切断する
  20. postgres=# \q
複数データ
  1. データベース(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. テーブルを作成する
  4. postgres=# create table list( no int, name text, birthday date);

  5. データを登録する
  6. 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');

  7. テーブル内容を確認する
  8. postgres=# select * from list order by no;

  9. データを変更する
  10. 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='小林';

  11. テーブル内容を確認する
  12. postgres=# select * from list order by no;

  13. データを削除する
  14. postgres=# delete from list where no<10;

  15. テーブル内容を確認する
  16. postgres=# select * from list order by no;

  17. テーブルを削除する
  18. postgres=# drop table list;

  19. データベース(PostgreSQL)を切断する
  20. postgres=# \q
複数テーブル結合
  1. データベース(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. メニューテーブルと売上テーブル作成する
  4. postgres=# create table menu( item text, price int );
    postgres=# create table bill( no int, item text, vol int );

  5. メニューを登録する
  6. 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);

  7. メニューを確認する
  8. postgres=# select * from menu;

  9. 売上を登録する
  10. 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);

  11. 売上伝票を確認する
  12. 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;

  13. 売上テーブルを削除する
  14. postgres=# drop table bill;

  15. データベース(PostgreSQL)を切断する
  16. postgres=# \q
集計処理
  1. データベース(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. メニューを確認する
  4. postgres=# select * from menu;

  5. 売上テーブル作成する
  6. postgres=# create table bill( no int, item text, vol int, dt date );

  7. 売上を登録する
  8. 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');

  9. 売上伝票を確認する
  10. 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;

  11. 売上伝票ごとに集計する
  12. 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;

  13. 売上伝票を日付ごとに集計する
  14. postgres=# select dt as 売上日,sum(menu.price*bill.vol) as 売上 from bill,menu where bill.item=menu.item group by dt order by dt;

  15. 売上テーブルを削除する
  16. postgres=# truncate table bill;

  17. データベース(PostgreSQL)を切断する
  18. postgres=# \q
SQL(1) SQL(2) SQL(3) SQL(4) SQL(5)
Copyright© Ciapia IT Academy 2022.