0

How to start my id column from 1 ?

My tabel is :

 id |   origin    | destination | duration
----+-------------+-------------+----------
  6 | Paris       | New York    | 540
  7 | Tokyo       | Shanghai    | 185
  8 | Seoul       | Mexico City | 825
  9 | Mexico City | Lima        | 350
 10 | Hong Kong   | Shanghai    | 130

Here id is my SERIAL KEY and PRIMARY KEY.

And I am using PostgreSQL.

1

1 Answer 1

2

Serial columns are linked to a SEQUENCE. If it is an automatically generated sequence it will be called something like tablename_columnname_id_seq. You can alter a sequences next value using the function SETVAL(sequence_name, number).

However, as you have existing data in the system you will end up with conflicts when the sequence hits 6. You have to update the id column of your existing table, then set the sequence to the biggest value so the next table entry continues the sequence correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Example how to call SETVAL in SQL script: PERFORM SETVAL('table_name_id_seq', 1); NOTE: SELECT statement is not suitable here, because it gets error message: ERROR: query has no destination for result data, so PERFORM is used instead

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.