pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Melvin Davidson <melvin6925@gmail.com>
Cc: Tim Cross <theophilusx@gmail.com>
Cc: Jeremy Finzel <finzelj@gmail.com>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Enforce primary key on every table during dev?
Date: Thu, 1 Mar 2018 09:20:09 +0100
Message-ID: <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com> (raw)
In-Reply-To: <CANu8Fix7pOzQ8Bjpzgiyw8yZgrJ9Zc95g9p=kQFYqAOFCf5jzw@mail.gmail.com>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com>
	<87vaegq0v4.fsf@gmail.com>
	<CANu8Fix7pOzQ8Bjpzgiyw8yZgrJ9Zc95g9p=kQFYqAOFCf5jzw@mail.gmail.com>


> On 1 Mar 2018, at 1:47, Melvin Davidson <melvin6925@gmail.com> wrote:

> > I think you would be better off having an automated report which alerts
> >you to tables lacking a primary key and deal with that policy through
> >other means. 
> 
> Perhaps a better solution is to have a meeting with the developers and explain to them 
> WHY the policy of enforcing a primary key is important. Also, explain the purpose of
> primary keys and why it is not always suitable to just use an integer or serial as the key,
> but rather why natural unique (even multi column) keys are better. But this begs the question, 
> why are "developers" allowed to design database tables? That should be the job of the DBA! At
> the very minimum, the DBA should be reviewing and have the authority to approve of disapprove 
> of table/schema designs/changes .

Not to mention that not all types of tables necessarily have suitable candidates for a primary key. You could add a surrogate key based on a serial type, but in such cases that may not serve any purpose other than to have some arbitrary primary key.

An example of such tables is a monetary transaction table that contains records for deposits and withdrawals to accounts. It will have lots of foreign key references to other tables, but rows containing the same values are probably not duplicates.
Adding a surrogate key to such a table just adds overhead, although that could be useful in case specific rows need updating or deleting without also modifying the other rows with that same data - normally, only insertions and selections happen on such tables though, and updates or deletes are absolutely forbidden - corrections happen by inserting rows with an opposite transaction.

Regards,

Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll find there is no forest.





view thread (59+ messages)  latest in thread

Message-ID: <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com>
Permalink:  ../C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com/
Also on:    postgresql.org/message-id/C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: haramrae@gmail.com, melvin6925@gmail.com, theophilusx@gmail.com, finzelj@gmail.com
  Subject: Re: Enforce primary key on every table during dev?
  In-Reply-To: <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com>
  References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com> <87vaegq0v4.fsf@gmail.com> <CANu8Fix7pOzQ8Bjpzgiyw8yZgrJ9Zc95g9p=kQFYqAOFCf5jzw@mail.gmail.com> <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox