Berkeley DB Reference Version 5.3.33

Using Sequences

create_sequence

nextval

currval

drop_sequence

You can use sequences with the SQL API. Sequences provide for an arbitrary number of increasing or decreasing integers that persist across database accesses. Use sequences if you need to create unique values in a highly efficient and persistent way.

To create and access a sequence, you must use SQL functionality that is unique to the BDB SQL interface; no corresponding functionality exists in SQLite. The sequence functionality is implemented using SQLite function plugins, as such it is necessary to use the 'select' keyword as a prefix to all sequence APIs.

The SQL API sequence support is a partial implementation of the sequence API defined in the SQL 2003 specification.

The following sections describe the BDB SQL interface sequence API.

create_sequence

Creates a new sequence. A name is required, all other parameters are optional. For example:

SELECT create_sequence("my_sequence", "start", 100, "incr", 10, 
                       "maxvalue", 300);

This creates a sequence called my_sequence starting at 100 and incrementing by 10 until it reaches 300.

SELECT create_sequence("my_decr_sequence", "incr", -100, 
                       "minvalue", -10000);

This creates a sequence call my_decr_sequence starting at 0 and decreasing by 100 until it reaches -10000.

Parameters are:

nextval

Retrieves the next value from the named sequence. For example:

SELECT nextval("my_sequence");

currval

Retrieves the last value that was returned from the named sequence. For example:

SELECT currval("my_sequence");

drop_sequence

Removes the sequence. For example:

SELECT drop_sequence("my_sequence");