0

I am working through the MySQL in a Nutshell book, which has me creating a database (schema) for a bookstore. The candidate (primary) key for each book has the attribute 'book_id', which is set to NOT NULL and AUTO INCREMENT. Eventually, I had 10 entries, the last of which had a book_id of 10. I deleted that entry, and then added a new one. However, the next entry had a book_id of 11 instead of 10. I used the UPDATE statement to change it to 10, but is there a way to make sure it will increment it properly?

After using the UPDATE statement.

Dennis
  • 523
  • 2
  • 7
  • 12
  • This is generally what you want... reusing an ID can cause all sorts of problems. As an example, perhaps you're selling books, and an order comes in from the website with ID 10. Perhaps the customer wanted to buy the book you just deleted. You don't want to now sell them the new book instead! As another example, maybe you store user sessions in your database. When a session expires, you delete it. What you absolutely don't want to happen is to see a session ID of 10 and suddenly show the user someone else's data because you reused that session ID. – user94559 Aug 01 '16 at 01:11
  • Thanks, Scott! And thanks, smarx! I didn't think of that at first, and it makes a lot of sense. – Dennis Aug 01 '16 at 01:19

0 Answers0