Chapter 7: Sequence
Sequence is a Database Object, you can use it to generates sequential integers that organizations can use to assist with internal control or simply to serve as primary keys for tables.
Create Sequence
CREATE SEQUENCE sequencename
[INCREMENT BY value]
[START WITH value]
[{MAXVALUE value | NOMAXVALUE}]
[{MINVALUE value | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{ORDER | NOORDER}]
[{CACHE value | NOCACHE}]The naming convention is including
_seqat the end of the name to make it easier to identify as a sequence.INCREMENT BY: specifies the interval between two sequential values. The default value is1. If the sequence is incremented by a positive value, the values generated by the sequence are in ascending order. However, values generated by the sequence are in descending order when a negative value are assigned to this option.START WITH: specifies the starting value for the sequence. The default value is1in a ascending order,-1in a descending order.MAXVALUE: establish a maximum of value for the sequence. If a nagative increment is used and you setMINVALUE,MAXVALUEmust also be set. Typecally,MAXVALUEis to set the same value as theSTART WITHNOMAXVALUE: it is default option if you do not specifyMAXVALUE.-1is the highest value for a descending sequence.10^27is the highest value for a ascending sequence.
MINVALUE: establish a minimum of value for the sequence. it does not make sense when the sequence increments by a positive value.NOMINVALUE: it is default option if you do not specifyMINVALUE.1is the lowest value for a ascending sequence.-10^26is the lowest value for a descending sequence.
CYCLE: reissue values from sequences after reaching theMINVALUEorMAXVALUEoption.NOCYCLE: oracle server does not generate value after reachingMINVALUEorMAXVALUEoption. And return an error message.ORDERandNOORDER: Used in application cluster environments.ORDERoption instructs oracle server to return sequence values in the same order in which requests are received.NOORDERis the default option. However, there are no problem to generate unique value for each request.
CACHE: Used to have oracle server generate a set of values ahead of time and store them in the memory.If
CACHEis not specified, the default option ofCACHE 20will be assigned.These generated values are lost if the system crashes or users do not use the values.
NOCACHE: means each number is generated when the request is received.
Using Sequence Value
NEXTVAL: Used to actually generate value from a sequence. In the other words, it calls the sequence object and requests the next value in a sequence.
CURRVAL: Used to store the current value of the sequence so that you can reference it again.
Setting Sequence Value
Oracle 12c allows a sequence value to be set as default value of a column.
The sequence must be created before being referenced in the default expression of a column.
If the sequence is dropped, the default expression referenceing the sequence must also be removed to avoid errors upon insert operations affecting the column.
If a value is provided in the insert operation, the default sequence value will not be used.
Altering Sequence Definition
Any changes are applied to value generated after the modifications are made.
START WITHcan not be changed because the sequence would have to be dropped and re-created to make this change.The changes could not make the previously issued sequence value invalid. (For exmaple, you can not set the
MAXVALUEto a number less than a number that had already been generated).
Removing Sequence
When a sequence is dropped, it does not affect any value previously generated and stored in a database table.
Identity Column
An Identity Column is particularly suited for use as primary key column values in which random or surrogate values are needed to serve as a unique id for each row.
only one
Identity Columnis allowed in one table.the
DEFAULTclause can not be assigned to the same column inCREATE TABLEorALTER TABLE. (AnIdentity Columnautomatically creates a sequence and default setting for the column).A
NOT NULLconstraint andUNIQUEconstraint are automatically applied to the column.An
Identity Columnmust use theNUMBERdata type.A value can be assigned to insert into the column.
Last updated
Was this helpful?