2016-12-15 2 views
-1

Ich möchte zwei Tabellen mit den gleichen Spalten in Postgresql zusammenführen. Tatsächlich habe ich bereits eine Tabelle in Benutzung und möchte den Inhalt eines Speicherauszugs von einem anderen Server anfügen, um die Tabellen zusammenzuführen. Beide Tabellen haben dieselben Spalten, einschließlich des Primärschlüssels ... Wenn ich also versuche, den Dump zu laden, erhalte ich doppelte Primärschlüsselfehler. Ich habe versucht, den Speicherauszug in eine separate Tabelle zu laden und den Inhalt dieser neuen Tabelle in den ersten mit einem INSERT/SELECT einzufügen, aber das Primärschlüsselfeld erzeugt Fehler wegen der Duplikate, und ich kann es nicht weglassen einfügen (oder eine "not-null constraint" -Verletzung erhalten).Postgresql: fügen Sie den Inhalt einer Tabelle an eine andere Tabelle an

Was wäre die Syntax (oder Lösung), um den Inhalt der zweiten Tabelle (oder ihres Dumps) am Ende des ersten mit korrekten Primärschlüsseln anzufügen?

Kurz gesagt, mit einem Beispiel möchte ich mag:

# 
# 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); 

Die Eingänge sind die Deponien von t_table1 und t_table2, und würden gerne t_merge bekommen.

+0

Was der „richtige“ Primärschlüssel ist? Wie werden die Schlüssel für die erste Tabelle normalerweise generiert? –

+0

Haben Sie ein automatisches Feld als Primärschlüssel? Wenn Sie Tabelle A mit 'pkField_id = 1 'und Tabelle B auch mit' pkField_id = 1' haben, welches Ergebnis erwarten Sie? –

+0

Sie stellen wahrscheinlich die falsche Frage [** Was ist das XY-Problem? **] (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) weil beide Tabellen Haben Sie dieselbe ID, verletzen Sie PK. Sie müssen also ein anderes Feld erstellen, um PK zu sein. Oder entscheiden Sie, was ein praktikabler Workaround ist. –

Antwort

0

OK, so dass nach einigen Versuchen und Forschung, ist es die Folge, dies zu tun scheint, ist:

-- 
-- 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); 

-- 
-- Create a temp table: 
-- 
CREATE TABLE t_table3 (
    f_fullcmd text, 
    f_id serial NOT NULL, 
    f_body text 
); 

SELECT setval('t_table3_f_id_seq', (SELECT MAX(f_id)+1 FROM t_table1), false); 

-- 
-- Merge table2 into table3 
-- 
INSERT INTO t_table3 (f_fullcmd, f_body) SELECT f_fullcmd, f_body FROM t_table2; 

-- 
-- Merge back into table1: 
-- 
INSERT INTO t_table1 (f_fullcmd, f_id, f_body) SELECT f_fullcmd, f_id, f_body FROM t_table3; 

Wenn t_table3 erstellen, den Primärschlüssel ändern, um eine Sequenz zu sein, setzen Sie seinen Startwert bis zum max Wert des ersten Tabellenprimärschlüssels +1, Einfügen des Inhalts von t_table2 in t_table3 (der Primärschlüssel wird als Sequenz neu generiert) und schließlich den Inhalt von t_table3 in t_table1 zurückimportieren.

0

Wenn beide Tabelle bereits autonumeric ist, statt eine zeitliche Tabelle erstellen, ist dies einfacher machen:

INSERT INTO t_table1 (f_fullcmd, f_id, f_body) 
SELECT f_fullcmd, 
     f_id + x.t1_max, 
     f_body 
FROM t_table2 
CROSS JOIN (SELECT MAX(f_id) t1_max FROM t_table1) x ; 
Verwandte Themen