我想合并两个表PostgreSQL中相同的列。实际上,我已经有一个表正在使用,并希望追加来自另一台服务器的转储内容以合并这些表。这两个表都有相同的列,包括主键...所以当我尝试加载转储时,我得到重复的主键错误。 我试图转储加载到一个单独的表,并插入这个新表的内容到第一个与INSERT/SELECT,但主键字段生成,因为重复的错误,我不能在省略它插入(或获得“非空约束”违规)。PostgreSQL的:一个表的内容追加到另一个表
会是什么语法(或溶液)到所述第二表中的第一个的端部的内容(或它的转储)追加,以正确的主键?
总之,有一个例子,我想:
#
# First table:
#
CREATE TABLE t_table1 (
f_fullcmd text,
f_id integer NOT NULL,
f_body text
);
INSERT INTO t_table1 VALUES ('command1', 1, 'This is my first command');
INSERT INTO t_table1 VALUES ('command2', 2, 'This is my second command');
INSERT INTO t_table1 VALUES ('command3', 3, 'This is my third command');
ALTER TABLE ONLY t_table1 ADD CONSTRAINT pk_1_t_table1 PRIMARY KEY (f_id);
#
# Second table to append to t_table1
#
CREATE TABLE t_table2 (
f_fullcmd text,
f_id integer NOT NULL,
f_body text
);
INSERT INTO t_table2 VALUES ('run-1', 1, 'This is my first run');
INSERT INTO t_table2 VALUES ('run-2', 2, 'This is my second run');
INSERT INTO t_table2 VALUES ('run-3', 3, 'This is my third run');
ALTER TABLE ONLY t_table2 ADD CONSTRAINT pk_1_t_table2 PRIMARY KEY (f_id);
#
# Resulting table:
#
CREATE TABLE t_merge (
f_fullcmd text,
f_id integer NOT NULL,
f_body text
);
INSERT INTO t_merge VALUES ('command1', 1, 'This is my first command');
INSERT INTO t_merge VALUES ('command2', 2, 'This is my second command');
INSERT INTO t_merge VALUES ('command3', 3, 'This is my third command');
INSERT INTO t_merge VALUES ('run-1', 4, 'This is my first run');
INSERT INTO t_merge VALUES ('run-2', 5, 'This is my second run');
INSERT INTO t_merge VALUES ('run-3', 6, 'This is my third run');
ALTER TABLE ONLY t_merge ADD CONSTRAINT pk_1_t_merge PRIMARY KEY (f_id);
的输入是t_table1和t_table2的转储,并希望得到t_merge。
什么是“正确”的主键?通常如何生成第一个表的键? –
你有一个自动数字字段是主键吗?如果你的表A有'pkField_id = 1',表B也有'pkField_id = 1',你期望得到什么结果? –
你可能会问错了问题[**什么是XY问题?**](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) ,因为如果两个表有相同的ID,你会违反PK。所以你需要创建一个不同的领域成为PK。或者决定什么是可行的解决方法。 –