About MySQL AUTO_INCREMENT

Source: Internet
Author: User

MySQL AUTO_INCREMENT introduction you can use composite indexes to create multiple independent auto-incrementing sequences in the same data table. The specific method is as follows: create a primary key or unique index composed of multiple data columns for the data table, and include the AUTO_INCREMENT data column in this index as its last data column. In this way, each of the preceding data columns in the composite index constitutes a unique combination. The AUTO_INCREMENT data column at the end generates a sequence number corresponding to the combination. We often use unique numbers to identify records. In MySQL, The AUTO_INCREMENT attribute of the data column can be automatically generated. MySQL supports multiple data tables, and the auto-increment attributes of each data table are different. Here we will introduce the auto-increment attributes of Data columns in various data tables. 1 usage:

CREATE TABLE test(id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,username VARCHAR(15) NOT NULL)AUTO_INCREMENT = 100;

 

You can use the "AUTO_INCREMENT = n" option to specify an auto-increment initial value when creating a table. You can use the alter table table_name AUTO_INCREMENT = n command to reset the start value of auto-increment. Note: (1) If a NULL value is inserted into an AUTO_INCREMENT data column, MySQL automatically generates the next sequence number. The number starts from 1 and increases with 1 as the base number. (2) Insert 0 into the AUTO_INCREMENT data column to be the same effect as insert NULL value. However, it is not recommended to insert NULL values. (3) When a record is inserted, if the value is not explicitly specified for AUTO_INCREMENT, it is equivalent to inserting a NULL value. (4) When a record is inserted, if a value is explicitly specified for the AUTO_INCREMENT data column, there are two situations. Case 1: If the inserted value is the same as the existing number, an error occurs because the value of the AUTO_INCREMENT data column must be unique. Case 2: If the inserted value is greater than the numbered value, the inserted value is inserted into the data column, and the next number will increase progressively from the new value. That is to say, you can skip some numbers. (5) If you use the UPDATE command to UPDATE the auto-increment column, if the column value is the same as the existing value, an error will occur. If the value is greater than the existing value, the next number increases progressively from this value.

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.