diff --git a/README.md b/README.md index 966d4a4..da7c31e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/postgres/restarting-sequences-when-truncating-tables.md b/postgres/restarting-sequences-when-truncating-tables.md new file mode 100644 index 0000000..b29f9fe --- /dev/null +++ b/postgres/restarting-sequences-when-truncating-tables.md @@ -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; +```