将 AUTO INCREMENT 或 IDENTITY 添加到 Vertica 中的现有 table
Add AUTO INCREMENT or IDENTITY to existing table in Vertica
我有一个带有自动递增列的现有 postgresql 数据库,我想将其迁移到 Vertica。在 Vertica 中,我可以创建一个带有 IDENTITY 或 AUTO INCREMENT 约束(或数据类型?)的 table,但我无法将这些约束添加到具有现有数据的列中。类似问题 here 的答案是使用序列的解决方法,这不是一个选项,因为允许重复数字并且可能存在多个节点之间的竞争条件,这由 IDENTITY 实现处理。使用 IDENTITY 列创建 table 也不是一种选择,因为不允许插入。
简而言之,我想在现有数据上有一个 IDENTITY 列,它同时也是主键。
您的问题是您需要为 Vertica 副本中的原始 IDENTITY 列设置相同的值 - 同时能够对您现在可能插入到新 [=49] 中的任何新行执行 AUTOINCREMENT 行为=].
这可以通过让 Vertica 目标代理键不是 IDENTITY,而是作为 INT NOT NULL,但通过手动创建序列的 nextval
方法给它一个 DEFAULT 来实现。
您有此 table 和此数据(可以来自任何源数据库...),其中包含 IDENTITY 列。
-- source (could be from anywhere - why not on my own Vertica sandbox)
CREATE TABLE src (
src_id IDENTITY NOT NULL
, first_name VARCHAR(32)
, last_name VARCHAR(32)
, hire_dt DATE
) UNSEGMENTED ALL NODES
;
INSERT /*+ DIRECT */ INTO src(first_name,last_name,hire_dt)
SELECT 'Ford','Prefect',DATE '2017-02-05'
UNION ALL SELECT 'Svlad','Cjelli',DATE '2017-02-05'
UNION ALL SELECT 'Cynthia','Fitzmelton',DATE '2017-02-05'
UNION ALL SELECT 'Stavro','Mueller',DATE '2017-02-05'
UNION ALL SELECT 'Veet','Voojagig',DATE '2017-02-05'
UNION ALL SELECT 'Trin','Tragula',DATE '2017-02-05'
UNION ALL SELECT 'Zarniwoop','Zarniwoop',DATE '2017-02-05'
UNION ALL SELECT 'Rob','McKenna',DATE '2017-02-05'
UNION ALL SELECT 'The Lajestic Vantrashell','of Lob',DATE '2017-02-05'
UNION ALL SELECT 'Paul Neil Milne','Johnston',DATE '2017-02-05'
UNION ALL SELECT 'Lunkwill','Lunkwill',DATE '2017-02-05'
UNION ALL SELECT 'Arthur','Dent',DATE '2017-02-05'
UNION ALL SELECT 'Zaphod','Beeblebrox',DATE '2017-02-05'
UNION ALL SELECT 'Tricia','McMillan',DATE '2017-02-05'
UNION ALL SELECT 'Prostetnic Vogon','Jeltz',DATE '2017-02-05'
UNION ALL SELECT 'Lionel','Prosser',DATE '2017-02-05'
UNION ALL SELECT 'Karl','Mueller',DATE '2017-02-05'
UNION ALL SELECT 'Hotblack','Desiato',DATE '2017-02-05'
UNION ALL SELECT 'Gogrilla','Mincefriend',DATE '2017-02-05'
UNION ALL SELECT 'Slartibartfast','Slartibartfast',DATE '2017-02-05'
UNION ALL SELECT 'Roosta','Roosta',DATE '2017-02-05'
UNION ALL SELECT 'Eccentrica','Gallumbitis',DATE '2017-02-05'
UNION ALL SELECT 'Pizpot','Gargravarr',DATE '2017-02-05'
UNION ALL SELECT 'Vroomfondel','Vroomfondel',DATE '2017-02-05'
UNION ALL SELECT 'Majikthise','Majikthise',DATE '2017-02-05'
UNION ALL SELECT 'Gengis Temüjin','Khan',DATE '2017-02-05'
UNION ALL SELECT 'Know-Nothing-Bozo','the Non-Wonder Dog',DATE '2017-02-05'
UNION ALL SELECT 'Lazlaar','Lyricon',DATE '2017-02-05'
UNION ALL SELECT 'Lintilla','Lintilla',DATE '2017-02-05'
UNION ALL SELECT 'Fook','Fook',DATE '2017-02-05'
UNION ALL SELECT 'Gag','Halfrunt',DATE '2017-02-05'
UNION ALL SELECT 'Benji','Mouse',DATE '2017-02-05'
UNION ALL SELECT 'Frankie','Mouse',DATE '2017-02-05'
UNION ALL SELECT 'Grunthos','the Flatulent',DATE '2017-02-05'
UNION ALL SELECT 'Wowbagger','The Infinitely Prolonged',DATE '2017-02-05'
UNION ALL SELECT 'Wonko','The Sane',DATE '2017-02-05'
UNION ALL SELECT 'Reg','Nullify',DATE '2017-02-05'
UNION ALL SELECT 'Fenchurch','of Rickmansworth',DATE '2017-02-05'
UNION ALL SELECT 'Oolon','Colluphid',DATE '2017-02-05'
UNION ALL SELECT 'Humma','Kavula',DATE '2017-02-05'
UNION ALL SELECT 'Judiciary','Pag',DATE '2017-02-05'
UNION ALL SELECT 'Max','Quordlepleen',DATE '2017-02-05'
;
从该来源 table,您确定当前发布的最高 IDENTITY 值:
SQL>select max(src_id) from src;
max
42
在目标 Vertica 数据库中,您使用该值创建一个全新的手动序列...
CREATE SEQUENCE seq_copy MINVALUE 43;
您根据给定的输入值(您将在 INSERT ... SELECT 或 COPY 命令中提供)或从新创建的序列的 nextval
:
CREATE TABLE cpy (
cpy_id INT NOT NULL DEFAULT(seq_copy.nextval)
, first_name VARCHAR(32)
, last_name VARCHAR(32)
, hire_dt DATE
) UNSEGMENTED ALL NODES
;
然后,如果您指定所有列(或根本 none)
在插入中...
INSERT INTO cpy (
cpy_id
, first_name
, last_name
, hire_dt
)
SELECT * FROM src;
.. 或复制 ...
COPY cpy (cpy_id,first_name,last_name,hire_dt)
FROM LOCAL 'src.csv' DELIMITER ',' ENCLOSED BY '''' ;
...您使用源中的值。
如果你插入一个新值,你不会触及代理键...
INSERT INTO cpy (
first_name
, last_name
, hire_dt
) VALUES (
'Thor'
, 'son of Odin'
, '2000-01-01'
;
... 将为您填充代理键。
这就是你想要的吗?
马可
我有一个带有自动递增列的现有 postgresql 数据库,我想将其迁移到 Vertica。在 Vertica 中,我可以创建一个带有 IDENTITY 或 AUTO INCREMENT 约束(或数据类型?)的 table,但我无法将这些约束添加到具有现有数据的列中。类似问题 here 的答案是使用序列的解决方法,这不是一个选项,因为允许重复数字并且可能存在多个节点之间的竞争条件,这由 IDENTITY 实现处理。使用 IDENTITY 列创建 table 也不是一种选择,因为不允许插入。
简而言之,我想在现有数据上有一个 IDENTITY 列,它同时也是主键。
您的问题是您需要为 Vertica 副本中的原始 IDENTITY 列设置相同的值 - 同时能够对您现在可能插入到新 [=49] 中的任何新行执行 AUTOINCREMENT 行为=].
这可以通过让 Vertica 目标代理键不是 IDENTITY,而是作为 INT NOT NULL,但通过手动创建序列的 nextval
方法给它一个 DEFAULT 来实现。
您有此 table 和此数据(可以来自任何源数据库...),其中包含 IDENTITY 列。
-- source (could be from anywhere - why not on my own Vertica sandbox)
CREATE TABLE src (
src_id IDENTITY NOT NULL
, first_name VARCHAR(32)
, last_name VARCHAR(32)
, hire_dt DATE
) UNSEGMENTED ALL NODES
;
INSERT /*+ DIRECT */ INTO src(first_name,last_name,hire_dt)
SELECT 'Ford','Prefect',DATE '2017-02-05'
UNION ALL SELECT 'Svlad','Cjelli',DATE '2017-02-05'
UNION ALL SELECT 'Cynthia','Fitzmelton',DATE '2017-02-05'
UNION ALL SELECT 'Stavro','Mueller',DATE '2017-02-05'
UNION ALL SELECT 'Veet','Voojagig',DATE '2017-02-05'
UNION ALL SELECT 'Trin','Tragula',DATE '2017-02-05'
UNION ALL SELECT 'Zarniwoop','Zarniwoop',DATE '2017-02-05'
UNION ALL SELECT 'Rob','McKenna',DATE '2017-02-05'
UNION ALL SELECT 'The Lajestic Vantrashell','of Lob',DATE '2017-02-05'
UNION ALL SELECT 'Paul Neil Milne','Johnston',DATE '2017-02-05'
UNION ALL SELECT 'Lunkwill','Lunkwill',DATE '2017-02-05'
UNION ALL SELECT 'Arthur','Dent',DATE '2017-02-05'
UNION ALL SELECT 'Zaphod','Beeblebrox',DATE '2017-02-05'
UNION ALL SELECT 'Tricia','McMillan',DATE '2017-02-05'
UNION ALL SELECT 'Prostetnic Vogon','Jeltz',DATE '2017-02-05'
UNION ALL SELECT 'Lionel','Prosser',DATE '2017-02-05'
UNION ALL SELECT 'Karl','Mueller',DATE '2017-02-05'
UNION ALL SELECT 'Hotblack','Desiato',DATE '2017-02-05'
UNION ALL SELECT 'Gogrilla','Mincefriend',DATE '2017-02-05'
UNION ALL SELECT 'Slartibartfast','Slartibartfast',DATE '2017-02-05'
UNION ALL SELECT 'Roosta','Roosta',DATE '2017-02-05'
UNION ALL SELECT 'Eccentrica','Gallumbitis',DATE '2017-02-05'
UNION ALL SELECT 'Pizpot','Gargravarr',DATE '2017-02-05'
UNION ALL SELECT 'Vroomfondel','Vroomfondel',DATE '2017-02-05'
UNION ALL SELECT 'Majikthise','Majikthise',DATE '2017-02-05'
UNION ALL SELECT 'Gengis Temüjin','Khan',DATE '2017-02-05'
UNION ALL SELECT 'Know-Nothing-Bozo','the Non-Wonder Dog',DATE '2017-02-05'
UNION ALL SELECT 'Lazlaar','Lyricon',DATE '2017-02-05'
UNION ALL SELECT 'Lintilla','Lintilla',DATE '2017-02-05'
UNION ALL SELECT 'Fook','Fook',DATE '2017-02-05'
UNION ALL SELECT 'Gag','Halfrunt',DATE '2017-02-05'
UNION ALL SELECT 'Benji','Mouse',DATE '2017-02-05'
UNION ALL SELECT 'Frankie','Mouse',DATE '2017-02-05'
UNION ALL SELECT 'Grunthos','the Flatulent',DATE '2017-02-05'
UNION ALL SELECT 'Wowbagger','The Infinitely Prolonged',DATE '2017-02-05'
UNION ALL SELECT 'Wonko','The Sane',DATE '2017-02-05'
UNION ALL SELECT 'Reg','Nullify',DATE '2017-02-05'
UNION ALL SELECT 'Fenchurch','of Rickmansworth',DATE '2017-02-05'
UNION ALL SELECT 'Oolon','Colluphid',DATE '2017-02-05'
UNION ALL SELECT 'Humma','Kavula',DATE '2017-02-05'
UNION ALL SELECT 'Judiciary','Pag',DATE '2017-02-05'
UNION ALL SELECT 'Max','Quordlepleen',DATE '2017-02-05'
;
从该来源 table,您确定当前发布的最高 IDENTITY 值:
SQL>select max(src_id) from src;
max
42
在目标 Vertica 数据库中,您使用该值创建一个全新的手动序列...
CREATE SEQUENCE seq_copy MINVALUE 43;
您根据给定的输入值(您将在 INSERT ... SELECT 或 COPY 命令中提供)或从新创建的序列的 nextval
:
CREATE TABLE cpy (
cpy_id INT NOT NULL DEFAULT(seq_copy.nextval)
, first_name VARCHAR(32)
, last_name VARCHAR(32)
, hire_dt DATE
) UNSEGMENTED ALL NODES
;
然后,如果您指定所有列(或根本 none)
在插入中...
INSERT INTO cpy (
cpy_id
, first_name
, last_name
, hire_dt
)
SELECT * FROM src;
.. 或复制 ...
COPY cpy (cpy_id,first_name,last_name,hire_dt)
FROM LOCAL 'src.csv' DELIMITER ',' ENCLOSED BY '''' ;
...您使用源中的值。
如果你插入一个新值,你不会触及代理键...
INSERT INTO cpy (
first_name
, last_name
, hire_dt
) VALUES (
'Thor'
, 'son of Odin'
, '2000-01-01'
;
... 将为您填充代理键。
这就是你想要的吗?
马可