Jump to content

MySQL select after a certain id?


rockinaway

Recommended Posts

The ID of a row is metadata, not actual data.  It's supposed to be used to identify a row 3 seconds later when a user clicks "update."  It's not supposed to be used to determine the long-term identity of a piece of data.

 

Back in the day when Fenway and I learned databases, especially MySQL, the ID of a row could actually CHANGE at random.  If you had all your foreign keys set up properly and your code was correct, it wouldn't affect you, but if you thought that User ID 3 was always you, and that User ID 47 was created after User ID 45, you were in for some surprises. 

 

It's not that big of a deal anymore, of course, because database tech has matured, but it's still not good to rely on an auto-number column for absolute long term identity.  The only thing that identifies the data accurately is the data itself.  The auto-numbered keys are for ease of access for your application that sits on top of a database (and in some instances for foreign keys).

 

Apologies to fenway if I made you feel old.

I see, thanks a lot for the explanation. You're going to laugh at this but I found that using the id as a method of ordering was actually causing some errors :P

 

So I added a 'AND time < ...' so using actual values at the same times. Solved the problems. Thanks!

The ID of a row is metadata, not actual data.  It's supposed to be used to identify a row 3 seconds later when a user clicks "update."  It's not supposed to be used to determine the long-term identity of a piece of data.

 

Back in the day when Fenway and I learned databases, especially MySQL, the ID of a row could actually CHANGE at random.  If you had all your foreign keys set up properly and your code was correct, it wouldn't affect you, but if you thought that User ID 3 was always you, and that User ID 47 was created after User ID 45, you were in for some surprises. 

 

It's not that big of a deal anymore, of course, because database tech has matured, but it's still not good to rely on an auto-number column for absolute long term identity.  The only thing that identifies the data accurately is the data itself.  The auto-numbered keys are for ease of access for your application that sits on top of a database (and in some instances for foreign keys).

 

Apologies to fenway if I made you feel old.

I have different complaints about auto-increment -- particularly as they relate to InnoDB and server restarts -- but the fact that you can assign an arbitrary one make it all meaningless, and gaps can filled as well if rows are deleted under certain circumstances.

 

You have to treat this as meta-data, that's true -- consider it a "row pointer", and nothing else -- it's an integer for monotonic convenience, nothing more.

 

@ManiacDan -- not that old, but those problems are still fresh.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.