Example of LAST_INSERT_ID in MySQL

Source: Internet
Author: User
Environment, amt, toaccno, fromaccno)

Environment: MySQL Sever 5.1 MySQL command line tool first let's take a look at an example (the primary key is auto-incrementing): mysql insert into bankaccount (name, balance) values ('123', 123); Query OK, 1 row affected (0.06 sec) mysql insert into bankstatement (action, txdate, amt, toaccno, fromaccno)

Environment: MySQL Sever 5.1 + MySQL command line tool

First, let's look at an example (the primary key is auto-incrementing ):


Mysql> insert into bankaccount (name, balance) values ('000000', 123); Query OK, 1 row affected (1000 sec) mysql> insert into bankstatement (action, txdate, amt, toaccno, fromaccno) values ('201312', curdate (), 122, 1, 2); Query OK, 1 row affected (1000 sec) mysql> select last_insert_id (); + ------------------ + | last_insert_id () | + -------------------- + | 7 | + ------------------ + 1 row in set (0.00 sec) mysql> select * from bankaccount; + ------- + ------ + --------- + | accno | name | balance | + ------- + ------ + --------- + | 1 | Zhang San | 200 | 2 | Li Si | 900 | 3 | 123 | 1000 | 4 | 123 | 1000 | + ------- + ------ + --------- + 4 rows in set (0.00 sec) mysql> select * from bankstatement; + ---- + -------------- + ------------ + ------ + --------- + ----------- + | id | action | txdate | amt | toaccno | fromaccno | + ---- + -------------- + ------------ + ------ + ----------- + 1 | account opening | 100 | NULL | 1 | 2 | account opening | 2012-10-14 | 1000 | NULL | 2 | 3 | query account information | 2012-10-14 | 0 | NULL | 2 | | 4 | search for account information | 2012-10-14 | 0 | NULL | 1 | 5 | transfer | 2012-10-14 | 100 | 1 | 2 | 6 | 122 | 2012-10-14 | 1000 | 1 | 2 | 7 | 122 | 2012-10-14 | 1000 | 1 | 2 | + ---- + ---------------- + ---------- + ------ + --------- + ----------- + 7 rows in set (0.00 sec)


Conclusion: LAST_INSERT_ID () returns the value of the AUTO_INCREMENT column in The Last INSERT or UPDATE statement.


References:
Http://zhaohe162.blog.163.com/blog/static/38216797201122411193745/


Http://it.100xuexi.com/view/otdetail/20120619/73a6cc8f-36b8-4b70-8904-57c18d3ab385.html


Http://blog.csdn.net/wh62592855/article/details/6722298

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.