Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Can you delete a row with a Primary Key?
Lets say you have a table with 20 rows and the Primary keys are auto-incremented from 1 thru 20.
You then decide you no-longer need row number 5 which has a Primary Key of the integer 5.
First, can the row be deleted?
IF deleted, can we assign 5 as the primary key of the next row added? Or must the primary key of the new row be 21?
Steven Parker220,415 Points
- You can always delete a row or rows, and every row will have a primary key in the same column.
- You can re-assign deleted keys if you include them explicitly instead of allowing the auto-increment mechanism to assign them for you. But you might not want to because it could make it difficult to track down information about the change history of the table later.