第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL LAST_INSERT_ID

標(biāo)簽:
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

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

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

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消