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

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

 SQLの基礎(2)
    難しい箇所は丁寧に説明しますので、質問を沢山してください
複数データ入力
  1. DB(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,'田中','19910101'),(3,'木村','19910103'),(5,'吉田','19910105'),(7,'小林','19910111'),(9,'渡辺','19910113'),(2,'鈴木','19910115'),(4,'山田','19910121'),(6,'斎藤','19910123'),(8,'清原','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. DB(PostgreSQL)を切断する
  20. postgres=# \q
データ初期値
  1. DB(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. テーブルを作成する
  4. postgres=# create table msg( no serial, msg text, dt timestamp DEFAULT CURRENT_TIMESTAMP );

  5. データを登録する
  6. postgres=# insert into msg(msg) values('メッセージ1'),('メッセージ2'),('メッセージ3'),('メッセージ4');

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

  9. データを削除する
  10. postgres=# delete from msg where no=2;

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

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

  15. DB(PostgreSQL)を切断する
  16. postgres=# \q
日付表示
  1. DB(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. DBにlocaleを設定する
  4. postgres=# set lc_time to 'ja_JP.UTF-8';

  5. 日付テーブル作成する
  6. postgres=# create table cal( yr int2 default 2022, dt date );

  7. 日付を登録する
  8. postgres=# insert into cal(dt) values ('20220101'), ('20220102'), ('20220103'), ('20220104'), ('20220105'), ('20220106'), ('20220107'), ('20220108'), ('20220109'), ('20220110'), ('20220111'), ('20220112'), ('20220113'), ('20220114'), ('20220115'), ('20220116'), ('20220117'), ('20220118'), ('20220119'), ('20220120'), ('20220121'), ('20220122'), ('20220123'), ('20220124'), ('20220125'), ('20220126'), ('20220127'), ('20220128'), ('20220129'), ('20220130'), ('20220131') ;

  9. 日付を確認する
  10. postgres=# select to_char(dt,'YYYY-MM-DD(TMdy)') from cal;

  11. DB(PostgreSQL)を切断する
  12. postgres=# \q
SQL(1) SQL(2) SQL(3) SQL(4) SQL(5)
Copyright© Ciapia IT Academy 2022.