复制表
可以使用带有 LIKE
子句的
CREATE TABLE
从现有表创建一个表:
CREATE TABLE [schema.]table-name LIKE [schema.]existing-table
[ {INCLUDING | EXCLUDING} PROJECTIONS ]
[ {INCLUDE | EXCLUDE} [SCHEMA] PRIVILEGES ]
使用 LIKE
创建表会复制源表定义以及与其关联的任何存储策略。它不会复制列上的表数据或表达式。
复制约束
CREATE TABLE...LIKE
会复制所有表约束,但以下情况除外:
-
外键约束
-
从序列获取值的任何列,包括
IDENTITY
和AUTO_INCREMENT
列。Vertica 会将列值复制到新表中,但会移除原始约束。例如,以下表定义对列ID
设置IDENTITY
约束:CREATE TABLE public.Premium_Customer ( ID IDENTITY , lname varchar(25), fname varchar(25), store_membership_card int );
以下
CREATE TABLE...LIKE
语句将此表复制为All_Customers
。Vertica 会从All_Customers.ID
中移除IDENTITY
约束,从而将其更改为具有NOT NULL
约束的整数列:=> CREATE TABLE All_Customers like Premium_Customer; CREATE TABLE => select export_tables('','All_Customers'); export_tables --------------------------------------------------- CREATE TABLE public.All_Customers ( ID int NOT NULL, lname varchar(25), fname varchar(25), store_membership_card int ); (1 row)
包括投影
可以使用 INCLUDING PROJECTIONS
或 EXCLUDING PROJECTIONS
限定 LIKE
子句,指定是否从源表中复制投影:
-
EXCLUDING PROJECTIONS
(默认值):不从源表复制投影。 -
INCLUDING PROJECTIONS
:从源表复制当前投影。Vertica 会根据 Vertica 命名约定命名新投影,以避免与现有对象发生名称冲突。
包括架构权限
可以为新表指定架构权限的默认继承:
-
EXCLUDE [SCHEMA] PRIVILEGES
(默认)禁用从架构继承权限 -
INCLUDE [SCHEMA] PRIVILEGES
为表授予向其架构授予的相同权限
有关详细信息,请参阅设置表和视图的权限继承。
限制
对于源表存在以下限制:
-
不能具有过期投影。
-
不能为临时表。
示例
-
创建表
states
:=> CREATE TABLE states ( state char(2) NOT NULL, bird varchar(20), tree varchar (20), tax float, stateDate char (20)) PARTITION BY state;
-
向表中填充数据:
INSERT INTO states VALUES ('MA', 'chickadee', 'american_elm', 5.675, '07-04-1620'); INSERT INTO states VALUES ('VT', 'Hermit_Thrasher', 'Sugar_Maple', 6.0, '07-04-1610'); INSERT INTO states VALUES ('NH', 'Purple_Finch', 'White_Birch', 0, '07-04-1615'); INSERT INTO states VALUES ('ME', 'Black_Cap_Chickadee', 'Pine_Tree', 5, '07-04-1615'); INSERT INTO states VALUES ('CT', 'American_Robin', 'White_Oak', 6.35, '07-04-1618'); INSERT INTO states VALUES ('RI', 'Rhode_Island_Red', 'Red_Maple', 5, '07-04-1619');
-
查看表内容:
=> SELECT * FROM states; state | bird | tree | tax | stateDate -------+---------------------+--------------+-------+---------------------- VT | Hermit_Thrasher | Sugar_Maple | 6 | 07-04-1610 CT | American_Robin | White_Oak | 6.35 | 07-04-1618 RI | Rhode_Island_Red | Red_Maple | 5 | 07-04-1619 MA | chickadee | american_elm | 5.675 | 07-04-1620 NH | Purple_Finch | White_Birch | 0 | 07-04-1615 ME | Black_Cap_Chickadee | Pine_Tree | 5 | 07-04-1615 (6 rows
-
创建示例投影并刷新:
=> CREATE PROJECTION states_p AS SELECT state FROM states; => SELECT START_REFRESH();
-
创建一个类似于
states
表的表并包括其投影:=> CREATE TABLE newstates LIKE states INCLUDING PROJECTIONS;
-
查看这两个表的投影。Vertica 已将投影从
states
复制到newstates
:=> \dj List of projections Schema | Name | Owner | Node | Comment -------------------------------+-------------------------------------------+---------+------------------+--------- public | newstates_b0 | dbadmin | | public | newstates_b1 | dbadmin | | public | newstates_p_b0 | dbadmin | | public | newstates_p_b1 | dbadmin | | public | states_b0 | dbadmin | | public | states_b1 | dbadmin | | public | states_p_b0 | dbadmin | | public | states_p_b1 | dbadmin | |
-
查看表
newstates
,它显示从states
复制的列:=> SELECT * FROM newstates; state | bird | tree | tax | stateDate -------+------+------+-----+----------- (0 rows)
使用 CREATE TABLE...LIKE
语句时,系统也会复制与表关联的存储策略对象。添加到新表的数据会使用与源表相同的标记存储位置,除非您更改了存储策略。有关详细信息,请参阅处理存储位置。