[英]MySQL - AUTO_INCREMENT, custom increment start and value for a table
Is there a way to specify a custom auto increment starting value and a custom auto increment increment value for only one MySQL table? 有没有一种方法可以仅针对一个MySQL表指定自定义自动增量起始值和自定义自动增量增量值?
I couldn't find a clear answer to my question. 我找不到我的问题的明确答案。 There seem to be two global variables auto_increment_increment and auto_increment_offset but I need it for one table only.
似乎有两个全局变量auto_increment_increment和auto_increment_offset,但是我只需要一个表。 Another site suggested using
AUTO_INCREMENT(<offset>,<increment>)
but this approach just triggered MySQL errors. 另一个站点建议使用
AUTO_INCREMENT(<offset>,<increment>)
但是这种方法只是触发了MySQL错误。
So is there a way to do this? 那么有没有办法做到这一点?
You can't change the step on a per table basis in MySQL. 在MySQL中,您无法按表更改步骤。 Running
SET @@auto_increment_increment=2;
运行
SET @@auto_increment_increment=2;
or changing the startup value will affect all tables and databases on the server. 或更改启动值将影响服务器上的所有表和数据库。
As far as I can tell you have two options: 据我所知,您有两种选择:
You could consider a trigger that performs the maths you want and sets a custom value to another field. 您可以考虑一个触发器,该触发器执行所需的数学运算并将自定义值设置为另一个字段。
CREATE TRIGGER trigger
BEFORE INSERT ON my_table
FOR EACH ROW BEGIN 在对每个行开始在
my_table
插入之前创建触发trigger
SET NEW.id2 = NEW.id * 2; SET NEW.id2 = NEW.id * 2;
END 结束
You can execute this SQL, say if you wanted to start it at 3: 您可以执行此SQL,说出是否要从3开始:
ALTER TABLE foo AUTO_INCREMENT = 3;
As others pointed out, I don't believe you can do both, however. 正如其他人指出的那样,我不相信您可以同时做到。
You can change the current value to be applied to the next inserted row via: 您可以通过以下方式更改要应用于下一个插入行的当前值:
ALTER TABLE table AUTO_INCREMENT = ?;
The increment value can be set via the auto_increment_increment
server variable. 可以通过
auto_increment_increment
服务器变量来设置增量值。
Unfortunately this will effect all databases/tables on the server if set in Global context. 不幸的是,如果在全局上下文中设置,这将影响服务器上的所有数据库/表。
You can set this variable on a session-specific basis. 您可以在特定于会话的基础上设置此变量。 So, if you wanted to use a specific set of DB connections, to which you set the value on a session basis, that is possible.
因此,如果您要使用一组特定的数据库连接,并以会话为基础设置值,则可以。 This might be a bit unwieldy to manage as you would, in essence, need a distinct connection (or pool of connections) just to update this single table.
从本质上讲,只需要一个唯一的连接(或连接池)来更新此单个表,就可能很难管理。
I have found the way to change auto_increment, just alter table. 我找到了更改auto_increment的方法,只需更改表即可。 I have to the problem when migrate old database to new system (from Heroku to Amazon), but have to keep all old data (not change id), but every day a hundred thousand happen, so how can I keep data and duplicate data do not happen, what I did:
在将旧数据库迁移到新系统(从Heroku到Amazon)时,我不得不面对这个问题,但是必须保留所有旧数据(不更改ID),但是每天都有十万次发生,因此如何保存数据和重复数据呢?没发生,我做了什么:
Ex: 例如:
- Create table:
CREATE TABLE `test`.`table_inc` (
`idtable_inc_id` INT NOT NULL AUTO_INCREMENT,
`table_inccol` VARCHAR(45) NULL,
PRIMARY KEY (`idtable_inc_id`));
- Alter table (to remove auto increment)
ALTER TABLE `test`.`table_inc`
CHANGE COLUMN `idtable_inc_id` `idtable_inc_id` INT(11) NOT NULL ;
I have found another answer for this solution 我找到了该解决方案的另一个答案
> mysqldump -u root -p database_heroku | sed 's/ AUTO_INCREMENT=[0-9]*\b/ AUTO_INCREMENT=1100001/' > /new_increment_with_data_heroku_database.sql
Ref: http://melikedev.com/2011/06/01/mysql-remove-auto_increment-from-schema-dumps-mysqldump/ 参考: http : //melikedev.com/2011/06/01/mysql-remove-auto_increment-from-schema-dumps-mysqldump/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.