简体   繁体   English

在MySQL 5.0中,是否可以在不更新auto_increment值的情况下插入带有auto_increment列的表中?

[英]In MySQL 5.0, can one insert into a table with an auto_increment column without updating the auto_increment value?

I have a MySQL database, containing a table with an auto_increment primary key. 我有一个MySQL数据库,其中包含带有auto_increment主键的表。

Consider the following definition as sufficient for this example: 考虑以下定义足以满足此示例要求:

create table test(id integer not null auto_increment primary key) engine=innodb;

Now, typically I would insert into this table using the following syntax: 现在,通常我将使用以下语法将此表插入:

insert into test() values();

This would appropriately update the auto_increment value, and that is fine. 这将适当地更新auto_increment值,这很好。

However, I would like to insert a high value into this table (say, 1000000) but not have it update the auto_increment ID. 但是,我想在此表中插入一个高值(例如1000000),但希望它更新auto_increment ID。

Is it possible to insert high values into this table in such a way as to not have an effect on the value of the auto_increment ID? 是否可以以不影响auto_increment ID的值的方式将高值插入该表中?

I know this is a terrible practice, but it would significantly simplify some things I have to do. 我知道这是一个可怕的做法,但是它将大大简化我必须做的一些事情。

Thanks in advance. 提前致谢。

MySQL 5.0 lets you insert an arbitrary value into an auto increment field, but this does alter the next value used for auto incrementing. MySQL 5.0允许您在自动递增字段中插入一个任意值,但这会改变用于自动递增的下一个值。 And although you can alter the "next number" an auto increment field will use (with ALTER TABLE t AUTO_INCREMENT = number ), this has to be greater than the maximum value currently in that column. 而且,尽管您可以更改“下一个数字”,但将使用自动递增字段( ALTER TABLE t AUTO_INCREMENT = number ),但是该字段必须大于该列中当前的最大值。

So, no, it doesn't appear you can achieve what you asked. 因此,不,您似乎并没有达到您的要求。

You can just give your row the id you like. 您可以给行添加自己喜欢的ID。 For example, given the following table definition: 例如,给定下表定义:

CREATE TABLE IF NOT EXISTS `stack-test` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(255),
  PRIMARY KEY  (`id`)
);

The following query will insert a row with a automatically generated id: 以下查询将插入具有自动生成的ID的行:

INSERT INTO `stack-test` (name) VALUES ('Peter');

And this query will insert a row with a user-defined id: 并且此查询将插入具有用户定义的ID的行:

INSERT INTO `stack-test` (id, name) VALUES (5, 'Joe');

Verify by querying all rows: 通过查询所有行进行验证:

SELECT * FROM `stack-test`;

Output: 输出:

+----+-------+
| id | name  |
+----+-------+
|  1 | peter |
|  5 | Joe   |
+----+-------+

Update: Just read, that you don't want to influence the AUTO_INCREMENT value. 更新:请阅读,您不想影响AUTO_INCREMENT值。 As staticsan mentioned, the AUTO_INCREMENT field automatically uses the highest value plus one. 如staticsan所述, AUTO_INCREMENT字段自动使用最大值加1。 There is a statement which resets the counter to a certain value, but this only works, if there is no higher value present in the table: 有一条语句会将计数器重置为某个值,但这仅在表中没有更高的值时才有效:

ALTER TABLE `stack-test` AUTO_INCREMENT = 2;

I had a similar issue and though I like the answer above I resolved it by using two tables. 我有一个类似的问题,尽管我喜欢上面的答案,但我还是通过使用两个表解决了这个问题。 One for high id's and the other for low ones. 一个用于高ID,另一个用于低ID。 It suited my particular situation quite well fwiw. 很适合我的特殊情况。

How about reserving some initial values instead of final ones ? 如何保留一些初始值而不是最终值? I mean, keep ids, say less than 1000 as reserved. 我的意思是,保留ID,例如保留少于1000个。 Make auto increment seed as 1001 so that new inserts will have value after that and use IDENTITY INSERT to add values in reserved range. 将自动增量种子设为1001,以便新插入的值在此之后具有值,并使用IDENTITY INSERT在保留范围内添加值。

Hope it helps. 希望能帮助到你。

我不这么认为-自动递增字段必须是主键,因此不为null。

This is not possible. 这是不可能的。 To have things behave the way you're suggesting would introduce a duplicate auto_increment value when the counter eventually does get up to that very high number you've inserted. 为了使事情表现出您建议的方式,当计数器最终确实达到您所插入的非常高的数字时,将引入一个重复的auto_increment值。

sorry for stating the bleedin' obvious, and I'm not an RDBMS specialist at all, but isn't your requirement so "unorthodox" that it dictates another approach? 对不起,我很遗憾地指出了流血问题,而且我也不是RDBMS专家,但是您的要求不是那么“非常规”,以致于它决定了另一种方法吗? You yourself say you know this is "terrible practice". 您自己说您知道这是“可怕的做法”。 Isn't the thing about auto-increment fields that they serve one purpose and one purpose only: to assign a unique ID to a particular record, and isn't it, um, absolutely wrong to attribute any significance whatever to the actual value? 自动递增字段只为一个目的和一个目的而已:不是为一个特定的记录分配唯一的ID,不是,将任何意义赋予实际值是绝对错误的吗?

So, ever so tentatively, might I suggest using another field to do what you want? 因此,试探性地,我是否可以建议使用另一个字段来执行您想要的操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM