From: Steve Atkins <steve@blighty.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Enforce primary key on every table during dev?
Date: Thu, 1 Mar 2018 09:07:16 -0800
Message-ID: <D7AC4CF3-5E7B-43AB-B7ED-5FA1915A7DE3@blighty.com> (raw)
In-Reply-To: <CANu8FiyhFnriWLR=JiDcevf-A4ATPt6W4jpwfJY=gx2htXHmTQ@mail.gmail.com>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com>
<87vaegq0v4.fsf@gmail.com>
<CANu8Fix7pOzQ8Bjpzgiyw8yZgrJ9Zc95g9p=kQFYqAOFCf5jzw@mail.gmail.com>
<84f6f201-8d70-b5a2-503a-62e0a452cc95@cox.net>
<20180301102258.485d09aa@wrkhors.com>
<CAF-3MvO121hZbqsf9+dwgQgH+wv0YAvwPXXLR_5NxZnrKSifpw@mail.gmail.com>
<CANu8FiyhFnriWLR=JiDcevf-A4ATPt6W4jpwfJY=gx2htXHmTQ@mail.gmail.com>
(Melvin's mail doesn't quote properly; I've tried to fix it but may have broken something)
> On Mar 1, 2018, at 8:50 AM, Melvin Davidson <melvin6925@gmail.com> wrote:
>
>
> On Thu, Mar 1, 2018 at 11:43 AM, Alban Hertroys <haramrae@gmail.com> wrote:
>
>
>> On 1 March 2018 at 17:22, Steven Lembark <lembark@wrkhors.com> wrote:
>>> If you have a design with un-identified data it means that you havn't
>>> normalized it properly: something is missing from the table with
>>> un-identifiable rows.
>>
>> While that holds true for a relational model, in reporting for
>> example, it is common practice to denormalize data without a
>> requirement to be able to identify a single record. The use case for
>> such tables is providing quick aggregates on the data. Often this
>> deals with derived data. It's not that uncommon to not have a primary
>> or even a uniquely identifiable key on such tables.
>>
>> I do not disagree that having a primary key on a table is a bad thing,
>> but I do disagree that a primary key is a requirement for all tables.
>>
>> More generally: For every rule there are exceptions. Even for this one.
>
> You may perceive that to be "common practice", but in reality it is not, and in fact a bad one. As was previously stated, PosgreSQL is a _relational_ database,
> and breaking that premise will eventually land you in very big trouble. There is no solid reason not to a primary key for every table.
Sure there is. It's an additional index and significant additional insert / update overhead.
If you're never going to retrieve single rows, nor join in such a way that uniqueness
on this side is required there's no need for a unique identifier.
It's a rare case that you won't want a primary key, and I'll often add
a surrogate one for convenience even when it's not actually needed,
but there are cases where it's appropriate not to have one, even in
OLTP work. Log tables, for example.
"Every table should have a primary key, whether natural or surrogate"
is a great guideline, and everyone should follow it until they understand
when they shouldn't.
More generally: For every rule there are exceptions. Even for this one.
Cheers,
Steve
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: steve@blighty.com
Subject: Re: Enforce primary key on every table during dev?
In-Reply-To: <D7AC4CF3-5E7B-43AB-B7ED-5FA1915A7DE3@blighty.com>
References: <CAMa1XUie6LD140KdMY7cst41uCaa5B1YEgJ3GH3XSgDh2WXguQ@mail.gmail.com> <87vaegq0v4.fsf@gmail.com> <CANu8Fix7pOzQ8Bjpzgiyw8yZgrJ9Zc95g9p=kQFYqAOFCf5jzw@mail.gmail.com> <84f6f201-8d70-b5a2-503a-62e0a452cc95@cox.net> <20180301102258.485d09aa@wrkhors.com> <CAF-3MvO121hZbqsf9+dwgQgH+wv0YAvwPXXLR_5NxZnrKSifpw@mail.gmail.com> <CANu8FiyhFnriWLR=JiDcevf-A4ATPt6W4jpwfJY=gx2htXHmTQ@mail.gmail.com> <D7AC4CF3-5E7B-43AB-B7ED-5FA1915A7DE3@blighty.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