「PostgreSQL」CREATE INDEXで新しいインデックスを作成する

環境
PostgreSQL 9.6.5
Windows 10 Home 64bit

書式
CREATE INDEX [ name ] ON [ ONLY ] table_name
( column_name [, …] )
テーブル( table_name )のカラム( column_name )を対象としたインデックスを作成します。

使用例
1.okschemaスキーマの中に次のようなテーブルを作成します
SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test2db=# create table okschema.stinfotbl (stid integer,stname varchar(10),staddr varchar(10));
CREATE TABLE
test2db=# create table okschema.stinfotbl (stid integer,stname varchar(10),staddr varchar(10)); CREATE TABLE
test2db=# create table okschema.stinfotbl (stid integer,stname varchar(10),staddr varchar(10));
CREATE TABLE

2.作成したテーブルのstnameカラムを対象としてインデックスを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test2db=# create index on okschema.stinfotbl(stname);
CREATE INDEX
test2db=# create index on okschema.stinfotbl(stname); CREATE INDEX
test2db=# create index on okschema.stinfotbl(stname);
CREATE INDEX

3. psql コマンドの ¥d コマンドを使ってテーブルのカラムの情報を取得します
書式
\d スキーマ名.テーブル名

操作例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test2db=# \d okschema.stinfotbl
テーブル"okschema.stinfotbl"
| タイプ | 照合順序 | Null 値を許容 | デフォルト
--------+-----------------------+----------+---------------+------------
stid | integer | | |
stname | character varying(10) | | |
staddr | character varying(10) | | |
インデックス:
"stinfotbl_stname_idx" btree (stname)
test2db=# \d okschema.stinfotbl テーブル"okschema.stinfotbl" 列 | タイプ | 照合順序 | Null 値を許容 | デフォルト --------+-----------------------+----------+---------------+------------ stid | integer | | | stname | character varying(10) | | | staddr | character varying(10) | | | インデックス: "stinfotbl_stname_idx" btree (stname)
test2db=# \d okschema.stinfotbl
                      テーブル"okschema.stinfotbl"
   列   |        タイプ         | 照合順序 | Null 値を許容 | デフォルト
--------+-----------------------+----------+---------------+------------
 stid   | integer               |          |               |
 stname | character varying(10) |          |               |
 staddr | character varying(10) |          |               |
インデックス:
    "stinfotbl_stname_idx" btree (stname)

 

PostgreSQL

Posted by arkgame