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

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

 SQLの基礎(4)
    今理解できなくても繰り返し学習すれば、少しずつ分かってきます
データ抽出の順序
  1. DB(PostgreSQL)と接続する
  2. $ psql -U postgres

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

  5. インデックスを作成する
  6. postgres=# create index tbl_1_idx on tbl_1( no );

  7. データを登録する
  8. postgres=# insert into tbl_1(no,msg,dt) values(1,'msg-1','now'),(3,'msg-3','now'),(5,'msg-5','now'),(4,'msg-4','now'),(2,'msg-2','now');

  9. データを抽出する
  10. postgres=# select * from tbl_1 order by no;
    postgres=# select * from tbl_1 order by no desc;

  11. テーブルを削除する
  12. postgres=# drop table tbl_1;

  13. DB(PostgreSQL)を切断する
  14. postgres=# \q
主キー(not null, unique, index)
  1. DB(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. テーブルを作成する
  4. postgres=# create table tbl_2( no int primary key, msg text, dt date );

  5. データを登録する
  6. postgres=# insert into tbl_2(no,msg,dt) values(1,'msg-1','now'),(3,'msg-2','now'),(5,'msg-3','now'),(4,'msg-4','now'),(2,'msg-5','now');

  7. データを抽出する
  8. postgres=# select * from tbl_2 order by no;

  9. テーブルを削除する
  10. postgres=# drop table tbl_2;

  11. DB(PostgreSQL)を切断する
  12. postgres=# \q
複合主キー(not null, unique, index)
  1. DB(PostgreSQL)と接続する
  2. $ psql -U postgres

  3. テーブルを作成する
  4. postgres=# create table tbl_3( no int, msg text, dt date, primary key(no,msg));

  5. データを登録する
  6. postgres=# insert into tbl_3(no,msg,dt) values(1,'msg-3','now'),(1,'msg-2','now'),(1,'msg-1','now'),(2,'msg-1','now'),(2,'msg-2','now');

  7. データを抽出する
  8. postgres=# select * from tbl_3 order by no,msg;
    postgres=# select * from tbl_3 order by no,msg desc;
    postgres=# select * from tbl_3 order by no desc,msg;

  9. テーブルを削除する
  10. postgres=# drop table tbl_3;

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