Search code examples
mysqluniqueauto-increment

How to make ID "Primary Key" Field set number based on Rows rather than AUTO_INCREMENT


Right now I have my ID field as the primary key in MySQL and have AUTO_INCREMENT on. What I want to know is how to make the ID represent the number of that row in the table rather than giving it a number when it's inserted, then sticking with that number? Because when I delete something, then that number isn't used. I want them all to be unique based on row count.


Solution

  • Always have a primary key. Either a basic auto increment int or a composite key of multiple fields. It helps your DB do it's job and comes in handy when you want to have relationships. Add a field called RowIndex and renumber it when you delete anything.