SQLの基礎(2)難しい箇所は丁寧に説明しますので、質問を沢山してください
複数データ入力
- DB(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table list( no int, name text, birthday date);
- 一度に複数データを登録する
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');
- テーブル内容を確認する
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;
- DB(PostgreSQL)を切断する
postgres=# \q
データ初期値
- DB(PostgreSQL)と接続する
$ psql -U postgres
- テーブルを作成する
postgres=# create table msg( no serial, msg text, dt timestamp DEFAULT CURRENT_TIMESTAMP );
- データを登録する
postgres=# insert into msg(msg) values('メッセージ1'),('メッセージ2'),('メッセージ3'),('メッセージ4');
- テーブル内容を確認する
postgres=# select * from msg order by no;
- データを削除する
postgres=# delete from msg where no=2;
- テーブル内容を確認する
postgres=# select * from msg order by no;
- テーブルを削除する
postgres=# drop table msg;
- DB(PostgreSQL)を切断する
postgres=# \q
日付表示
- DB(PostgreSQL)と接続する
$ psql -U postgres
- DBにlocaleを設定する
postgres=# set lc_time to 'ja_JP.UTF-8';
- 日付テーブル作成する
postgres=# create table cal( yr int2 default 2022, dt date );
- 日付を登録する
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')
;
- 日付を確認する
postgres=# select to_char(dt,'YYYY-MM-DD(TMdy)') from cal;
- DB(PostgreSQL)を切断する
postgres=# \q
|