mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 15:48:01 +00:00
Add Restarting Sequences When Truncating Tables as a postgres til.
This commit is contained in:
@@ -145,6 +145,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
||||
- [List Database Users](postgres/list-database-users.md)
|
||||
- [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md)
|
||||
- [Restart A Sequence](postgres/restart-a-sequence.md)
|
||||
- [Restarting Sequences When Truncating Tables](postgres/restarting-sequences-when-truncating-tables.md)
|
||||
- [Send A Command To psql](postgres/send-a-command-to-psql.md)
|
||||
- [Set Inclusion With hstore](postgres/set-inclusion-with-hstore.md)
|
||||
- [Special Math Operators](postgres/special-math-operators.md)
|
||||
|
||||
15
postgres/restarting-sequences-when-truncating-tables.md
Normal file
15
postgres/restarting-sequences-when-truncating-tables.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# Restarting Sequences When Truncating Tables
|
||||
|
||||
PostgreSQL's
|
||||
[`truncate`](http://www.postgresql.org/docs/current/static/sql-truncate.html)
|
||||
feature is a handy way to clear out all the data from a table. If you use
|
||||
`truncate` on a table that has a `serial` primary key, you may notice that
|
||||
subsequent insertions keep counting up from where you left off. This is
|
||||
because the sequence the table is using hasn't been restarted. Sure, you can
|
||||
restart it manually or you can tell `truncate` to do it for you. By
|
||||
appending `restart identity` to the end of a `truncate` statement, Postgres
|
||||
will make sure to restart any associated sequences at `1`.
|
||||
|
||||
```
|
||||
truncate pokemon, trainers, pokemons_trainers restart identity;
|
||||
```
|
||||
Reference in New Issue
Block a user