[Distinct] MySQL auto-increment Field

Source: Internet
Author: User

From: http://www.linuxidc.com/Linux/2008-08/15068.htm

1. Keywords
Auto_increment

2. Auto-increment usage
Example:
Create Table animals (ID mediumint not null auto_increment,
Name char (30) not null,
Primary Key (ID ));

3. Auto-Increment

Q: How to obtain the current maximum auto-increment value?
 

A: Select @ identity

Q: How to obtain the current auto-increment maximum value of a table?
A: Select max (ID) from table

Q: What is auto-increment?
A: Generally, you can obtain the ID of the data you just inserted by using select max (ID) from table. Last_insert_id is table-independent. If you insert data to table A and then insert data to table B, last_insert_id will change.

Insert multiple records using a single insert statement. last_insert_id returns a list.

@ Identity refers to the value of the auto-incrementing column corresponding to the last time data is inserted into a table with the identity attribute (that is, the auto-incrementing column). It is a global variable defined by the system. For example, if the auto-increment column of Table A is ID, after a row of data is inserted into Table A, if the value of the auto-increment column is automatically increased to 101 after the data is inserted, the value obtained through select @ identity is 101.

Note: last_insert_id is a function.
Usage: last_insert_id ()

Q: last_insert_id () in MySQL and @ identity in MSSQL
A: According to application requirements, you often need to obtain the id value of the records just inserted into the database table.
You can use the last_insert_id () function in MySQL and @ identity in MSSQL. A convenient function.

However, note that when inserting multiple records using the insert statement, last_insert_id () returns the first ID value, while @ identity returns the last record.

Q: mysql_insert_id () and last_insert_id ()
A: mysql_insert_id () converts the return value of MySQL's internal c api function mysql_insert_id () to long (INT in PHP ). If the column type of auto_increment is bigint, the value returned by mysql_insert_id () is incorrect. The SQL function last_insert_id () in MySQL can be used in SQL queries.

MySQL last_insert_id () Description mysql_insert_id () is implemented by calling last_insert_id.

Use last_insert_id ()... in MySQLProgramUse mysql_insert_id ().

Related Article

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.