MySQL-如何实现table奇增一table偶增

MySQL-How to achieve a table odd increase one table even increase

我有两个table,一个叫user1,一个叫user2。

我想要 table user1 union table user2 的 id 是唯一的。

所以我打算把一个table的ID设为奇数,另一个设为偶数。

如何实现?

您可以使用 auto_increment_incrementauto_increment_offset 将值分成两个 tables.Hope 以下 link 将帮助您..

https://dev.mysql.com/doc/refman/8.0/en/replication-options-master.html#sysvar_auto_increment_increment

现在明白了,我的情况auto-increment是做不到的

引自mysql-auto-increasement

the next value inserted is the least value in the series that is greater than the maximum existing value in the AUTO_INCREMENT column. The series is calculated like this: auto_increment_offset + N × auto_increment_increment

最后,我在代码中控制了 primary key(id) 而不是使用 auto-increment