In today’s article, I will tell you what postgresql sequence is and how it was created.
Sequence is a tool created for self -increasing numbers according to the values given. MSSQL, Oracle and Postgresql databases are also used.
Sequence sample usage is as follows.
CREATE SEQUENCE Sequence name
INCREMENT how to increase
START Start Value
MINVALUE minimum value
MAXVALUE Maximum value
CACHE Cache value to be held
CYCLE If the process to exceed the given maximum value is Cycle, when the maximum value exceeds, it starts from the value given to the start parameter again. No Cycle does not allow to start again.
OWNED BY Tablename, Columnname: Used to specify which table is used for the OWNED by parameter.
Sequence cannot be used when the table is deleted.
You can use the following command to create a sample sequence.
1 2 3 4 5 6 7 |
CREATE SEQUENCE public.seqdeneme INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 5 CACHE 2 NO CYCLE ; |
Let’s use Sequence in the table. Let’s create a two -column table called seqtable.
1 |
create table seqtable (id integer ,name char(100)) |
We have created the table and now let’s insert data using Sequence.
1 2 3 |
insert into seqtable values (nextval('seqdeneme'),'faruk') insert into seqtable values (nextval('seqdeneme'),'erdem') |
We inserted the data, but we used a different parameter here “Nextval” This parameter gives the next value. Let’s see how it was inserted by pulling select from our inserted table.
As seen above, we see that our data is inserted sequentially.
You can see the next number to be added with the help of the following command.
1 |
Select nextval('sequence_name') |