Mysql

保留自動增量 ID

  • August 28, 2021

我正在開發一個電子商務網路應用程序,這就是我的想法。

為了簡化這個問題,該user表包含兩列:id(int,PK,NN,AI),name(varchar(45),NN)。我希望員工的 ID 較短,所以我想保留較短的 ID 並在以後分配它們。id分為三類。

  1. 為員工保留1-1000。
  2. 為合作夥伴保留 1001-5000。
  3. 5001+ 使用者。

那麼有沒有辦法可以操縱自動增量ID?我目前的演練是:火災創建垃圾記錄來填充 ID,然後將它們分配給雇主、合作夥伴。

要更改 AUTO_INCREMENT 值,您必須進行下一個查詢:

mysql> ALTER TABLE user AUTO_INCREMENT=next_id;

當您在下一個插入語句中省略 id 時,您必須將 next_id 替換為將要設置的值。

然而屬性 AUTO_INCREMENT 值並不能像你想要的那樣工作,至少不是單獨的,你每次都必須搜尋你正在尋找的下一個值,在你所說的 3 種情況下進行 sql 查詢。

例如,對於您正在尋找員工下一個 ID 的情況,您必須進行下一個查詢:

mysql> SELECT MAX(id) AS max_given_id FROM user WHERE id >= 1 AND id < 1000;

然後對該值加 1 以獲取您在 INSERT 中設置的 id,而不使用 AUTO_INCREMENT 屬性,或者執行第一個給定語句來更改表 AUTO_INCREMENT 值,然後在插入中省略 id。

一種方法是執行我剛才用儲存過程解釋的過程:

mysql> DELIMITER //
mysql>
mysql> DROP PROCEDURE IF EXISTS InsertUserByType;
   -> /*first we try to erase the procedure if it is set, so we dont redeclare*/
   ->
   -> CREATE PROCEDURE InsertUserByType(IN pname VARCHAR(45), IN type VARCHAR(45))
   -> BEGIN
   ->
   ->     /*this is the declaration of the variable where we are storing the next value to set in the insert*/
   ->     DECLARE next_id INT DEFAULT 0;
   ->
   ->     /*we do the first case where we are inserting an employee*/
   ->     IF (type = 'employe') THEN
   ->         /*we look for the max value in the range for the given case*/
   ->         SELECT MAX(id) INTO next_id FROM user WHERE id >= 1 AND id < 1000;
   ->         IF (next_id IS NULL) THEN
   ->             /*if the max value doesnt exists we set it to the first value for this case*/
   ->             SET next_id = 1;
   ->         ELSE
   ->             SET next_id = next_id + 1;
   ->         END IF;
   ->     ELSE
   ->         /*we do the second case where we are inserting a partner*/
   ->         IF (type = 'partner') THEN
   ->             /*we look for the max value in the range for the given case*/
   ->             SELECT MAX(id) INTO next_id FROM user WHERE id >= 1001 AND id < 5000;
   ->             IF (next_id IS NULL) THEN
   ->                 /*if the max value doesnt exists we set it to the first value for this case*/
   ->                 SET next_id = 1001;
   ->             ELSE
   ->                 SET next_id = next_id + 1;
   ->             END IF;
   ->         ELSE
   ->             /*we do the third case where we are inserting an user*/
   ->             IF (type = 'user') THEN
   ->                 /*we look for the max value in the range for the given case*/
   ->                 SELECT MAX(id) INTO next_id FROM user WHERE id >= 5001;
   ->                 IF (next_id IS NULL) THEN
   ->                     /*if the max value doesnt exists we set it to the first value for this case*/
   ->                     SET next_id = 5001;
   ->                 ELSE
   ->                     SET next_id = next_id + 1;
   ->                 END IF;
   ->             END IF;
   ->         END IF;
   ->     END IF;
   ->
   ->     /*we make our insert with the recolected value and the name of the user given as a param*/
   ->     INSERT INTO user (id, name) VALUES (next_id, pname);
   ->
   -> END//
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;
mysql> 

每次你想插入一個使用者時,然後寫下一條語句:

mysql> CALL InsertUserByType("User Name", "partner");
Query OK, 1 row affected (0.01 sec)

引用自:https://dba.stackexchange.com/questions/298630