Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?
I mean why don’t just insert a record and the rdbms generate ID for you?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading