diff --git a/README.md b/README.md index 902dc07..26a54fe 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_375 TILs and counting..._ +_376 TILs and counting..._ --- @@ -195,6 +195,7 @@ _375 TILs and counting..._ - [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md) - [pg Prefix Is Reserved For System Schemas](postgres/pg-prefix-is-reserved-for-system-schemas.md) - [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md) +- [Renaming A Sequence](postgres/renaming-a-sequence.md) - [Renaming A Table](postgres/renaming-a-table.md) - [Restart A Sequence](postgres/restart-a-sequence.md) - [Restarting Sequences When Truncating Tables](postgres/restarting-sequences-when-truncating-tables.md) diff --git a/postgres/renaming-a-sequence.md b/postgres/renaming-a-sequence.md new file mode 100644 index 0000000..9749a84 --- /dev/null +++ b/postgres/renaming-a-sequence.md @@ -0,0 +1,26 @@ +# Renaming A Sequence + +If a table is created with a `serial` type column, then a sequence is also +created with a name based on the name of the table. + +```sql +> \d + List of relations + Schema | Name | Type | Owner +--------+-----------------+----------+------------ + public | accounts | table | jbranchaud + public | accounts_id_seq | sequence | jbranchaud +``` + +In [Renaming A Table](renaming-a-table.md), I showed how a table can be +renamed in PostgreSQL. This will not, however, rename associated sequences. +To maintain naming consistency, you may want to also rename sequences when +renaming tables. This can be done with a query like the following: + +```sql +> alter sequence accounts_id_seq rename to users_id_seq; +``` + +See the [`alter +sequence`](http://www.postgresql.org/docs/current/static/sql-altersequence.html) +docs for more details.