I have a table (several actually) that I want to get results from with the most recent entries first. Here are my ORDER BY clause options:
- date_created INT (never changes value)
- id (INT AUTO_INCREMENT of course!)
Both columns should equally represent the order in which the records were inserted. I naturally would use the date_created field like any sane person would, but I’m still curious about this.
I know this is probably splitting hairs, but is there any reason or edge case why I should NOT use the id column?
EDIT: I’m thinking that this question is vague as to which value we want to truly represent the insert order. Thank you for all your answers everybody, I am going to accept the best one and move on because I think I have made this difficult by assuming that ids will always be in order (see @Wrikken’s comment). My gut instinct is that id should never be considered by the developer, which is what most of the answers here are pointing to.
It isn’t a good idea to depend on the ID column for time ordering, because that isn’t its purpose. Basically, the ID is just a unique key for that row, nothing more. Using ID might never cause problems, but there is no reason to add complexity of assuming that ordering by ID will always hold. For instance, you might in the future want to delete entries and then manually insert new entries, or import entries from some other source that are timestamped in the past. If you didn’t have a date_created column, then ID would be your only option, but since you have the column, use it, as it is your best choice.