Is it a bad practice to rely on an auto-incrementing primary key to sort rows in a table? A coworker and I were having an argument about this subject. We need to be able to find the last-inserted row in a table, and we have no create/update time columns (which might be considered a bad practice in itself, but that's a different argument). We obviously can't rely on the natural order of the rows, since that can be inconsistent. Is there anything inherently wrong with using the primary key in this way, given that it's an immutable value?
I should have noted: our implementation is using Hibernate (an ORM) to fetch our objects. We're not using any native queries -- only JPQL or Hibernate's functionality.
LAST_INSERT_ID()
to an axilliary table (namedLAST_INSERTED_ID
, with one column whose name you can probably guess) and then use Hibernate to read that table. It's ugly but it might work. I guess you can't use stored procedures, can you? Have you checked to see if Hibernate has some built-in mechanism to getLAST_INSERT_ID()
?