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 Querying Relational Databases Database Keys Primary Keys

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.

  1. First, can the row be deleted?

  2. 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?

1 Answer

Steven Parker
Steven Parker
229,744 Points
  1. You can always delete a row or rows, and every row will have a primary key in the same column.
  2. 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.