shsql: sequences

SQL database system
| Manual page for sequences(TDH)

## Sequences

shsql has a built-in convenience facility for allocating unique sequence numbers (serial numbers).`` This may be useful in generating integer key values, or for other purposes.`` In the descriptions below, identifier can be any unique identifier. In practice it is often a table name that will use the resulting sequence numbers as primary key values.``

Turning on sequences for a table is done using `CREATE SEQUENCE ON` identifier.`` For example:

  `$ shsql "create sequence on people"`

To allocate the next available sequence number, use `SELECT _SEQUENCE FROM` identifier.`` For example:

  `$ shsql "select _sequence from people"
  1
  $`

## Notes

The `select _sequence from` construct must be used in the simple form as illustrated.

Sequences are implemented by storing them in a separate table called `_sequences`.`` This table is created upon the first `CREATE SEQUENCE`, and is managed using normal access and locking conventions. The current state of all sequences in a database can be seen by doing `select * from _sequences`

The next allocated value will be one greater than the value seen in the `_sequences` table.``

Normally the first allocated sequence number will be 1, the second 2, and so on.`` You can initialize to a desired value by issuing a command such as this (after the sequence has been created):

    	update _sequences
    	 set value = desirednumber
    	 where tablename = 'tablename'

Resetting or backing up sequences can be done using a similar `UPDATE` command to the above.``

Other than administrative needs, the `_sequences` table shouldn't be accessed directly by applications.``
Copyright Steve Grubb

Markup created by unroff 1.0, `` `` March 18, 2004.