I am having trouble getting my database to stay organized I would like the ID field which is set to an INT PRIMARY KEY AUTO_INCREMENT to stay in numerical order. The problem comes when I delete all of the entries in the database. Before deleting the entries everything is in order as intended, after deleting instead of going in numerical order starting with the smaller integer first it will always go in descending order. Can someone explain what is causing this.
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
| id | employeeID | date | timeIn | jobDescription | equipType | unitNumber | unitHours | timeOut |
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
| 1 | 2 | 01/13/13 | 1:19 pm | Test1 | Dozer | 1D | 98 | 1:20 pm |
| 2 | 2 | 01/13/13 | 1:20 pm | Test2 | Dozer | 1D | 98 | 1:20 pm |
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
AFTER DELETING ALL ROWS:
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
| id | employeeID | date | timeIn | jobDescription | equipType | unitNumber | unitHours | timeOut |
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
| 4 | 2 | 01/13/13 | 1:22 pm | Test2 | Dozer | 1D | 98 | 1:22 pm |
| 3 | 2 | 01/13/13 | 1:22 pm | Test1 | Dozer | 1D | 98 | 1:22 pm |
+----+------------+----------+---------+----------------+-----------+------------+-----------+---------+
Remember that without an
ORDER BYclause in yourSELECTquery, the order is effectively not deterministic and you cannot and should not rely on it being in any particular order.What I see here is
Test2inserted withid=4, and assuming you first inserted theTest1row, then yourauto_incrementis working as expected.If you simply did something like
SELECT * FROM thistable, do not expect the rows to be ordered byid ASC. Add anORDER BYclause to be explicit about it.Since all the other columns between these two new rows have equal values, whichever column MySQL has chosen to sort on absent an explicit
ORDER BYdoesn’t have anything to differentiate.