9951 explained code solutions for 126 technologies


postgresqlHow can I use the PostgreSQL nextval function?


The PostgreSQL nextval function can be used to generate a sequence of unique values. It is typically used to create a unique identifier for a row in a table.

Example

CREATE SEQUENCE my_sequence START 1;
SELECT nextval('my_sequence');

Output example

1

The code above creates a sequence called my_sequence starting at 1 and then generates the next value in the sequence.

The code can be modified to set different start values, increments, min/max values, and caching.

Parts of the code:

  • CREATE SEQUENCE - Creates a sequence with a given name
  • START - Sets the starting value for the sequence
  • SELECT nextval - Generates the next value in the sequence

Helpful links

Edit this code on GitHub