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;In this syntax:
Here is an example of creating a sequence:
CREATE SEQUENCE order_id_seqSTART WITH 1INCREMENT BY 1MINVALUE 1MAXVALUE 9999NOCYCLE;In this example, the sequence named ‘order_id_seq’ begins at 1 and increments by 1 each time a new number is generated. The sequence will not exceed 9999 and will not cycle back to 1 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.
![]() 100% | ![]() Global | ![]() 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. |
![]() 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. |
![]() Global |
International Tuition |
Based in Cambridge, with operations spanning the globe, we can provide our services to support your family anywhere. |
![]() 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. |
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.