1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Tips】PostgreSQLでテンポラリーテーブルをコミット時にDropする。

Posted at

はじめに

表題通り、テンポラリーテーブルをコミット時にテンポラリーテーブルをDropする。
データコンバート実施時に、テンポラリーテーブルをよく作ります。テンポラリーテーブルを作った後、最後にDROPする必要があるのですが、本番環境でDROPコマンドを打ちたくなかったです。
そこで、コミット時に一時テーブルをDROPするコマンドを記載します。

SQL

BEGIN TRANSACTION;
 
CREATE TEMP TABLE tmp_table ON COMMIT DROP AS 
(
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL
);
COMMIT;

ON COMMIT DROPを追加するだけです。これでCOMMIT実行後に一時テーブルはDROPされます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?