「PostgreSQL」uniqueインデックスを作成する方法
構文
create unique index on スキーマ.テーブル名(カラム名)
使用例
1.テーブルの作成
postgres=# create table cftschema.ktbl (uid integer,uname varchar(20),memo varchar(50));
CREATE TABLE
postgres=# create table cftschema.ktbl (uid integer,uname varchar(20),memo varchar(50));
CREATE TABLE
postgres=# create table cftschema.ktbl (uid integer,uname varchar(20),memo varchar(50)); CREATE TABLE
2.uniqueインデックスを作成
postgres=# create unique index on cftschema.ktbl(uname);
CREATE INDEX
postgres=# create unique index on cftschema.ktbl(uname);
CREATE INDEX
postgres=# create unique index on cftschema.ktbl(uname); CREATE INDEX
3.インデックスを確認
postgres=# \d cftschema.ktbl
テーブル "cftschema.ktbl"
列 | 型 | 修飾語
-------+-----------------------+--------
uid | integer |
uname | character varying(20) |
memo | character varying(50) |
インデックス:
"ktbl_uname_idx" UNIQUE, btree (uname)
postgres=# \d cftschema.ktbl
テーブル "cftschema.ktbl"
列 | 型 | 修飾語
-------+-----------------------+--------
uid | integer |
uname | character varying(20) |
memo | character varying(50) |
インデックス:
"ktbl_uname_idx" UNIQUE, btree (uname)
postgres=# \d cftschema.ktbl テーブル "cftschema.ktbl" 列 | 型 | 修飾語 -------+-----------------------+-------- uid | integer | uname | character varying(20) | memo | character varying(50) | インデックス: "ktbl_uname_idx" UNIQUE, btree (uname)