mysql returns increment id

Mysql execute insert in operation, there is no increment id returned for follow-up which caused great inconvenience to write logic.

In fact, there are several ways to do directly after the insert to increment id.

1. Perform select max (id);

  The principle is the largest direct lookup database id, the downside: once the concurrent capacity, the amount of data for a long time will be lower performance for wrong situation.

2. Perform selsct LAST_INSERT_ID ();

  It is based on principle because LAST_INSERT_ID Connection, as long as each thread uses a separate Connection object, LAST_INSERT_ID function returns the Connection ID for the first record AUTO_INCREMENT column insert or update * the latest generation of operation. This value will not be affected by other client (Connection), to ensure that you can recover your ID without worrying about other client activities, and does not require locking. Using a single INSERT statement to insert multiple records, LAST_INSERT_ID returns a list.

  Note that when you perform multiple insert, it will return the first increment id.

reference:

  https://www.cnblogs.com/duanxz/p/3862356.html

 

Guess you like

Origin www.cnblogs.com/two-bees/p/11039454.html