Auto increment (Postgresql)

Asked

Viewed 475 times

-1

When I give delete from on the table, then I give select, back to table without insert no, at the time I give the insert again in the table the data id does not restart from the 0.

In other words, it comes like this:

7 
8
9

After you delete the table and give the Insert again, it does not come like this:

1
2
3

He comes:

10
11
12
  • 1

    When adding something, edit your question instead of commenting on the post itself.

  • 4

    Id is a primary key, it’s not to come back at all. If you’re in a test environment and want to come back, you have to go to Quence and change manually.

2 answers

1

This is normal, the command DELETE erases records but does not restart the seed of quence.

The command to change the seed value is this:

ALTER SEQUENCE seq RESTART WITH 1

Dai starts again from 1

  • 1

    Definitely "rewind" to SEQUENCE is a very dangerous practice and should be carried out with sufficient caution. A SEQUENCE serves to meet the need for identificadores únicos for certain information, by "rewinding" the identificadores cease to be únicos and start repeating themselves!

  • 1

    Yes for sure, by the way, the identifier is a field we should "forget" him, IE, not worry about the value that has there, It is something that the bank should use to ensure a unique record. I just asked the question to illustrate the explanation, that although the behavior of the bank is correct, it is possible to change the value of the bank

1

There’s no problem in that!

A chave primária does not need to be sequential and not even begin at a specific value to serve its purpose. A chave primária need only be a identificador único No matter what the hell he is.

The answer of the colleague @Ricardo Pontual suggests the RESTART of SEQUENCE as a possible solution, but it is a rather dangerous practice and should be studied with sufficient caution.

One SEQUENCE serves to meet the need for identificadores únicos for certain information, when "rewind" the identifiers are no longer unique and start repeating themselves.

Now you answer me: What is the impact of a self-improvement that did not start from the number 1 in your system ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.