Background image of landing

Unrivalled
Education
Solutions for your
Family

How do you create a sequence in SQL?

You can create a sequence in SQL using the ‘CREATE SEQUENCE’ statement, followed by the sequence name and its parameters.

In SQL, a sequence is a specialized database object that generates a series of integers. Sequences are commonly used to automatically produce primary key values. To create a sequence, you use the ‘CREATE SEQUENCE’ statement. The basic syntax is as follows:

CREATE SEQUENCE sequence_nameSTART WITH initial_valueINCREMENT BY increment_valueMINVALUE minimum_valueMAXVALUE maximum_valueCYCLE | NOCYCLE;\begin{aligned} \text{CREATE SEQUENCE sequence\_name} \\ \text{START WITH initial\_value} \\ \text{INCREMENT BY increment\_value} \\ \text{MINVALUE minimum\_value} \\ \text{MAXVALUE maximum\_value} \\ \text{CYCLE | NOCYCLE;} \end{aligned}

In this syntax:

  • ‘sequence_name’ is the designated name for the sequence you are creating.
  • The ‘START WITH initial_value’ clause specifies the initial value of the sequence.
  • The ‘INCREMENT BY increment_value’ clause defines the interval between successive sequence numbers.
  • The ‘MINVALUE minimum_value’ and ‘MAXVALUE maximum_value’ clauses set the boundaries of the sequence.
  • The ‘CYCLE’ option allows the sequence to restart from the minimum value once the maximum value is exceeded, while ‘NOCYCLE’ prevents this behavior.

Here is an example of creating a sequence:

CREATE SEQUENCE order_id_seqSTART WITH 1INCREMENT BY 1MINVALUE 1MAXVALUE 9999NOCYCLE;\begin{aligned} \text{CREATE SEQUENCE order\_id\_seq} \\ \text{START WITH 1} \\ \text{INCREMENT BY 1} \\ \text{MINVALUE 1} \\ \text{MAXVALUE 9999} \\ \text{NOCYCLE;} \end{aligned}

In this example, the sequence named ‘order_id_seq’ begins at 11 and increments by 11 each time a new number is generated. The sequence will not exceed 99999999 and will not cycle back to 11 when it reaches the maximum value.

Once a sequence is established, you can use the ‘NEXTVAL’ and ‘CURRVAL’ functions to generate and retrieve sequence numbers. The ‘NEXTVAL’ function increments the sequence and returns the new value, while ‘CURRVAL’ returns the current value of the sequence without incrementing it.

It’s important to note that sequences are persistent database objects. This means they remain available across different sessions and transactions. Even if a transaction is rolled back, the sequence number generated during that transaction is not returned to the sequence, ensuring the uniqueness of each generated number.

Answered by: Dr. Sarah Collins
A-Level Economics Tutor
Medal Icon

100%

Globe Icon

Global

Crest Icon

97%

Professional Tutors

International Tuition

Independent School Entrance Success

All of our elite tutors are full-time professionals, with at least five years of tuition experience and over 5000 accrued teaching hours in their subject.

Based in Cambridge, with operations spanning the globe, we can provide our services to support your family anywhere.

Our families consistently gain offers from at least one of their target schools, including Eton, Harrow, Wellington and Wycombe Abbey.

Medal Icon

100%

Professional Tutors

All of our elite tutors are full-time professionals, with at least five years of tuition experience and over 5000 accrued teaching hours in their subject.

Globe Icon

Global

International Tuition

Based in Cambridge, with operations spanning the globe, we can provide our services to support your family anywhere.

Crest Icon

97%

Independent School Entrance Success

Our families consistently gain offers from at least one of their target schools, including Eton, Harrow, Wellington and Wycombe Abbey.

Book a free
30-minute consultation
session

At the Beyond Tutors we recognise that no two students are the same. 

That’s why we’ve transcended the traditional online tutoring model of cookie-cutter solutions to intricate educational problems. Instead, we devise a bespoke tutoring plan for each individual student, to support you on your path to academic success.

To help us understand your unique educational needs, we provide a free 30-minute consultation with one of our founding partners, so we can devise the tutoring plan that’s right for you.

To ensure we can best prepare for this consultation, we ask you to fill out the short form below.

Hire a Tutor

All the form fields are optional, but we ask you to provide as much information as possible so that we are in a better position to quickly meet your tutoring requirements.

Still have questions?
Let's get in touch