简体   繁体   中英

Mysql auto_increment proceed with lowest value

My problem is: I have a table with an auto_increment column. When I insert some values, all is right. Insert first row: ID 1 Insert second row: ID 2

Now I want to insert a row at ID 10.

My problem is, that after this there are only rows inserted after ID 10 (which is the normal behaviour ).

But I want that the database first fills up ID 3-9 before making that.

Any suggestions?

EDIT:

To clarify: this is for an URL shortener I want to build for myself. I convert the id to a word(a-zA-z0-9) for searching, and for saving in the database I convert it to a number which is the ID of the table.

The Problem is now: I shorten the first link (without a name) -> ID is 1 and the automatically name is 1 converted to a-zA-Z0-9 which is a

Next the same happenes -> ID is 2 and the name is b, which is 2 converted.

Next interesting, somebody want to name the link test -> ID is 4597691 which is the converted test

Now if somebody adds another link with no name -> ID is 4597692 which would be tesu because the number is converted.

I want that new rows will be automatically inserted at the last gap that was made (here 3)

You could do 6 dummy inserts and delete/update them later as you need. The concept of the auto increment, by design, is meant to limit the application's or user's control over the number to ensure a unique value for every single record entered into the table.

ALTER TABLE MY_TABLE AUTO_INCREMENT = 3;

You would have to find first unused id, store it as user variable, use as id for insert.

SELECT @id := t1.id +1 
FROM sometable t1 LEFT JOIN sometable t2 
ON t2.id = t1.id +1 WHERE t2.id IS NULL LIMIT 1;

INSERT INTO sometable(id, col1, col2, ... ) VALUES(@id, 'aaa', 'bbb', ... );

You will have to run both queries for every insert if you still have gaps, its up to you to decide whether it is worth doing it.

not 100% sure what you're trying to achieve but something like this might work:

drop table if exists foo;
create table foo
(
id int unsigned not null auto_increment primary key,
row_id tinyint unsigned unique not null default 0
)
engine=innodb;

insert into foo (row_id) values (1),(2),(10),(3),(7),(5);

select * from foo order by row_id;

+----+--------+
| id | row_id |
+----+--------+
|  1 |      1 |
|  2 |      2 |
|  4 |      3 |
|  6 |      5 |
|  5 |      7 |
|  3 |     10 |
+----+--------+
6 rows in set (0.00 sec)

You could have another integer column for URL IDs.

Your process then might look like this:

  • If a default name is generated for a link, then you simply insert a new row, fill the URL ID column with the auto-increment value, then convert the result to the corresponding name.

  • If a custom name is specified for a URL, then, after inserting a row, the URL ID column would be filled with the number obtained from converting the chosen name to an integer.

And so on. When looking up for integer IDs, you would then use the URL ID column, not the table auto-increment column.

If I'm missing something, please let me know.

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