Auto-increment field in Oracle

Source: Internet
Author: User

Environment: PLSQL Developer 7.1.5 Oracle 11.2.0

Oracle does not specify an auto-increment column as MYSQL or MSSQLServer does. However, in Oracle, the SEQUENCE can be used to automatically increase fields. In Oracle, SEQUENCE is called a SEQUENCE. It is automatically increased every time it is obtained. It is generally used in places where the SEQUENCE numbers need to be sorted.

Before using SEQUENCE, you must first define a SEQUENCE. The syntax for defining SEQUENCE is as follows:

  1. CREATE SEQUENCESequence_name
  2. INCREMENTBYStep
  3. STARTWITHStartvalue;

WhereSequence_nameIt is the name of a sequence. Each sequence must have a unique name;StartvalueParametersThe value is the starting number,StepThe parameter value is the step size, that is, the value that is added each time it increases automatically. OnceSEQUENCEYou can useCURRVALTo obtainSEQUENCEYou can also useNEXTVALTo addSEQUENCEAnd then returnNewSEQUENCEValue. For example:

  1. Sequence_name.CURRVAL
  2. Sequence_name.NEXTVAL

IfSEQUENCEYou can delete it if you do not need it:

Drop sequence sequence_name;

The following example usesSEQUENCEAn example of Automatic sequence growth. First, createSeq_PersonIdOfSEQUENCE:

  1. CREATE SEQUENCESeq_PersonId
  2. MINVALUE 0
  3. INCREMENTBY1
  4. STARTWITH0;

Note: If this sentence is not added (MINVALUE 0), This error may occur (ORA-04006: STARTCannot be lessMINVALUE). The solution is to specify the minimum value.

Then createT_PersonTable:

  1. CREATE TABLET_Person
  2. (
  3. FId NUMBER (10)PRIMARY KEY,
  4. FName VARCHAR2 (20 ),
  5. FAge NUMBER (10)
  6. );

Execute the precedingSQLThe statement is created successfully.T_PersonAnd then execute the followingSQLStatement directionT_PersonInsert some data into the table:

  1. INSERT INTOT_Person (FId, FName, FAge)
  2. VALUES(Seq_PersonId.NEXTVAL,'Tom', 18 );
  3. INSERT INTOT_Person (FId, FName, FAge)
  4. VALUES(Seq_PersonId.NEXTVAL,'Jim', 81 );
  5. INSERT INTOT_Person (FId, FName, FAge)
  6. VALUES(Seq_PersonId.NEXTVAL,'Kerry', 33 );
  • 1
  • 2
  • Next Page

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.