RefLib

Reference library for AI

Back

postgresql_16:7.1.79.4 Examples

PostgreSQL 16 > Reference > SQL Commands > CREATE SEQUENCE > Examples

Create an ascending sequence called serial, starting at 101: CREATE SEQUENCE serial START 101;

Select the next number from this sequence: SELECT nextval('serial'); nextval --------- 101

Select the next number from this sequence: SELECT nextval('serial'); nextval --------- 102

Use this sequence in an INSERT command: INSERT INTO distributors VALUES (nextval('serial'), 'nothing');

Update the sequence value after a COPY FROM: BEGIN; COPY distributors FROM 'input_file'; SELECT setval('serial', max(id)) FROM distributors; END;

Back