Auto increment (PostgreSQL)

-1

When I give delete from to the table, then I give select , returns the table without insert none, at the time I give insert again in the table the data id does not restart from 0 / p>

In other words, it's like this:

7 
8
9

After giving delete in the table and giving the insert again, it does not come like this:

1
2
3

It comes:

10
11
12
    
asked by anonymous 05.07.2018 / 14:15

2 answers

1

This is normal, the DELETE command deletes records but does not restart the sequence seed.

The command to change the seed value is this:

ALTER SEQUENCE seq RESTART WITH 1

Dai starts again from 1

    
05.07.2018 / 14:19
1

There's no problem there!

A chave primária need not be sequential and not even start at a specific value to serve its purpose. A chave primária just needs to be identificador único of the registry, no matter what the hell it is.

The response of fellow @Ricardo Pontual suggests RESTART of SEQUENCE as a possible solution, but it is a very dangerous practice and should be studied with caution.

A SEQUENCE serves to meet the requirement of identificadores únicos for certain information, by "rewinding" it, the identifiers cease to be unique and begin to repeat themselves.

Now, what is the impact of a self-increment that did not start from the number 1 on your system?

    
05.07.2018 / 14:47