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.

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
220,415 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.