Oracle Tutorial

  • Home
  • Start Here
  • Basic
  • Advanced
    • Views
    • Indexes
    • Synonyms
    • Sequences
    • Triggers
    • Administration
  • PL/SQL
  • Functions
    • Aggregate Functions
    • Analytic Functions
    • Comparison Functions
    • Date Functions
    • String Functions
Home / Oracle Sequence / Oracle CREATE SEQUENCE

Oracle CREATE SEQUENCE

Summary: in this tutorial, you will learn how to use the Oracle CREATE SEQUENCE statement to create a new sequence in Oracle.

Introduction to Oracle CREATE SEQUENCE statement

The CREATE SEQUENCE statement allows you to create a new sequence in the database.

Here is the basic syntax of the CREATE SEQUENCE statement:

1
2
3
4
5
6
7
8
CREATE SEQUENCE schema_name.sequence_name
[INCREMENT BY interval]
[START WITH first_number]
[MAXVALUE max_value | NOMAXVALUE]
[MINVALUE min_value | NOMINVALUE]
[CYCLE | NOCYCLE]
[CACHE cache_size | NOCACHE]
[ORDER | NOORDER];

CREATE SEQUENCE

Specify the name of the sequence after the CREATE SEQUENCE keywords. If you want to create a sequence in a specific schema, you can specify the schema name in along with the sequence name.

INCREMENT BY

Specify the interval between sequence numbers after the INCREMENT BY keyword.

The interval can have less than 28 digits. It also must be less than MAXVALUE - MINVALUE.

If the interval is positive, the sequence is ascending e.g., 1,2,3,…

If the interval is negative, the sequence is descending e.g., -1, -2, -3 …

The default value of interval is 1.

START WITH

Specify the first number in the sequence.

The default value of the first number is the minimum value of the sequence for an ascending sequence and maximum value of the sequence for a descending sequence.

MAXVALUE

Specify the maximum value of the sequence.

The max_value must be equal to or greater than first_number specify after the START WITH keywords.

NOMAXVALUE

Use NOMAXVALUE to denote a maximum value of 10^27 for an ascending sequence or -1 for a descending sequence. Oracle uses this option as the default.

MINVALUE

Specify the minimum value of the sequence.

The min_value must be less than or equal to the first_number and must be less than max_value.

NOMINVALUE

Use NOMINVALUE to indicate a minimum value of 1 for an ascending sequence or -10^26 for a descending sequence. This is the default.

CYCLE

Use CYCLE to allow the sequence to generate value after it reaches the limit, min value for a descending sequence and max value for an ascending sequence.

When an ascending sequence reaches its maximum value, it generates the minimum value.

On the other hand, when a descending sequence reaches its minimum value, it generates the maximum value.

NOCYCLE

Use NOCYCLE if you want the sequence to stop generating the next value when it reaches its limit. This is the default.

CACHE

Specify the number of sequence values that Oracle will preallocate and keep in the memory for faster access.

The minimum of the cache size is 2. The maximum value of the cache size is based on this formula:

1
(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

In case of a system failure event, you will lose all cached sequence values that have not been used in committed SQL statements.

ORDER

Use ORDER to ensure that Oracle will generate the sequence numbers in order of request.

This option is useful if you are using Oracle Real Application Clusters. When you are using exclusive mode, then Oracle will always generate sequence numbers in order.

NOORDER

Use NOORDER if you do not want to ensure Oracle to generate sequence numbers in order of request. This option is the default.

Oracle CREATE SEQUENCE statement examples

Let’s take some example of using sequences.

1) Basic Oracle Sequence example

The following statement creates an ascending sequence called id_seq, starting from 10, incrementing by 10, minimum value 10, maximum value 100. The sequence returns 10 once it reaches 100 because of the CYCLE option.

1
2
3
4
5
6
7
CREATE SEQUENCE id_seq
    INCREMENT BY 10
    START WITH 10
    MINVALUE 10
    MAXVALUE 100
    CYCLE
    CACHE 2;

To get the next value of the sequence, you use the NEXTVAL pseudo-column:

1
2
3
4
SELECT
    id_seq.NEXTVAL
FROM
    dual;

Here is the output:

1
2
3
    NEXTVAL
----------
        10        

To get the current value of the sequence, you use the CURRVAL pseudo-column:

1
2
3
4
SELECT
    id_seq.CURRVAL
FROM
    dual;

The current value is 10:

1
2
3
    CURRVAL
----------
        10

This SELECT statement uses the id_seq.NEXTVAL value repeatedly:

1
2
3
4
5
SELECT
    id_seq.NEXTVAL
FROM
    dual
CONNECT BY level <= 9;

Here is the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
    NEXTVAL
----------
        20
        30
        40
        50
        60
        70
        80
        90
    100
 
9 rows selected

Because we set the CYCLE option for the id_seq sequence, the next value of the id_seq will be 10:

1
SELECT id_seq.NEXTVAL FROM dual;

And here is the output:

