Sequences on Oracle

Here is an example of how to create a sequence which you can use for creating sequential numbers, typically for ID’s.

CREATE SEQUENCE person_seq
    INCREMENT BY 1
    START WITH 1
    NOMAXVALUE
    NOCYCLE
    CACHE 10;

This is how you would then use the sequence.

INSERT INTO persons (id, name) VALUES (person_seq.NEXTVAL, 'Dylan')
INSERT INTO persons (id, name) VALUES (person_seq.NEXTVAL, 'Isaac')

If you want to select the next ID you would do it like this.

SELECT person_seq.NEXTVAL FROM Dual

To learn more, Managing Sequences and the Oracle DUAL table.

Leave a Reply

Your email address will not be published. Required fields are marked *