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:
- CREATE SEQUENCESequence_name
- INCREMENTBYStep
- 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:
- Sequence_name.CURRVAL
- 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:
- CREATE SEQUENCESeq_PersonId
- MINVALUE 0
- INCREMENTBY1
- 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:
- CREATE TABLET_Person
- (
- FId NUMBER (10)PRIMARY KEY,
- FName VARCHAR2 (20 ),
- FAge NUMBER (10)
- );
Execute the precedingSQLThe statement is created successfully.T_PersonAnd then execute the followingSQLStatement directionT_PersonInsert some data into the table:
- INSERT INTOT_Person (FId, FName, FAge)
- VALUES(Seq_PersonId.NEXTVAL,'Tom', 18 );
- INSERT INTOT_Person (FId, FName, FAge)
- VALUES(Seq_PersonId.NEXTVAL,'Jim', 81 );
- INSERT INTOT_Person (FId, FName, FAge)
- VALUES(Seq_PersonId.NEXTVAL,'Kerry', 33 );