PostgreSQL 13にTRUNCATEでテーブルから全ての行を削除する

環境
Windows10 64bit
PostgreSQL 13.2

書式
TRUNCATE [ TABLE ] name [, …]
TRUNCATEはテーブル群から全ての行を素早く削除します。

使用例
1.テーブルを作成します
testdb=# create table emptbl (empname varchar(30), empno int);
CREATE TABLE

2.テーブルに4つのデータを追加します
testdb=# insert into emptbl values('A01’, 20),('B02’, 29),('C03’, 34),('Yamashiro’, 36);
INSERT 0 4

3.データを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# select * from emptbl;
empname | empno
-----------+-------
A01 | 20
B02 | 29
C03 | 34
Yamashiro | 36
(4)
testdb=# select * from emptbl; empname | empno -----------+------- A01 | 20 B02 | 29 C03 | 34 Yamashiro | 36 (4 行)
testdb=# select * from emptbl;
  empname  | empno
-----------+-------
 A01       |    20
 B02       |    29
 C03       |    34
 Yamashiro |    36
(4 行)

4.emptblテーブルのデータをすべて削除します
testdb=# truncate table emptbl;
TRUNCATE TABLE

5.データ削除確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# select * from emptbl;
empname | empno
---------+-------
(0)
testdb=# select * from emptbl; empname | empno ---------+------- (0 行)
testdb=# select * from emptbl;
 empname | empno
---------+-------
(0 行)

 

PostgreSQL

Posted by arkgame