「PostgreSQL」uniqueインデックスを作成する方法

2021年9月6日

構文
create unique index on スキーマ.テーブル名(カラム名)
使用例
1.テーブルの作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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インデックスを作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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.インデックスを確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)

 

PostgreSQL

Posted by arkgame