MySQL auto_increment Attribute Application

Source: Internet
Author: User

We often use a unique number to identify the record. The Auto_increment property of the data column can be automatically generated in MySQL. MySQL supports a variety of data tables, each of which has a different self-increment property, which describes the data column self-increment properties in various tables. 1 Usage:
CREATE TABLE  INT notNULLPRIMARYKEYVARCHAR (15  notNULL =

Available when building tables "Auto_increment=n"option to specify a self-increment initial value.
Available ALTER TABLE table_name AUTO_INCREMENT=Ncommand to reset the self-increment starting value.

Description
(1) If you insert a null into a auto_increment data column, MySQL will automatically generate the next serial number. Numbering starts at 1, and 1 increments by cardinality.
(2) inserting 0 into the auto_increment data column has the same effect as inserting a null value. However, it is not recommended to do this, or to insert a null value as well.
(3) when inserting a record, the value is not explicitly specified for auto_increment, which is equivalent to inserting a null value.
(4) When inserting a record, if a numeric value is explicitly specified for the Auto_increment data column, there are two cases where an error message occurs if the inserted value repeats with an existing number because the value of the Auto_increment data column must be unique; In case two, If the inserted value is greater than the numbered value, it is inserted into the data column and the next number is incremented from the new value. In other words, you can skip some numbers.
(5) If the self-increment column is updated with the update command, an error occurs if the column value repeats with the existing value. If it is greater than the existing value, the next number increments from that value. The Oracle database does not support direct setting of primary key auto-increment, which can be implemented by setting the trigger

For example, set the field ID of T_user to self-increment: (Implemented using the method of sequence sequence)

----Create a tableCreate TableT_user (Id Number(6), UserIDvarchar2( -), Loginpasswordvarchar2( -), isdisable Number(6));----Create sequenceCreatesequence User_seqincrement by 1Start with 1Nomaxvaluenominvaluenocache----Create a triggerCreate or Replace TriggerTr_userbeforeInsert  onT_user forEach rowbeginSelectUser_seq.nextval into: new.id fromdual;End;----TestInsert  intoT_user (Userid,loginpassword, isdisable)Values('FFLL','Liudddyujj',0);Insert  intoT_user (Userid,loginpassword, isdisable)Values('dddd','Zhang',0)Select *  fromT_user;

Reference: http://blog.csdn.net/tancaiyi/article/details/4040289

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.