1
2
3
    NEXTVAL
----------
        10

2) Using a sequence in a table column example

Prior Oracle 12c, you can associate a sequence indirectly with a table column only at the insert time.

See the following example.

First, create a new table called tasks:

1
2
3
4
CREATE TABLE tasks(
    id NUMBER PRIMARY KEY,
    title VARCHAR2(255) NOT NULL
);

Second, create a sequence for the id column of the tasks table:

1
CREATE SEQUENCE task_id_seq;

Third, insert data into the tasks table:

1
2
3
4
5
INSERT INTO tasks(id, title)
VALUES(task_id_seq.NEXTVAL, 'Create Sequence in Oracle');
 
INSERT INTO tasks(id, title)
VALUES(task_id_seq.NEXTVAL, 'Examine Sequence Values');

Finally, query data from the tasks table:

1
2
3
4
SELECT  
    id, title
FROM
    tasks;

Oracle Create Sequence example

In this example, the tasks table has no direct association with the task_id_seq sequence.

3) Using the sequence via the identity column example

From Oracle 12c, you can associate a sequence with a table column via the identity column.

First, drop the tasks table:

1
DROP TABLE tasks;

Second, recreate the tasks table using the identity column for the id column:

1
2
3
4
CREATE TABLE tasks(
    id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    title VARCHAR2(255) NOT NULL
);

Behind the scenes, Oracle creates a sequence that associates with the id column of the tasks table.

Because Oracle generated the sequence automatically for the id column, in your Oracle instance, the name of the sequence may be different.

Oracle Create Sequence - identity column

Oracle uses the sys.idnseq$ to store the link between the table and the sequence.

This query returns the association of the tasks table and ISEQ$$_74366 sequence:

1
2
3
4
5
6
7
8
9
SELECT
    a.name AS table_name,
    b.name AS sequence_name
FROM  
    sys.idnseq$ c
    JOIN obj$ a ON c.obj# = a.obj#
    JOIN obj$ b ON c.seqobj# = b.obj#
WHERE
    a.name = 'TASKS';  

Third, insert some rows into the tasks table:

1
2
3
4
5
INSERT INTO tasks(title)
VALUES('Learn Oracle identity column in 12c');
 
INSERT INTO tasks(title)
VALUES('Verify contents of the tasks table');

Finally, query data from the tasks table:

1
2
3
4
SELECT
    id, title
FROM
    tasks;

Oracle Create Sequence - oracle identity column example

In this tutorial, you have learned how to use the Oracle CREATE SEQUENCE statement to create a new sequence in the database.

  • Was this tutorial helpful?
  • YesNo
Next Tutorial: Oracle ALTER SEQUENCE

Getting Started

  • What Is Oracle Database
  • Install Oracle Database Server
  • Download Oracle Sample Database
  • Create Oracle Sample Database
  • Connect To Oracle Database Server

Oracle Data Manipulation

  • SELECT
  • Oracle DUAL Table
  • ORDER BY
  • SELECT DISTINCT
  • WHERE
  • Table & Column Aliases
  • AND
  • OR
  • FETCH
  • BETWEEN
  • IN
  • LIKE
  • IS NULL
  • Joins
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
  • Self Join
  • GROUP BY
  • HAVING
  • UNION
  • INTERSECT
  • MINUS
  • GROUPING SETS
  • CUBE
  • ROLLUP
  • PIVOT
  • UNPIVOT
  • INSERT
  • INSERT INTO SELECT
  • INSERT ALL
  • UPDATE
  • DELETE
  • MERGE
  • Subquery
  • Correlated Subquery
  • EXISTS
  • NOT EXISTS
  • ANY
  • ALL

Oracle Data Types

  • Oracle Data Types
  • NUMBER
  • FLOAT
  • BINARY_FLOAT
  • CHAR
  • NCHAR
  • VARCHAR2
  • NVARCHAR2
  • DATE
  • INTERVAL
  • TIMESTAMP
  • TIMESTAMP WITH TIME ZONE

Oracle Data Definition

  • CREATE TABLE
  • Identity Column
  • ALTER TABLE
  • ALTER TABLE ADD Column
  • ALTER TABLE MODIFY Column
  • Drop Columns
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME Table
  • Oracle Virtual Column

Oracle Constraints

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • CHECK
  • NOT NULL

Oracle Views

  • CREATE VIEW
  • DROP VIEW
  • Updatable Views
  • Inline Views
  • WITH CHECK OPTION

About Oracle Tutorial

OracleTututorial.com website provides Developers and Database Administrators with the updated Oracle tutorials, scripts, and tips.

Search

Recent Tutorials

  • Oracle Books
  • PL/SQL VARRAY
  • PL/SQL Nested Tables
  • PL/SQL Associative Array
  • Mutating Table Error in Oracle

Site Links

  • Oracle Books
  • About
  • Contact
  • Privacy Policy
  • Terms of Use

Copyright © 2019 Oracle Tutorial. All Rights Reserved.

⤒