Can I assume the highest auto increment value is always the newest entry?

SarcasticSully

I have the following code to get the newest entry from the a table.

$con = mysqli_connect($host,$username,$password,$database);
$result = $con->query("SELECT MAX(ID) FROM Samples");
$newest = $result->fetch_assoc();

The field ID is the primary key and has auto_increment. I have no code to alter the value of ID.

The code always successfully yields the newest entry when I test it, but I don't know if this is behavior I can rely on. Will this always work? If not, is there a more reliable way to get the newest entry?

O. Jones

Will SELECT MAX(ID) FROM ... always work?

No, it will not. If you have more than one copy of your program connected to your database, and one program does what you suggest, and another program then performs an insert, the first program's MAX(ID) value will become stale. It's a race condition. Those are very difficult to detect and debug, because they don't crop up until a system is under heavy load.

If all you care about is a snapshot of MAX(ID) you'll be OK. But you cannot use this MAX(ID) value as the value for the next insertion, unless you never will have more than one program instance inserting data. It's unwise to rely on that kind of never.

If not, is there a more reliable way to get the newest entry?

Immediately after you do an INSERT operation you can do SELECT LAST_INSERT_ID() to retrieve the autoincrementing ID just used. Various APIs, like mysqli, PDO, and JDBC return this ID to a calling program when an INSERT operation completes.

LAST_INSERT_ID() and the related API functions work on a connection-by-connection basis. Therefore, if multiple connections are inserting rows, each connection will get its own id values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mysql : is the auto increment highest value always the most recent?

Can I assume that Java enumerations auto-increment by 1?

Can I assume /proc will always be readable?

How can I auto-increment the value in a dictionary when there is a difference?

Can I always assume that an mvp matrix with corner value !=1 is performing scaling?

Can I assume NULL value in comparison as the false?

Can I assume the size of long int is always 4 bytes?

Can I assume that it is always possible to open a pdf NULL device in R?

How can I set my auto-increment value to start from 1 in MySQL?

How can I get the corresponding user_id (auto increment) value of a particular row in my database?

SQL Server : setting the value to the next highest id number when the table is not set to auto increment

How can i get object with highest value?

auto increment value in liferay

Django auto increment value

How can I create an auto generated column that always has its default value in MySQL?

How can the event listener always get the newest state value in a function component?

Can't get table column to auto increment from preset value

Can I always assume that a function that does not return a Promise or an Observable is synchronous functions in JavaScript?

How do I create auto increment value for a field in elastic search

Auto Increment column value is larger than I expected

Can I atomically increment a column value in BigTable?

How can I increment a value in scheme with closure?

How can I increment a value on page refresh?

How can I increment a value from firebase?

How can I set AUTO_INCREMENT for @PrimaryColumn()?

How can I set no auto increment in migration laravel 5.3?

How can I auto-increment an MVC 6 version number?

Can I override auto increment ID with Alice Fixtures?

how can I create a auto-increment field in mongoose