Skip to Content
Discovering SQL: A Hands-On Guide for Beginners
book

Discovering SQL: A Hands-On Guide for Beginners

by Alex Kriegel
April 2011
Beginner
432 pages
12h 14m
English
Wrox
Content preview from Discovering SQL: A Hands-On Guide for Beginners

SEQUENCES

Unlike identity columns, sequences are independent RDBMS objects, not tied to a specific table. They have been implemented in Oracle, IBM DB2, PostgreSQL, and HSQLDB/OpenOffice BASE. More importantly, they have made it into the SQL Standard Committee.

The basic syntax for creating a sequence is identical across those RDBMSs supporting sequences (some might insist on adding the SCHEMA name as the first part of the sequence name). To create a generator that begins with 1, and generates sequential values in increments of 1, all you have to do is to issue this statement:

CREATE SEQUENCE seq_books;

Of course, there are quite a few optional clauses that can go with this statement: You can specify initial value, increment value, max and minimum values, the order (descending or ascending), and so on. Once a sequence is created, it can be referenced from an SQL statement by name, and its properties provide access to the current, next, and previous values.

Here are some examples of how the sequence created in the previous example can be used:

SELECT seq_books.NEXTVAL as next_value 
FROM dual;

next_value
-----------------
1

SELECT seq_books.CURRVAL as current_value 
FROM dual;

current_value
-----------------
1

SELECT seq_books.NEXTVAL as next_value 
FROM dual;

next_value
-----------------
2

The DUAL table is provided by Oracle to SELECT from when you really do not need any data. IDB DB2 is even more forthright; it wants you to select from SYSIBM.SYSDUMMY1 so you'll never forget ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn SQL Database Programming

Learn SQL Database Programming

Josephine Bush
The Complete Python and PostgreSQL Developer Course

The Complete Python and PostgreSQL Developer Course

Codestars By Rob Percival, Jose Salvatierra Fuentes

Publisher Resources

ISBN: 9781118002674Purchase book