実行例
環境
- Windows 10 64bit
- SQLite3 (3.35.5) Command-Line Shell
サンプルテーブル(product)
|
id
|
name
|
quantity
|
|
1
|
tomato |
100
|
|
2
|
potato |
120
|
|
3
|
pumpkin |
50
|
「id=2」のレコードを削除し、削除された行を確認してみる。
sqlite> select * from product;
1|tomato|100
2|potato|120
3|pumpkin|50
sqlite> -- # 1.
sqlite> delete from product
...> where
...> id = 2
...> returning
...> id
...> , name
...> , quantity
...> ;
2|potato|120
sqlite> -- # 2.
sqlite> select * from product;
1|tomato|100
3|pumpkin|50
- returning構文を使うと削除対象になったレコードが表示される
- テーブルからは正しく削除されている
returningで取得するカラムに「*」を指定することもできる。
sqlite> delete from product
...> where
...> id = 2
...> returning
...> *
...> ;
2|potato|120
returningで取得する内容に別名をつけることもできる。
sqlite> .mode box
sqlite> .headers on
sqlite> delete from product
...> where
...> id = 2
...> returning
...> id as column1
...> , name as column2
...> , quantity as column3
...> ;
┌─────────┬─────────┬─────────┐
│ column1 │ column2 │ column3 │
├─────────┼─────────┼─────────┤
│ 2 │ potato │ 120 │
└─────────┴─────────┴─────────┘
SQLite 3 コマンドラインツールでselectの結果を見やすくする
SQLite 3 コマンドラインツールでカラム名を表示する「.headers」について