简体   繁体   English

MySQL的auto_increment加一定数量

[英]MySql auto_increment plus a certain number

I want my the id field in my table to be a bit more " random" then consecutive numbers. 我希望我表中的id字段比连续数字更具“随机性”。

Is there a way to insert something into the id field, like a +9, which will tell the db to take the current auto_increment value and add 9 to it? 有没有一种方法可以在id字段中插入一些内容,例如+9,这将告诉db使用当前的auto_increment值并将其添加9?

You have a design flaw. 您有设计缺陷。 Leave the auto increment alone and shuffle your query result (when you fetch your data) 不用理会自动递增,并改组查询结果(获取数据时)

Though this is generally used to solve replication issues, you can set an increment value for auto_increment: 尽管这通常用于解决复制问题,但是您可以为auto_increment设置一个增量值:

auto_increment_increment auto_increment_increment

Since that is both a session and a global setting, you could simply set the session variable just prior to the insert. 由于这既是会话设置,又是全局设置,因此您只需在插入之前设置会话变量即可。

Besides that, you can manually do it by getting the current value with MAX() then add any number you want and insert that value. 除此之外,您可以通过使用MAX()获取当前值,然后添加所需的任何数字并插入该值来手动执行此操作。 MySQL will let you know if you try to insert a duplicate value. 如果您尝试插入重复值,MySQL会通知您。

As far as i know, it's not possible to 'shuffle' your current IDs. 据我所知,不可能“改组”您当前的ID。 If you wanted though, you could pursue non-linear IDs in the future. 如果您愿意,将来可以使用非线性ID。

The following is written in PDO, there are mysqli equivalents . 以下是用PDO编写的,有mysqli等效项

This is just an arbitrary INSERT statement 这只是一个任意的INSERT语句

$name = "Jack";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = "INSERT INTO tableName (name) VALUES(:name)";
$q = $conn->prepare($sql);
$q->execute(':name' => $name);

Next, we use lastInsertId() to return the ID of the last inserted row, then we concatenate the result to rand() 接下来,我们使用lastInsertId()返回最后插入的行的ID,然后将结果连接到rand()

$lastID = $conn->lastInsertId();
$randomizer = $lastID.rand();    

Finally, we use our 'shuffled' ID and UPDATE the previously inserted record. 最后,我们使用“改组” ID并UPDATE之前插入的记录。

$sql = "UPDATE tableName SET ID = :randomizer WHERE ID=:lastID ";
$q = $conn->prepare($sql);
$q->execute(array(':lastID' => $lastID , ':randomizer' => $randomizer));

An idea.. (Not tested) 一个想法..(未测试)

  1. CREATE TRIGGER 'updateMyAutoIncrement' CREATE TRIGGER 'updateMyAutoIncrement'
  2. BEFORE INSERT
  3. ON 'DatabaseName'.'TableName' ON '数据库名称'。'表名称'
  4. FOR EACH ROW
  5. BEGIN
  6. DECLARE aTmpValueHolder INT DEFAULT 0; DECLARE aTmpValueHolder INT DEFAULT 0;
  7. SELECT AUTO_INCREMENT INTO aTmpValueHolder SELECT AUTO_INCREMENT INTO aTmpValueHolder
  8. FROM INFORMATION_SCHEMA.TABLES
  9. WHERE TABLE_SCHEMA = 'DatabaseName' WHERE TABLE_SCHEMA = '数据库名称'
  10. AND TABLE_NAME = 'TableName' ; AND TABLE_NAME = 'TableName' ;
  11. SET NEW .idColumnName = aTmpValueHolder + 9; SET NEW .idColumnName = aTmpValueHolder + 9;
  12. END ; END

Edit : If the above trigger doesn't work try to update AUTO_INCREMENT value directly into the system's schema. 编辑:如果上述触发器不起作用,请尝试将AUTO_INCREMENT值直接更新到系统的架构中。 But as noted by Eric, your design seems to be flawed. 但正如Eric所指出的那样,您的设计似乎存在缺陷。 I don't see the point of having an auto-increment here. 我看不到在这里自动增加的意义。

Edit 2 : For a more 'random' and less linear number. 编辑2:对于更多的“随机”和较少的线性数。

  1. SET NEW .idColumnName = aTmpValueHolder + RAND(10); SET NEW .idColumnName = aTmpValueHolder + RAND(10);

Edit 3 : As pointed out by Jack Williams, Rand() produces a float value between 0 and 1. So instead, to produce an integer, we need to use a floor function to transform the 'random' float into an integer. 编辑3:正如Jack Williams指出的那样, Rand()产生一个介于0和1之间的float值。因此,要产生一个整数,我们需要使用floor函数将“随机”浮点数转换为整数。

SET NEW .idColumnName = aTmpValueHolder + FLOOR(a + RAND() * (b - a)); SET NEW .idColumnName = aTmpValueHolder + FLOOR(a + RAND() * (b - a));

where a and b are the range of the random number. 其中a和b是随机数的范围。

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

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