我有一个带有自动递增列的postgresql数据库,我想迁移到Vertica。在Vertica中,我可以使用IDENTITY或AUTO INCREMENT约束(或数据类型?)创建一个表,但我无法将这些约束添加到包含现有数据的列。类似问题here的答案是使用序列的解决方法,这不是一个选项,因为允许重复的数字,并且可能存在多个节点之间的竞争条件,这由IDENTITY实现处理。使用IDENTITY列创建表也不是一种选择,因为不允许插入。
简而言之,我希望在现有数据上有一个IDENTITY列,同时也是主键。
答案 0 :(得分:1)
您的问题是,您需要为Vertica中的副本中的原始IDENTITY列提供相同的值 - 同时能够对现在可能插入新表的任何新行具有AUTOINCREMENT行为。
这可以通过将Vertica目标代理键不是IDENTITY,而是作为INT NOT NULL,但是从手动创建的序列的nextval
方法给它一个DEFAULT来实现。
你有这个表,这个数据(可以来自任何源数据库......),它运行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'
;
从该源表中,您可以确定当前发布的最高IDENTITY值:
SQL>select max(src_id) from src;
max
42
在目标Vertica数据库中,您使用该值创建全新的手动序列...
CREATE SEQUENCE seq_copy MINVALUE 43;
使用代理ID创建目标表,具体取决于给定的输入值(您将在INSERT ... SELECT或COPY命令中提供)或新创建的序列{{1 }}:
nextval
然后,如果您指定所有列(或根本没有)
在INSERT中......
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
;
..或COPY ......
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 '''' ;
...并且将为您填充代理键。
这是你之后的事吗?
马