From: marcelo <marcelo.nicolet@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Enforce primary key on every table during dev?
Date: Fri, 2 Mar 2018 11:47:39 -0300
Message-ID: <b4cfb733-c490-cd2e-75e3-a23b58af0e64@gmail.com> (raw)
In-Reply-To: <CAAZnbVob_f3vXucHhZ38wpV=vqsaEsicseDyUWy9rpW5V7i5qg@mail.gmail.com>
References: <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com>
<161e1761560-58a6-18e4b@webjas-vad211.srv.aolmail.net>
<CAAZnbVowox3FUrjazK7xvwEYqXdf4e58mUfgaYEpPRpgk_Lxeg@mail.gmail.com>
<2580784f-3b8c-d044-3809-b4ca28993175@archidevsys.co.nz>
<CAAZnbVob_f3vXucHhZ38wpV=vqsaEsicseDyUWy9rpW5V7i5qg@mail.gmail.com>
On 02/03/2018 01:10 , Daevor The Devoted wrote:
>
>
> On Fri, Mar 2, 2018 at 12:05 AM, Gavin Flower
> <GavinFlower@archidevsys.co.nz <mailto:GavinFlower@archidevsys.co.nz>>
> wrote:
>
> On 02/03/18 06:47, Daevor The Devoted wrote:
>
>
> On Thu, Mar 1, 2018 at 2:07 PM, Rakesh Kumar
> <rakeshkumar464@aol.com <mailto:rakeshkumar464@aol.com>
> <mailto:rakeshkumar464@aol.com
> <mailto:rakeshkumar464@aol.com>>> wrote:
>
>
> >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.
>
> I routinely add surrogate keys like serial col to a table
> already
> having a nice candidate keys
> to make it easy to join tables. SQL starts looking
> ungainly when
> you have a 3 col primary
> key and need to join it with child tables.
>
>
> I was always of the opinion that a mandatory surrogate key (as
> you describe) is good practice.
> Sure there may be a unique key according to business logic
> (which may be consist of those "ungainly" multiple columns),
> but guess what, business logic changes, and then you're
> screwed! So using a primary key whose sole purpose is to be a
> primary key makes perfect sense to me.
>
>
> I once worked in a data base that had primary keys of at least 4
> columns, all character fields, Primary Key could easily exceed 45
> characters. Parent child structure was at least 4 deep.
>
> A child table only needs to know its parent, so there is no
> logical need to include its parent and higher tables primary keys,
> and then have to add a field to make the composite primary key
> unique! So if every table has int (or long) primary keys, then a
> child only need a single field to reference its parent.
>
> Some apparently safe Natural Keys might change unexpectedly. A
> few years aback there was a long thread on Natural versus
> Surrogate keys - plenty of examples were using Natural Keys can
> give grief when they had to be changed! I think it best to
> isolate a database from external changes as much as is practicable.
>
> Surrogate keys also simply coding, be it in SQL or Java, or
> whatever language is flavour of the month. Also it makes setting
> up testdata and debugging easier.
>
> I almost invariably define a Surrogate key when I design tables.
>
>
> Cheers,
> Gavin
>
>
> Thank you! I think you have expressed far more clearly what I have
> been trying to say. +10 to you.
Me too. Another +10.
---
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus
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: marcelo.nicolet@gmail.com
Subject: Re: Enforce primary key on every table during dev?
In-Reply-To: <b4cfb733-c490-cd2e-75e3-a23b58af0e64@gmail.com>
References: <C9EFC294-83B5-41FD-9542-60FDBB2588C6@gmail.com> <161e1761560-58a6-18e4b@webjas-vad211.srv.aolmail.net> <CAAZnbVowox3FUrjazK7xvwEYqXdf4e58mUfgaYEpPRpgk_Lxeg@mail.gmail.com> <2580784f-3b8c-d044-3809-b4ca28993175@archidevsys.co.nz> <CAAZnbVob_f3vXucHhZ38wpV=vqsaEsicseDyUWy9rpW5V7i5qg@mail.gmail.com> <b4cfb733-c490-cd2e-75e3-a23b58af0e64@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