MySQL:仅使用偶数或奇数自动递增? [英] MySQL: Auto increment with even or odd numbers only?
问题描述
是否有可能会生成自动生成偶数或奇数(跳过相反的数字)的字段?我问的原因是因为我想在两个表之间共享自动增量.其他实现它的方式也受到欢迎.
Is it possible to have auto increment field which generates even or odd numbers (skips the opposite)? The reason I am asking is because I want to share auto increment between two tables. Other ways for achieving it are welcomed.
提前谢谢!
推荐答案
您可以将一个表的自动递增字段与另一个表偏移,即,一个表的ID从1开始,而另一个表的ID从1000000开始(或者其他一些值取决于您的使用方式).
You can offset one table's auto increment field from the other, i.e. one table starts ids from 1 while the other starts from 1000000 (or some other value chosen depending on your usage pattern).
CREATE TABLE table1 (id BIGINT UNSIGNED AUTO_INCREMENT);
CREATE TABLE table2 (id BIGINT UNSIGNED AUTO_INCREMENT) AUTO_INCREMENT = 1000000;
您还可以根据需要选择自动增量列类型. BIGINT UNSIGNED的范围是0..18446744073709551615,应该可以覆盖大多数情况.
You can also choose your autoincrement column type according to your needs. BIGINT UNSIGNED's range is 0..18446744073709551615, which should cover most cases.
这篇关于MySQL:仅使用偶数或奇数自动递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!