简体   繁体   中英

is it possible to update the autoincrement primary key values to the vacant values for mysql table

This is my table with id field as unsigned tinyint PRIMARY KEY with NOT NULL and AUTO_INCREMENT.

+----+
| id |
+----+
|  1 |
|  4 |
|254 |
|255 |
+----+

My application is an embedded application so I am using the small datatypes like tinyint. My total count of id values wont go upto 255. But in case if the in between values are deleted for number of times then inserting the new id value will definitely reach 255 sooner or later.

I have some questions,

Is it possible to set the auto increment feature such that it will be insert the new id to the not existing greater value (here 2)?

If not please suggest some way to handle this issue other than using higher data type for id and updating the higher id values after deleting an id (like if I delete id = 2 update all the id values greater than 2 to id-1 so that all the values remain in sequence while inserting new value).

Append your fields for insert. this will use the lowest free id

INSERT INTO ai (id) (
  SELECT a.id+1 FROM ai a
  LEFT JOIN ai b ON a.id+1 = b.id
  WHERE b.id IS NULL LIMIT 1
)

OR to get also 1

   SELECT a.id+1 FROM (
       SELECT 0 AS id UNION SELECT id FROM ai 
       ) AS a
   LEFT JOIN ai b ON a.id+1 = b.id
   WHERE b.id IS NULL
   ORDER by a.id ASC 
   LIMIT 1;

I can't think why you choose to be bound by this arbitrary limit. That said, if were to do this then I would construct a table with all possible integers (1-255) and a flag indicating whether the value was currently in use. So a DELETE becomes UPDATE x SET flag = 0 WHERE id = n . Then the query to find the lowest 0 flag becomes trivial.

@

Edge Goldberg - if you want to reorder the entrys and use auto_increment use this. Then also lst_insertid will work -- : -- After DELETE a ROW UPDATE abc , (SELECT @nr:=0) AS INIT SET a := @nr := (@nr+1); ALTER TABLE abc AUTO_INCREMENT=1;

-- INSERT a new one INSERT INTO abc (a) VALUES(1234); SELECT LAST_INSERT_ID();

Here is the Sample for set auto_increment

MariaDB []> select * from abc;
+-------+------+
| a     | b    |
+-------+------+
| 00001 |    2 |
| 00002 |    3 |
| 00004 |    5 |
| 00005 |    6 |
| 00007 |    8 |
| 00008 |    9 |
| 00009 |   10 |
| 00010 |   11 |
| 00012 |   13 |
| 00013 |   14 |
| 00014 |   15 |
| 00015 |   16 |
| 00016 |   17 |
| 00017 |   18 |
| 00018 |   19 |
| 00033 |   34 |
| 00077 |   78 |
| 00555 |  556 |
+-------+------+
18 rows in set (0.00 sec)

MariaDB []> -- After DELETE a ROW
MariaDB []> UPDATE abc , (SELECT @nr:=0) AS INIT SET a := @nr := (@nr+1);
Query OK, 16 rows affected (0.03 sec)
Rows matched: 18  Changed: 16  Warnings: 0

MariaDB []> ALTER TABLE abc AUTO_INCREMENT=1;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB []> select * from abc;
+-------+------+
| a     | b    |
+-------+------+
| 00001 |    2 |
| 00002 |    3 |
| 00003 |    4 |
| 00004 |    5 |
| 00005 |    6 |
| 00006 |    7 |
| 00007 |    8 |
| 00008 |    9 |
| 00009 |   10 |
| 00010 |   11 |
| 00011 |   12 |
| 00012 |   13 |
| 00013 |   14 |
| 00014 |   15 |
| 00015 |   16 |
| 00016 |   17 |
| 00017 |   18 |
| 00018 |   19 |
+-------+------+
18 rows in set (0.00 sec)

MariaDB []> INSERT INTO abc (a) VALUES(NULL);
Query OK, 1 row affected (0.01 sec)

MariaDB []> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|               19 |
+------------------+
1 row in set (0.00 sec)

MariaDB []> select * from abc;
+-------+------+
| a     | b    |
+-------+------+
| 00001 |    2 |
| 00002 |    3 |
| 00003 |    4 |
| 00004 |    5 |
| 00005 |    6 |
| 00006 |    7 |
| 00007 |    8 |
| 00008 |    9 |
| 00009 |   10 |
| 00010 |   11 |
| 00011 |   12 |
| 00012 |   13 |
| 00013 |   14 |
| 00014 |   15 |
| 00015 |   16 |
| 00016 |   17 |
| 00017 |   18 |
| 00018 |   19 |
| 00019 |   20 |
+-------+------+
19 rows in set (0.01 sec)

MariaDB []>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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