SQLの基礎(4)今理解できなくても繰り返し学習すれば、少しずつ分かってきます
データ抽出の順序
- DB(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table tbl_1( no int, msg text, dt date );
- インデックスを作成する
postgres=# create index tbl_1_idx on tbl_1( no );
- データを登録する
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');
- データを抽出する
postgres=# select * from tbl_1 order by no;
postgres=# select * from tbl_1 order by no desc;
- テーブルを削除する
postgres=# drop table tbl_1;
- DB(PostgreSQL)を切断する
postgres=# \q
主キー(not null, unique, index)
- DB(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table tbl_2( no int primary key, msg text, dt date );
- データを登録する
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');
- データを抽出する
postgres=# select * from tbl_2 order by no;
- テーブルを削除する
postgres=# drop table tbl_2;
- DB(PostgreSQL)を切断する
postgres=# \q
複合主キー(not null, unique, index)
- DB(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table tbl_3( no int, msg text, dt date, primary key(no,msg));
- データを登録する
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');
- データを抽出する
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;
- テーブルを削除する
postgres=# drop table tbl_3;
- DB(PostgreSQL)を切断する
postgres=# \q
|