How do I get the current AUTO_INCREMENT value for a table in MySQL?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
Even though methai's answer is correct if you manually run the query, a problem occurs when 2 concurrent transaction/connections actually execute this query at runtime in production (for instance).
Just tried manually in MySQL workbench with 2 connections opened simultaneously:
Transaction 1:
Transaction 2:
Insert of transaction 1 is ok: Insert of transaction 2 goes in error: Error Code: 1062. Duplicate entry '21' for key 'PRIMARY'.
A good solution would be jvdub's answer because per transaction/connection the 2 inserts will be:
Transaction 1:
Transaction 2:
But we have to execute the last_insert_id() just after the insert! And we can reuse that id to be inserted in others tables where a foreign key is expected!
Also, we cannot execute the 2 queries as following:
because we actually are interested to grab/reuse that ID in other table or to return!
If column is autoincremented in sql server then to see the current autoincremented value, and if you want to edit that value for that column use the following query.
If you just want to know the number, rather than get it in a query then you can use:
You should see the auto_increment at the bottom
mysqli executable sample code:
You can get all of the table data by using this query:
You can get exactly this information by using this query:
I believe you're looking for MySQL's LAST_INSERT_ID() function. If in the command line, simply run the following:
You could also obtain this value through a SELECT query: