Merging tables in mysql
February 2, 2009 11:36 AM
Subscribe
How do I merge three tables in mysql with duplicate entries for the primary key?
I want to merge tables 1,2, and 3.
Each one has a field called ID (the primary key) which is set to auto_increment.
I want to merge all three tables into one. However, I cannot merge because mysql coughs up a duplicate entry on the primary key.
I'm guessing that the solution would be to insert a new field into table two that is Total number of records in table 1 + ID (and then do a similar thing with table 3). After that I would delete the ID field and make the new field the primary key. How do I do this?
Is there an easier way?
posted by special-k to computers & internet (5 comments total)
2 users marked this as a favorite
create table foo (every, column, except, IDfield)
insert into foo ( every, column, except, idfield)
select every, column, except, idfield from
(
select ( every, column, except, IDfield) from table1 UNION ALL
select (every, column, except, idfield ) from table2 UNION ALL
select(every, column, except, idfield) from table3
) a
2.) add a new int identity column to the new table:
ALTER TABLE table_name ADD COLUMN id INTEGER AUTO_INCREMENT UNIQUE KEY
posted by spatula at 11:44 AM on February 2, 2009