Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i need help about ID number..my table id is auto incremented and if i delete a specific id then insert another why is it that the numbering continue even if i deleted some id. take a look at the last part
|  4 | Delsa     | Arjay     | E              |
is this normal or is there another way to solve this?

What I have tried:

MariaDB [project]> DELETE FROM Student_Name WHERE id=3;
Query OK, 1 row affected (0.005 sec)

MariaDB [project]> select *from student_name;
+----+-----------+------------+----------------+
| id | Last_Name | First_Name | Middle_Initial |
+----+-----------+------------+----------------+
|  1 | Gargar    | Rosales  | F              |
|  2 | florante    | Rosal      | L         |
+----+-----------+------------+----------------+
2 rows in set (0.001 sec)

MariaDB [project]> DELETE FROM Student_Name WHERE id=2;
Query OK, 1 row affected (0.003 sec)

MariaDB [project]> select *from student_name;
+----+-----------+------------+----------------+
| id | Last_Name | First_Name | Middle_Initial |
+----+-----------+------------+----------------+
|  1 | Gargar    | Rosales  | F              |
+----+-----------+------------+----------------+
1 row in set (0.000 sec)

MariaDB [project]> desc student_name;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| Last_Name      | varchar(50) | NO   |     | NULL    |                |
| First_Name     | varchar(50) | NO   |     | NULL    |                |
| Middle_Initial | varchar(1)  | NO   |     | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+
4 rows in set (0.006 sec)

MariaDB [project]> insert into student_name (Last_Name, First_Name, Middle_Initial) values ('Delsa', 'Arjay', 'E');
Query OK, 1 row affected (0.002 sec)

MariaDB [project]> DELETE FROM Student_Name WHERE id=2;
Query OK, 0 rows affected (0.001 sec)

MariaDB [project]> select *from student_name;
+----+-----------+------------+----------------+
| id | Last_Name | First_Name | Middle_Initial |
+----+-----------+------------+----------------+
|  1 | Delan     | Rosalinda  | F              |
|  4 | Delsa     | Arjay     | E              |
+----+-----------+------------+----------------+
2 rows in set (0.000 sec)
Posted
Updated 22-Apr-22 23:18pm
Comments
Richard MacCutchan 23-Apr-22 3:57am    
Yes, that is correct, database systems do not try to re-use deleted values. The number is held in an internal table and incremented for every usage.
Member 15356357 23-Apr-22 4:30am    
okay i get it now thanks

1 solution

That is exactly what is expected to happen: SQL does not "look" at past values when assigning a new ID value because previous values are "used" and could potentially have other data which was related to that ID (though not in a formal FOREIGN KEY relationship) stored elsewhere.

If you want incrementing numbers, use the ROW_NUMBER[^] function instead and define a sort criteria - but remember that the values can change unlike ID values which should remain constant forever.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900