How does MySQL obtain the maximum value of an automatically added ID?

Source: Internet
Author: User

Sometimes we store the largest ID in the database, and this ID is automatically added whenever there is a new record.

Create the following table:

Create Table maxidtest ('id' int (10) unsigned not null auto_increment, 'name' varchar (100) not null, primary key ('id ')) engine = InnoDB auto_increment = 23 Default charset = utf8;

The ID field is the automatically added field.

Insert a record:

Insert into maxidteset (name) values ("hello ');

Then use:

Select last_insert_id ();

Obtain the id value automatically added and generated when the inserted record is obtained.

Because last_insert_id () is session-oriented, there is no problem when multiple users operate simultaneously.

In addition, if you useProgramming LanguageTo obtain the maximum ID of the inserted record.CodeAs follows:

Dataset DS = mysqlquery ("select last_insert_id ()"); string id = Ds. Tables [0]. Rows [0] [0]. tostring ();

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.