M
M
Mimocodil2021-07-30 16:53:17
MySQL
Mimocodil, 2021-07-30 16:53:17

How can I set up an auto-incrementing column so that no gaps appear when new rows are added?

Table
ID : INT UNSIGNED
USER_ID : BIGINT
TITLE : VARCHAR(500)
CHECKED : BOOL
I make a query like this:

INSERT INTO `testDB`.`table1` (`USER_ID`, `TITLE`, `CHECKED`) VALUES ('707', 'ExmpleString', '0');

I had a line numbered, for example, 10. Now the 11th has been added.
Next, I make a request: Line 11 is deleted. When I repeat the query with the addition, not row 11 appears in the table, but row 12. Although there is no row 11. Also, if I delete, for example, all lines from 1 to 9, leaving 10, then I can no longer fill these lines without specifying the corresponding parameter, for example, for line 2:
DELETE FROM `testDB`.`table1` WHERE (`ID` = '11');
INSERT INTO `testDB`.`table1` (`ID`, `USER_ID`, `TITLE`, `CHECKED`) VALUES ('2', '707', 'ExmpleString', '0');


Question: how to set up a table so that new records are added to the very first empty space in the table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2021-07-30
@Ezekiel4

Ok, let's imagine that you set up a similar mechanism (although it is obvious that this was done for a reason), and now you have, let's say, users with numbers 10 and 11. Each of them has some data that is tied to this user by id. Now we are deleting user number 10 and then creating a new one to take that place. Now the owner of all the data of the deleted user will be the new user, who, as it were, should not have access to them.
This is a banal and simplest example, more to understand... A unique identifier is unique for that, that it will no longer be repeated and guarantees this non-repeatability at the level of the database mechanism.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question