Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I would like to force the auto increment field of a table to some value, I tried with this:

ALTER TABLE product AUTO_INCREMENT = 1453

AND

ALTER SEQUENCE product  RESTART WITH 1453;

ERROR:  relation "your_sequence_name" does not exist

I'm new to postgres :(

I have a table product with Id and name field

1 Answer

0 votes
by (40.7k points)

If you have created the table product with associated id column, then the sequence isn't merely known as product, however rather product_id_seq (that is, ${table}_${column}_seq).

This is the ALTER SEQUENCE command that you need:

ALTER SEQUENCE product_id_seq RESTART WITH 1453

 In PSQL, you can see the sequences in the database by using the \ds command. 

Note: If you are doing \d product and observe the default constraint for your column, the nextval(...) decision call can specify the sequence name too.

If you want to learn sql commands visit these SQL commands cheat sheet.

Related questions

+2 votes
1 answer
asked Jun 24, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 16, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Jul 31, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...