Hello, I am trying to sync between 2 tables: I have active table where has auto_increment, and I have archive table with the same values. I would like both ID's to be unique (between the tables as well) - I mean, I would like to save auto incremenet, and if I UNION both table I still have uniqness. How can I do that? Is there a possibility to save auto increment when mysql is off?
First off, this sounds like a bad idea - you shouldn't have an auto_increment
in your archive table, since all the data is presumably copied directly from the live table (including its IDs).
However, here are some hacky solutions:
You can change the current
AUTO_INCREMENT
of a table using:ALTER TABLE tbl_name AUTO_INCREMENT = some_new_value
Set it to a large offset for one of the tables, and you'll be safe for a while. Except when you reach the offset, having forgot this little hack, the whole house of cards will fall on your head.
You can simply add an offset by a constant when selecting (e.g.
SELECT id + 1000000 AS id
) in one of the tables. Still a hack, but at least it's closer to the surface, and when you eventually reach an overlapping area, it's easier to fix.Finally, you can select the maximum ID from one table, then offset all the IDs in the other table by this value during the same select. Make sure you have a large enough datatype for the ID though.