laserlight;10932925 wrote:sonny100 did not state what database system was used, so MySQL is just an assumption, although it is probably the best assumption to make when you have no other information 🙂
Now laserlight you have been around here long enough to know that in this forum it is always mysql unless the user specifies otherwise.
That said, my method will work for any sql db engine: unless the user has done something like force the reuse of burnt autoincs.
laserlight;10932925 wrote:Checking the MySQL documentation, can anyone confirm that AUTO_INCREMENT for MySQL does not allow reuse of ids even for deleted rows? The part about "this happens even for MyISAM tables, for which AUTO_INCREMENT values normally are not reused" seems to hint otherwise.
That quote specifically relates to the use of an autoinc in a multi-column compound key. What it actually says is that the next autoinc value is then calculated as max(autoinc)+1. So the number will be reused only if you delete the last row, or of course if an insert fails. Gaps elsewhere in the sequence are NOT filled in EVER.
As to the rest, what you do inside the db is up to you. My strictures apply to the use of the column value in the outside world. I can't think of any other reason why one would be bothered about gaps in the sequence.
The book that you read probably also warned against using an autoinc as the primary key as well. Now that is a debate I am not going to waste my time on either.
But you are right. Striclty speaking the insert datetime should be used. The fact that no such column was mentioned does not mean that we can not assume it exists. After all I assumed that there was an autoinc column as well. The fact that datetime indexes are nmuch slower to process than integer indexes should not be important: except that query processing speed is almost always important to me at least.