PostgreSQL:カラムの列追加でデフォルト値を設定

8系列では一発で列追加出来るのだけど、7系列では若干回りくどい。

PostgreSQL 8.* 以降

ALTER TABLE tab ADD COLUMN new_col integer DEFAULT 0 NOT NULL;

PostgreSQL 7.*系列

ALTER TABLE tab ADD COLUMN new_col integer;
ALTER TABLE tab ALTER COLUMN new_col SET DEFAULT 0;
UPDATE tab SET new_col=0; -- 既存レコードは NULL なので NOT NULL の前にデフォルト値を設定
ALTER TABLE tab ALTER COLUMN new_col SET NOT NULL;
Advertisements
Tagged with:
Posted in database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: