PostgreSQL Sequence
Sequence:
It is used to automatically generate the number(unique).I generate serial list of unique number for numerical column of database tables.Sequence Options:
TEMPORARY or TEMP //If specified, the sequence object is created only for this session and is automatically dropped on session exit.IF NOT EXISTS //Do not throw an error if a relation with the same name already exists.
A notice is issued in this case. Note that there is no guarantee that
the existing relation is anything like the sequence that would have been created - it might not even be a sequence.
NAME //The name of the sequence to be created.
INCREMENT //The optional clause INCREMENT BY increment specifies which value is added to the current sequence value to create a new value.
A positive value will make an ascending sequence, a negative one a descending sequence. The default value is 1.
MINVALUE
NO MINVALUE //The optional clause MINVALUE minvalue determines the minimum value a sequence can generate.
If this clause is not supplied or NO MINVALUE is specified, then defaults will be used.
The default for an ascending sequence is 1. The default for a descending sequence is the minimum value of the data type.
MAXVALUE
NO MAXVALUE //The optional clause MAXVALUE maxvalue determines the maximum value for the sequence.
If this clause is not supplied or NO MAXVALUE is specified, then default values will be used.
The default for an ascending sequence is the maximum value of the data type.
The default for a descending sequence is -1.
START //The optional clause START WITH start allows the sequence to begin anywhere.
The default starting value is minvalue for ascending sequences and maxvalue for descending ones.
CACHE //The optional clause CACHE cache specifies how many sequence numbers are to be preallocated and stored in memory for faster access.
The minimum value is 1 (only one value can be generated at a time, i.e., no cache), and this is also the default.
CYCLE
NO CYCLE //The CYCLE option allows the sequence to wrap around when the maxvalue or minvalue has been reached by an ascending or descending sequence respectively.
If the limit is reached, the next number generated will be the minvalue or maxvalue, respectively.
If NO CYCLE is specified, any calls to nextval after the sequence has reached its maximum value will return an error.
If neither CYCLE or NO CYCLE are specified, NO CYCLE is the default.
OWNED BY table_name.column_name
OWNED BY NONE //The OWNED BY option causes the sequence to be associated with a specific table column,
such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well.
The specified table must have the same owner and be in the same schema as the sequence.
OWNED BY NONE, the default, specifies that there is no such association.
Example:
Sample Table:
pearl=# create table muthu(id int,name text);CREATE TABLE
pearl=# select * from muthu;
id | name
----+------
(0 rows)
pearl=# create sequence muthu_id start 1 increment 1 minvalue 1 maxvalue 5 cycle;
CREATE SEQUENCE
pearl=# insert into muthu values (nextval('muthu_id'),'siva');
INSERT 0 1
pearl=# insert into muthu values (nextval('muthu_id'),'regan');
INSERT 0 1
pearl=# insert into muthu values (nextval('muthu_id'),'chandru');
INSERT 0 1
pearl=# insert into muthu values (nextval('muthu_id'),'thanigai');
INSERT 0 1
pearl=# insert into muthu values (nextval('muthu_id'),'kathir');
INSERT 0 1
pearl=# insert into muthu values (nextval('muthu_id'),'jana');
INSERT 0 1
pearl=# select * from muthu;
id | name
----+----------
1 | siva
2 | regan
3 | chandru
4 | thanigai
5 | kathir
1 | jana
(6 rows)
Comments
Post a Comment