亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

MySQL LAST_INSERT_ID

標簽:
MySQL

Summary: in this tutorial, you will learn how to use the MySQL LAST_INSERT_ID function to obtain the generated sequence number of the last insert record.

In database design, we often use a surrogate key to generate unique values for the identity column by setting the AUTO_INCREMENT attribute for the column. When we insert a new record into the table that has AUTO_INCREMENT column, MySQL generates a unique ID automatically based on the column’s definition.

For more information on how to set AUTO_INCREMENT attribute for a column, check it out the MySQL sequencetutorial.

We can obtain the generated sequence number by using the MySQL LAST_INSERT_ID function and use the number for the next statements e.g., inserting a new row into the related tables.

Let’s take a look at an example of using MySQL LAST_INSERT_ID function:

CREATE TABLE tbl(    id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,    description varchar(250) NOT NULL ); INSERT INTO tbl(description) VALUES('MySQL last_insert_id'); SELECT LAST_INSERT_ID();

mysql last_insert_id

How it works.

  • First, we created a new table named tbl for testing. In the tbl table, we set  AUTO_INCREMENT attribute for the   id column.

  • Second, we inserted a new record into the tbl table.

  • Third, we used MySQL LAST_INSERT_ID function to obtain last insert id that MySQL has been generated.

It’s important to note that if you insert multiple records into a table using a single INSERT statement, the LAST_INSERT_ID function will return the last insert id of the first record. Suppose the AUTO_INCREMENT column has the last sequence number 3 and you insert 5 records into the table, when you use the LAST_INSERT_ID function to get the last insert id, you will get 4 instead of 8.

Let’s take a look at the following example:

INSERT INTO tbl(description) VALUES('record 1'), ('record 2'), ('record 3'); SELECT LAST_INSERT_ID();

mysql last_insert_id multiple inserts

Check the data of the tbl table:

SELECT * FROM tbl

mysql last_insert_id tbl table

First, we inserted 3 records into the tbl table by using a single INSERT statement. Then, we used the LAST_INSERT_ID function to get the last insert id, which is the id of the 'record 1'.

The LAST_INSERT_ID function works based on client-independent principle. It means the value returned by the LAST_INSERT_ID function for a specific client is the value generated by that client only. This ensures that each client can obtain its own unique ID.

In this tutorial, we have shown you how to use the MySQL LAST_INSERT_ID function to get the sequence number of the last record that has been inserted into a table.

原文链接:http://outofmemory.cn/mysql/function/mysql-last_insert_id

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消