WEBの勉強ノート
Loading

PostgreSQL 主キーの設定方法

2009 年 5 月 27 日 カテゴリー: PostgreSQL

テーブル作成時に主キーを設定

列名、データ型につづけて、PRIMARY KEY を加えます。

test=> create table test2
test-> (num int primary key, name varchar(50));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test2_pkey" for table "test2"
CREATE TABLE

作成済みのテーブルに主キーを設定

作成済みのテーブルに対して、主キーを設定する方法です。

test=> alter table test add primary key (num);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_pkey" for table "test"
ALTER TABLE

コメントをどうぞ