mySql - Get Last Inserted ID

Given the scenario where you have the tables:
  • Clients and Clients_Addresses
In order to retrieve the id of the last client inserted in Clients you may do:
  • select LAST_INSERT_ID()
This will then retrieve the last inserted id, but looking at the query you will notices that you don't have the table. So this solution will only work if you want to retrieve the id of the last inserted row in all your database, so be aware of concurrency problems.

If this solution does not fit, you may construct you table in a way that the id is generated with AUTO_INCREMENT flag, than simply issue a
  • select max(cli_id) from Clients
and use this value to add in the Clients_Addresses table

Comments

Popular posts from this blog

Oracle - duplicate fields in RECORD,TABLE or argument list are not permitted

Oracle - ORA-29282: invalid file ID, ORA-06512: at "SYS.UTL_FILE"