Skip to content

Commit

Permalink
Add the list of naming conventions to PostgreSQL README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Feb 24, 2016
1 parent 0473c79 commit b626368
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
38 changes: 38 additions & 0 deletions postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,46 @@
* [PostgreSQL](http://www.postgresql.org/) 9.1 and higher, or
* [Amazon RDS for PostgreSQL](https://aws.amazon.com/rds/postgresql/)

### Naming Conventions

* Singular snake_case names for tables. E.g. `user_login`
* Singular snake_case names for columns. E.g. `phone_number`
* Always try to use names that make sense and are descriptive of their purpose
* Single column primary key fields should be named `id`
* Use the following pattern for constraints and indexes: `{tablename}_{columnname(s)}_{suffix}` where the suffix is
one of the following:
* `pkey` for a Primary Key constraint
* `key` for a Unique constraint
* `excl` for an Exclusion constraint
* `idx` for any other kind of index
* `fkey` for a Foreign key
* `check` for a Check constraint
* `seq` for all sequences

### References

<table width="100%">
<tr>
<td width="185">
<a href="http://amzn.to/21qCtqH">
<img src="http://ecx.images-amazon.com/images/I/51xWZ8b11ML._SX150.jpg" width="150" height="185" alt="PostgreSQL 9 Administration Cookbook" />
</a>
</td>
<td>
<p>
<strong><a href="http://amzn.to/21qCtqH">PostgreSQL 9 Administration Cookbook</a>, 2nd Edition</strong><br />
by Simon Riggs, Gianni Ciolli, Hannu Krosing; Packt Publishing (May 2015)
</p>
<p>
A practical guide with over 150 recipes, this book will help you to get you back up and running with an
exploration of the ins and outs of your database, its configuration, server control, tables, and data.
This is a practical guide aimed at giving sysadmins and database administrators the necessary toolkit to be
able to set up, run, and extend powerful databases with PostgreSQL.
</p>
</td>
</tr>
</table>

* [PostgreSQL Documentation](http://www.postgresql.org/docs/current/interactive/index.html) on PostgreSQL.org
* [User Profiles Data Model](http://www.databaseanswers.org/data_models/user_profiles/index.htm) on DatabaseAnswers.org
* [How I Write SQL, Part 1: Naming Conventions](https://launchbylunch.com/posts/2014/Feb/16/sql-naming-conventions/) by Sehrope Sarkuni, JackDB
Expand Down
4 changes: 2 additions & 2 deletions postgres/user_account.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ CREATE TABLE user_account (
lockout_enabled boolean NOT NULL DEFAULT false,
access_failed_count smallint NOT NULL DEFAULT 0,
-- Keys
CONSTRAINT user_account_pk_id PRIMARY KEY (id),
CONSTRAINT user_account_uk_email UNIQUE (email)
CONSTRAINT user_account_pkey PRIMARY KEY (id),
CONSTRAINT user_account_email_key UNIQUE (email)
);
4 changes: 2 additions & 2 deletions postgres/user_claim.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ CREATE TABLE user_claim (
type character varying(256),
value character varying(4000),
-- Keys
CONSTRAINT user_claim_pk_id PRIMARY KEY (id),
CONSTRAINT user_claim_user_account_fk_user_id FOREIGN KEY (user_id)
CONSTRAINT user_claim_pkey PRIMARY KEY (id),
CONSTRAINT user_claim_user_account_fkey FOREIGN KEY (user_id)
REFERENCES user_account (id) MATCH SIMPLE
ON DELETE CASCADE ON UPDATE CASCADE
);
4 changes: 2 additions & 2 deletions postgres/user_login.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ CREATE TABLE user_login (
key character varying(100),
user_id uuid NOT NULL,
-- Keys
CONSTRAINT user_login_pk_name_key PRIMARY KEY (name, key),
CONSTRAINT user_login_user_account_fk_user_id FOREIGN KEY (user_id)
CONSTRAINT user_login_pkey PRIMARY KEY (name, key),
CONSTRAINT user_login_user_account_fkey FOREIGN KEY (user_id)
REFERENCES user_account (id) MATCH SIMPLE
ON DELETE CASCADE ON UPDATE CASCADE
);
4 changes: 2 additions & 2 deletions postgres/user_profile.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ CREATE TABLE user_profile (
created_at timestamp without time zone DEFAULT timezone('utc'::text, now()),
updated_at timestamp without time zone DEFAULT timezone('utc'::text, now()),
-- Keys
CONSTRAINT user_profile_pk_user_id PRIMARY KEY (user_id),
CONSTRAINT user_profile_user_account_fk_user_id FOREIGN KEY (user_id)
CONSTRAINT user_profile_pkey PRIMARY KEY (user_id),
CONSTRAINT user_profile_user_account_fkey FOREIGN KEY (user_id)
REFERENCES user_account (id) MATCH SIMPLE
ON DELETE CASCADE ON UPDATE CASCADE
);

0 comments on commit b626368

Please sign in to comment.