OpenText Analytics Database 旧Vertica

技術情報サイト

Analytics Database

既存テーブルのテーブル定義のみをコピーして、新規にテーブルを作成する方法

公開日:
更新日:
基本操作
#テーブル

はじめに

CREATE TABLE…LIKE文を使用すると、すでに存在するテーブルのテーブル定義のみをコピーして、新規にテーブルを作成することができます。

※CREATE TABLE…AS SELECT文を使用した場合は、データも含めてコピーされますが、CREATE TABLE…LIKE文の場合はテーブル定義のみがコピーされます。

CREATE TABLE…LIKE文

構文

以下にCREATE TABLE…LIKE文の使用方法を記載します。

CREATE TABLE <新規作成テーブル名> LIKE <ソーステーブル名>;

実行例

table1の定義をコピーしてtable2を作成する場合の例です。

CREATE TABLE table2 LIKE table1;

table2のテーブル定義を確認すると、table1の定義で作成されています。

=> \d table2
                                    List of Fields by Tables
 Schema | Table  | Column |    Type     | Size | Default | Not Null | Primary Key | Foreign Key
--------+--------+--------+-------------+------+---------+----------+-------------+-------------
 public | table2 | 日付   | date        |    8 |         | f        | 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)

=> \d table1
                                    List of Fields by Tables
 Schema | Table  | Column |    Type     | Size | Default | Not Null | Primary Key | Foreign Key
--------+--------+--------+-------------+------+---------+----------+-------------+-------------
 public | table1 | 日付   | date        |    8 |         | f        | 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)

参考情報

CREATE TABLE
https://docs.vertica.com/23.4.x/en/sql-reference/statements/create-statements/create-table/

検証バージョンについて

この記事の内容はVertica 23.4で確認しています。

更新履歴

2023/12/21 Vertica23.4用に検証バージョンを修正
2019/12/08 バージョン9.3リリース対応
2016/08/19 本記事を公開