Home > OS >  What is the benefit to use sequence rather than just insert and get generated ID in postgresql?
What is the benefit to use sequence rather than just insert and get generated ID in postgresql?

Time:09-22

What is the benefit to use sequence rather than just insert and get generated ID in postgresql ? I mean why don't just insert a record and the rdbms generate ID for you?

CodePudding user response:

For most purposes, SERIAL does the same thing as a sequence can do. However, if you wanted your auto increment column to have a custom behavior, you might have to use a sequence. For example, let's say that you wanted the sequence to start at 100. Then you could use:

CREATE SEQUENCE your_seq
    START 101
    INCREMENT 1;

INSERT INTO yourTable (id, val)
VALUES (NEXTVAL('your_seq'), 'some text here');
  • Related