Implicit sequences were introduced in Oracle 12.1.0.1
In Oracle 12c you can specify sequences with the NEXTVAL or CURRVAL pseudo-columns as default column values. When a new row is inserted into the table, the sequence is automatically incremented and assigned to the column. In this page, I have called this new feature "implicit sequences". However, this name is technically incorrect as the sequences are not themselves implicit, only the assignment of default values to the affected column(s).
CREATE SEQUENCE seq1;In Oracle 12c, as it earlier versions, by default, sequences are created with a cache size of 20. We can then create a table that references the sequence. For example:
CREATE TABLE transaction ( transaction_id NUMBER DEFAULT seq1.NEXTVAL, holding_id NUMBER, transaction_type NUMBER, transaction_date DATE, credit_value NUMBER, debit_value NUMBER );Note that the DEFAULT clause includes the sequence name and the NEXTVAL pseudo-column. When a row is inserted into the table, the TRANSACTION_ID column will automatically be set to the next value in the sequence. Assume that a few rows have already been added to the table. For example:
SELECT MAX(transaction_id) FROM transaction; MAX(TRANSACTION_ID) ------------------- 41
INSERT INTO transaction (holding_id,transaction_type,transaction_date,credit_value,debit_value) VALUES (10,1,SYSDATE,100,0);
SELECT MAX(transaction_id) FROM transaction; MAX(TRANSACTION_ID) ------------------- 42Note that the INSERT statement does not include the TRANSACTION_ID column. The default value for this column is generated internally.
SELECT MAX(transaction_id) FROM transaction; MAX(TRANSACTION_ID) ------------------- 42
INSERT INTO transaction (holding_id,transaction_type,transaction_date,credit_value,debit_value) VALUES (11,2,SYSDATE,0,200);
SELECT MAX(transaction_id) FROM transaction; MAX(TRANSACTION_ID) ------------------- 46
CREATE TABLE transaction ( transaction_id NUMBER DEFAULT us01.seq1.NEXTVAL, holding_id NUMBER, transaction_type NUMBER, transaction_date DATE, credit_value NUMBER, debit_value NUMBER );Specifying an owner for the sequence will increase resilience at the possible expense of flexibility.
CREATE PUBLIC SYNONYM syn1 FOR us01.seq1;
CREATE TABLE transaction ( transaction_id NUMBER DEFAULT syn1.NEXTVAL, holding_id NUMBER, transaction_type NUMBER, transaction_date DATE, credit_value NUMBER, debit_value NUMBER );When the table is created, the synonym will be resolved. The result is stored in the DEFAULT$ column of the COL$ table in the data dictionary. For example:
SELECT default$ FROM sys.col$ WHERE obj# = ( SELECT obj# FROM sys.obj$ WHERE owner# = ( SELECT user# FROM sys.user$ WHERE name = 'US01' ) AND name = 'TRANSACTION' ) AND name = 'TRANSACTION_ID';
DEFAULT$ -------------------------------------------------- "US01"."SEQ1"."NEXTVAL"
ALTER TABLE transaction MODIFY transaction_id DEFAULT seq1.NEXTVAL;
DROP SEQUENCE seq1; CREATE TABLE transaction ( transaction_id NUMBER DEFAULT seq1.NEXTVAL, holding_id NUMBER, transaction_type NUMBER, transaction_date DATE, credit_value NUMBER, debit_value NUMBER ); transaction_id NUMBER DEFAULT seq1.NEXTVAL, * ERROR at line 3: ORA-02289: sequence does not existIf the sequence is accessible when the table is created, but then is subsequently dropped or becomes inaccessible, then an error will be raised the next time a row is inserted. For example:
DROP SEQUENCE seq1; INSERT INTO transaction (holding_id,transaction_type,transaction_date,credit_value,debit_value) VALUES (25,2,SYSDATE,0,300); INSERT INTO transaction * ERROR at line 1: ORA-02289: sequence does not exist
select increment$,minvalue,maxvalue,cycle#,order$,cache, highwater,audit$,flags,partcount from seq$ where obj#=:1When the cache is exhausted the server process is responsible for issuing a recursive transaction to obtain the next batch of sequence numbers.
update seq$ set increment$=:2,minvalue=:3,maxvalue=:4,cycle#=:5,order$=:6,cache=:7, highwater=:8,audit$=:9,flags=:10,partcount=:11 where obj#=:1