目次
はじめに
CREATE TABLE…LIKE文を使用すると、すでに存在するテーブルのテーブル定義のみをコピーして、新規にテーブルを作成することができます。※CREATE TABLE…AS SELECT文を使用した場合は、データも含めてコピーされますが、CREATE TABLE…LIKE文の場合はテーブル定義のみがコピーされます。
CREATE TABLE…LIKE文
構文
以下にCREATE TABLE…LIKE文の使用方法を記載します。
1 |
CREATE TABLE <新規作成テーブル名> LIKE <ソーステーブル名>; |
実行例
table1の定義をコピーしてtable2を作成する場合の例です。
1 |
CREATE TABLE table2 LIKE table1; |
table2のテーブル定義を確認すると、table1の定義で作成されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
dbadmin=> \d table2 List of Fields by Tables Schema | Table | Column | Type | Size | Default | Not Null | Primary Key | Foreign Key --------+--------+--------+-------------+------+---------+----------+-------------+------------- public | table2 | 日付 | date | 8 | | t | f | public | table2 | 顧客ID | int | 8 | | f | f | public | table2 | 店舗 | varchar(10) | 10 | | f | f | public | table2 | エリア | varchar(10) | 10 | | f | f | public | table2 | 売上高 | int | 8 | | f | f | (5 rows) dbadmin=> \d table1 List of Fields by Tables Schema | Table | Column | Type | Size | Default | Not Null | Primary Key | Foreign Key --------+--------+--------+-------------+------+---------+----------+-------------+------------- public | table1 | 日付 | date | 8 | | t | f | public | table1 | 顧客ID | int | 8 | | f | f | public | table1 | 店舗 | varchar(10) | 10 | | f | f | public | table1 | エリア | varchar(10) | 10 | | f | f | public | table1 | 売上高 | int | 8 | | f | f | (5 rows) |
プロジェクション定義のコピー
INCLUDING PROJECTIONSオプションを使用することで、ソーステーブルのプロジェクション定義(クエリスペシフィックプロジェクションも含む)も合わせてコピーすることができます。
1 |
CREATE TABLE table2 LIKE table1 INCLUDING PROJECTIONS; |
プロジェクションの命名ルール
9.3からINCLUDING PROJECTIONSで作成したテーブル名が、プロジェクション名に反映されるようになりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#### 9.2 以前 #### dbadmin=> CREATE PROJECTION tableT100_v1 AS SELECT * FROM tableT100; CREATE PROJECTION dbadmin=> CREATE TABLE newtableT100 LIKE tableT100 INCLUDING PROJECTIONS; CREATE TABLE dbadmin=> SELECT projection_name,projection_basename,anchor_table_name FROM projections; projection_name | projection_basename | anchor_table_name --------------------+---------------------+------------------- tableT100_v1_b0 | tableT100_v1 | tableT100 tableT100_v1_b1 | tableT100_v1 | tableT100 newtableT100_v1_b0 | newtableT100_v1 | newtableT100 /* プロジェクション名に「_v1」が付与される */ newtableT100_v1_b1 | newtableT100_v1 | newtableT100 /* プロジェクション名に「_v1」が付与される */ (4 rows) #### 9.3 以降 #### dbadmin=> CREATE PROJECTION tableT100_v1 AS SELECT * FROM tableT100; CREATE PROJECTION dbadmin=> CREATE TABLE newtableT100 LIKE tableT100 INCLUDING PROJECTIONS; CREATE TABLE dbadmin=> SELECT projection_name,projection_basename,anchor_table_name FROM projections; projection_name | projection_basename | anchor_table_name -----------------+---------------------+------------------- tableT100_v1_b0 | tableT100_v1 | tableT100 tableT100_v1_b1 | tableT100_v1 | tableT100 newtableT100_b0 | newtableT100 | newtableT100 /* プロジェクション名に「_v1」が付与されない */ newtableT100_b1 | newtableT100 | newtableT100 /* プロジェクション名に「_v1」が付与されない */ (4 rows) |
参考情報
CREATE TABLEhttps://www.vertica.com/docs/9.3.x/HTML/Content/Authoring/SQLReferenceManual/Statements/CREATETABLE.htm
Projection Naming
https://www.vertica.com/docs/9.3.x/HTML/Content/Authoring/AdministratorsGuide/Projections/ProjectionNaming.htm
検証バージョンについて
この記事の内容はVertica 9.3で確認しています。更新履歴
2016/08/19 本記事を公開2019/12/08 バージョン9.3リリース対応