0

I have a table having id set to auto increment.

I have records 1 to 10 and I have deleted records 3 and 7.

So is their any way I can add records 3 and 7 again?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • why are you trying to do that? what's the use case? – Ali Jan 28 '18 at 21:05
  • Assumptions, as I understand you, you want the deleted records back, if not committed through DB side, you can roll back, if committed, then, sorry your records are gone... – ArifMustafa Jan 29 '18 at 00:36
  • Does this answer your question? [How to reuse auto\_increment values?](https://stackoverflow.com/questions/9743121/how-to-reuse-auto-increment-values) – Nico Haase Oct 23 '20 at 13:18

3 Answers3

0

Yes, you have to "force" the id.

INSERT INTO table (id, field1) VALUES (3, "value"); // id = 3

But if you let the id "null" or not set, it will be incremented :

INSERT INTO table (field1) VALUES ("value"); // id = 8
Syscall
  • 19,327
  • 10
  • 37
  • 52
0

In Mysql You can override the auto-increment id simply assingning the value you need .. this is perfectly legal ..

so you can insert your row with id 3 and 7 using

  insert into your_table (id, col1, col2 ..) values (3, 'value_for_col1', 'value_for_col2'...)
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

the autoincrement only works if no value is specified for the field, in other words you can insert the specifying the value of the field with autoincrement, example: insert in db.banks (id, description,status, created_at,updated_at) values ('3', 'Central Bank', '1', '2017-04-14 10:30:22', null);