Used AUTO_INCREMENT to create ID numbers

Asked

Viewed 32 times

0

I am using AUTO_INCREMENT to automatically add an ID whenever I create a new column. However when clearing a column, the ID number continues to add +1 instead of starting from where it left off.

How do I make when I clean a column the ID number is the same instead of adding +1?

1 answer

1


When you use auto increment, it will increment in all the added Rows, then when you eliminate you can use multiple approaches. Being all the data at once, uses the truncate table that will reseed to this value. I note, however, that data is typically not deleted, but marked as deleted with an isDeleted flag for example. But if you really want to eliminate and maintain the ID sequence, you can do it this way:

Mysql

ALTER TABLE table_name AUTO_INCREMENT = value;

MS SQL

DBCC CHECKIDENT (table_name, reseed, value)
  • I had already seen the first option (Mysql) and really should be the best I can do, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.