Skip to main content

Posts

Showing posts with the label Oracle database use of sequences for incrementing values.

Oracle database use of sequences for incrementing values.

We always find a need to use incremental sequence for our table data columns like primary key or other unique keys. Examples of such fields could be id fields like employee id or order sequences for order processing in OLTP databases. Oracle provides very efficient non blocking way of getting and generating these new sequences with a database object called sequence. Sequence should be created and used as follows. CREATE SEQUENCE employee_id_seq START WITH 10000 -- Start your sequence with 10000 INCREMENT BY 1 -- Sequence would be incremented by 1 when nextvalue is fetched from sequence NOCYCLE; -- Oracle sequence would not restart -- If you want sequence to start over after N then specify maxvalue N clause in the sequence Full syntax of sequence statement Once we are done creating sequence object in database next thing is pretty straightforward. You can use your sequence immediately in insert statement. INSERT INTO EM...