Oracle Create Sequence: SQL Guide

To create a sequence in Oracle, you can use the following SQL statement:

CREATE SEQUENCE sequence_name
START WITH 1
INCREMENT BY 1
MINVALUE 1
NOCACHE;

Among them, sequence_name is the name of the sequence, START WITH specifies the starting value of the sequence, INCREMENT BY specifies the value to increment by each time, MINVALUE specifies the minimum value of the sequence, and NOCACHE means that the sequence values are not cached.

After creating a sequence, you can use the following SQL statement to retrieve the next value of the sequence:

SELECT sequence_name.NEXTVAL FROM dual;

If you need to use a sequence in a table, you can specify the default value to be the next value in the sequence when creating the table.

CREATE TABLE table_name (
    id NUMBER DEFAULT sequence_name.NEXTVAL,
    column1 VARCHAR2(50),
    column2 NUMBER
);
bannerAds