Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Modifying Data with SQL Deleting Data From a Database Removing Specific Rows

Vaidas Staugaitis
Vaidas Staugaitis
1,431 Points

Auto incremented ID after deleting rows

Hi,

If I let's say delete the rows with ID's 1 to 6, and only have the ones with 7 and up left, is there any way to sort of move all the ID's so it starts at 1 again, rather than 7, without having to manually update every ID in every row?

1 Answer

Steven Parker
Steven Parker
231,248 Points

Why would you want to do that?

Auto-increment ID's serve the purpose of providing unique identifiers for each row, but there's no real advantage to having them be contiguous. In fact, at some later point it may actually be useful to identify that something was removed even if it cannot be completely restored.

If I did have a compelling reason to make ID's contiguous (though I can't think of one offhand), I would probably copy the existing rows into a new autonumbered table.