Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

CREATE SEQUENCE

Name

CREATE SEQUENCE -- Creates a new sequence number generator.

Synopsis

  CREATE SEQUENCE seqname [ INCREMENT increment ]
         [ MINVALUE minvalue ] [ MAXVALUE maxvalue ]
         [ START start ] [ CACHE cache ] [ CYCLE ]

Parameters

seqname

The name of the new sequence.

increment

The value to be applied on each sequence increment. Specify a positive number to make an ascending sequence; specify a negative number to make a descending sequence.

minvalue

The minimum value the new sequence will generate. The default minimum is 1 for an ascending sequence and –2147483647 for a descending sequence.

maxvalue

The maximum value the new sequence will generate. The default is 2147483647 for an ascending sequence, and –1 for a descending sequence.

start

The starting value of the sequence. By default, an ascending sequence will start at minvalue, and a descending sequence will start at maxvalue.

cache

The quantity of sequence numbers that can be stored in cache memory. Using a cache value greater than 1 will speed up performance, because some calls for new sequence values will be satisfied from the cache. By default, the cache value is set at 1, which forces generation of one sequence number at a time (by default, cache is not used). Set it to a number higher than 1 to enable the use of caching.

CYCLE

Use this keyword to enable wrapping. When wrapping is enabled, a sequence can wrap around past its minimum or maximum value and begin again at its minimum or maximum value. The direction of the wrap depends on whether a sequence is ascending or descending.

Results

CREATE

The message returned when a sequence is created successfully.

ERROR: Relation 'seqname' already exists

The error returned if the sequence already exists.

ERROR: DefineSequence: MINVALUE (start) can't be >= MAXVALUE (max)

The error returned if the sequence's minimum starting value is out of range.

ERROR: DefineSequence: START value (start) can't be < MINVALUE (min)

The error returned if the starting value is out of range.

ERROR: DefineSequence: MINVALUE (min) can't be >= MAXVALUE (max)

The error returned if the minimum and maximum values are incompatible.

Description

Use the CREATE SEQUENCE command to create a new sequence number generator into the database.

Examples

This example demonstrates the creation of a sequence named shipments_ship_id_seq:

booktown=# CREATE SEQUENCE shipments_ship_id_seq
booktown-#                 START 200 INCREMENT 1;
CREATE

Once created, you can select the next number from a sequence with the nextval() function:

booktown=# SELECT nextval ('shipments_ship_id_seq');
 nextval
---------
     200
(1 row)

You can also use a sequence in an INSERT command:

booktown=# INSERT INTO shipments VALUES 
booktown-#        (nextval('shipments_ship_id_seq'), 107, '0394800753', 'now');

 
 
  Published courtesy of O'Reilly Design by Interspire