.ig >>
<STYLE TYPE="text/css">
<!--
        A:link{text-decoration:none}
        A:visited{text-decoration:none}
        A:active{text-decoration:none}
        OL,UL,P,BODY,TD,TR,TH,FORM { font-family: arial,helvetica,sans-serif;; font-size:small; color: #333333; }

        H1 { font-size: x-large; font-family: arial,helvetica,sans-serif; }
        H2 { font-size: large; font-family: arial,helvetica,sans-serif; }
        H3 { font-size: medium; font-family: arial,helvetica,sans-serif; }
        H4 { font-size: small; font-family: arial,helvetica,sans-serif; }
-->
</STYLE>
<title>shsql: sequences</title>
<body bgcolor=99cc99 vlink=0000FF>
<br>
<br>
<center>
<table cellpadding=2 bgcolor=FFFFFF width=550 ><tr>
<td align=right><a href="shsql_home.html">
<img src="img/shsql.gif" border=0><br><small>SQL database system</a> &nbsp; </td></tr>
<td>
.>>

.TH sequences TDH "18-MAR-2004   TDH scg@jax.org" 

.SH Sequences
\fBshsql\fR 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,
\fIidentifier\fR can be any unique identifier.  In practice it is often a table 
name that will use the resulting sequence numbers as primary key values.

.ig >>
<br><br><br>
.>>

.LP
Turning on sequences for a table is done using \fCCREATE SEQUENCE ON \fIidentifier\fR.
For example:
.IP
\fC$ shsql "create sequence on people"\fR
.LP

.ig >>
<br><br><br>
.>>

.LP
To allocate the next available sequence number, use \fCSELECT _SEQUENCE FROM \fIidentifier\fR.
For example:
.IP
\fC$ shsql "select _sequence from people"
.br
1
.br
$ \fR
.LP

.ig >>
<br><br><br>
.>>
.SH Notes
.LP
The \fCselect _sequence from\fR construct must be used in the simple form
as illustrated.  

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

.LP
The next allocated value will be one greater than the value seen in the \fC_sequences\fR table.

.LP
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):
.nf
	update _sequences
	 set value = \fIdesirednumber\fR
	 where tablename = '\fItablename\fR'

.LP
Resetting or backing up sequences can be done using a similar \fCUPDATE\fR command
to the above.

.LP
Other than administrative needs, the \fC_sequences\fR table shouldn't be
accessed directly by applications.


.ig >>
<br>
<br>
</td></tr>
<td align=right>
<a href="shsql_home.html">
<img src="img/shsql.gif" border=0></a><br>
<a href="Copyright.html">Copyright Steve Grubb</a> &nbsp;
</td></tr>
</table>
.>>

