After inserting the data return method mysql increment the ID (the AUTO_INCREMENT)

After mysql insert the data returned from the method of increasing ID 
mysql after inserting a piece of data, how to get to the increment id value of it? That is how to get id (AUTO_INCREMENT) increment primary keys ??

Method one: using last_insert_id

mysql> SELECT LAST_INSERT_ID();

Increment after each generated connection ID stored in the server. 
This means that the value of the function to a given client is returned to the client first AUTO_INCREMENT value generated for most recent statement affecting AUTO_INCREMENT columns. 
This value can not be affected by other clients, even if they generate AUTO_INCREMENT values of their own. 
This action ensures that you can recover your ID without worrying about other client activities, but do not need to lock or handle. 
Every mysql_query operations on the mysql server can be understood as an "atomic" operation, a write operation is often necessary to lock the table, the application server mysql lock table is not our application lock table.

值得注意的是,如果你一次插入了多条记录,这个函数返回的是第一个记录的ID值。
因为LAST_INSERT_ID是基于Connection的,只要每个线程都使用独立的Connection对象,
LAST_INSERT_ID函数将返回该Connection对AUTO_INCREMENT列最新的insert or update*作生成的第一个record的ID。
这个值不能被其它客户端(Connection)影响,保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁。
使用单INSERT语句插入多条记录, LAST_INSERT_ID返回一个列表。
LAST_INSERT_ID 是与table无关的,如果向表a插入数据后,再向表b插入数据,LAST_INSERT_ID会改变。

Method two: using max (id)

Use last_insert_id is connected (connection), if the time window for a call would always return 0, based on 
if not frequent insertion we can also use this method to get the value returned by id

select max(id) from user;

The disadvantage of this method is not suitable for high concurrency. If both the insertion of the value returned may not be accurate.

Method three: is to create a stored procedure call in a stored procedure to insert operation and then get the maximum value

DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`(in name varchar(100),out oid int)
BEGIN
  insert into user(loginname) values(name);
  select max(id) from user into oid;
  select oid;
END $$
DELIMITER ;
call test('gg',@id);

Method 4: Use @@ identity

select @@IDENTITY

@@ identity is represented by a value corresponding to an auto-increment when last insert data into a table with identity attribute (i.e., auto-increment), the system-defined global variables. General system-defined global variables all start with @@, user-defined variables that begin with @. A table such as there, it is an auto-increment id, when inserted into a row of data in Table A, if data from additional value is inserted automatically increased to 101, 101 select @@ identity value is obtained through. @@ identity is provided using the insert operation is performed, performing SELECT @@ identity when connection is not closed, or a NULL value will be obtained.

Method five: is the use of getGeneratedKeys ()

Connection conn = ;
Serializable ret = null;
PreparedStatement state = .;
ResultSet rs=null;
try {
    state.executeUpdate();
    rs = state.getGeneratedKeys();
    if (rs.next()) {
        ret = (Serializable) rs.getObject(1);
    }     
} catch (SQLException e) {
}
return ret;

Guess you like

Origin blog.csdn.net/zhuchunyan_aijia/article/details/93620357