Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erlyR-0006JT-Ji for pgsql-general@arkaria.postgresql.org; Fri, 02 Mar 2018 14:47:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erlyQ-0007We-0z for pgsql-general@arkaria.postgresql.org; Fri, 02 Mar 2018 14:47:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erlyP-0007OX-Dj for pgsql-general@lists.postgresql.org; Fri, 02 Mar 2018 14:47:49 +0000 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1erlyK-0003Bc-EO for pgsql-general@lists.postgresql.org; Fri, 02 Mar 2018 14:47:47 +0000 Received: by mail-qt0-x242.google.com with SMTP id n12so12120984qtl.5 for ; Fri, 02 Mar 2018 06:47:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=JVssIpYCs2kc3SRcXPzkVBztBxSMhG43chsfGGBfGGc=; b=Ll23CsPSHcjfnfZUaz1DaMvuO5PowBW5xVVLkczDsxoIshH8NZM0PxQGFkByVUHPQ5 XmIT5K6Kal2E1BSNkp398MywDU94BkU5LyaN+rFLLr/nd05qNd4V1SZXW49/H15/SnAo G3SsfPUw5oMGqEJcg+fagEWKiu1qE78CeBtPgro3Pd6q/K3qvN/wbfr26ZgY7lGp5+f2 2dTOjPh42mEomgEO7DFUXfq+Lx9EwzoxhzY9saBghe+cYX1YgCzZLuAUTT3cTa+XM4/1 TpvW/tkOhaJLNhNj13SWFquVDMuM9VX6DYqv+wehHZnshsI6veNYOKtinANapgWvHJX7 fYSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=JVssIpYCs2kc3SRcXPzkVBztBxSMhG43chsfGGBfGGc=; b=pvbZ8j9/fSbhfKPtAGPNopjZqwfqfKpsJmufehpU3MLrxKYa3fH3fhemb/+13BPWr/ OREGhega6Uk8hGtG4lClSJiwf0IMk4yxmr/KyCwF5em//lUPe00EWvDGbnUzx3TKNyQo BJ/K+ULu4ZQqTXg0jB195faWJrCW3nmR2TvOAaIXT4CqqAxArlA9isbS6dPm53/C4sPZ U5SrLXKCYYf/vKs18vdLjIpcjMaEbVe2WlPwsxL20qhSc2Z+2ncgGbkzztz/Lq+zCU8C c3c0OmWJFZMGV/4cuZiB3Jl8roA0vB5FQFd04o02eG79byFIhqHR/N4HJ9BQ8nPuSUhf B4FA== X-Gm-Message-State: AElRT7HPC4WEzuDO/VMZ07q91j+ED1xSw1FX5f65r4nYWYYxgCx7Z7L0 uv7wWekrXDYspNoluhKJFgG23D0a X-Google-Smtp-Source: AG47ELstiN/MgyD3x/MEsQku6Uhp41Pl6IY5Jx1rGc8DPixlp5jQOMCqeh4lmtOuWxa/4T3QXQmS9w== X-Received: by 10.237.36.55 with SMTP id r52mr8804805qtc.268.1520002062980; Fri, 02 Mar 2018 06:47:42 -0800 (PST) Received: from [192.168.1.250] (host63.190-229-77.telecom.net.ar. [190.229.77.63]) by smtp.gmail.com with ESMTPSA id c42sm4729667qtc.42.2018.03.02.06.47.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Mar 2018 06:47:42 -0800 (PST) Subject: Re: Enforce primary key on every table during dev? To: pgsql-general@lists.postgresql.org References: <161e1761560-58a6-18e4b@webjas-vad211.srv.aolmail.net> <2580784f-3b8c-d044-3809-b4ca28993175@archidevsys.co.nz> From: marcelo Message-ID: Date: Fri, 2 Mar 2018 11:47:39 -0300 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------989B841C8C94C9A5FC8D97CC" Content-Language: en-US X-Antivirus: Avast (VPS 180302-0, 02/03/2018), Outbound message X-Antivirus-Status: Clean List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------989B841C8C94C9A5FC8D97CC Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 02/03/2018 01:10 , Daevor The Devoted wrote: > > > On Fri, Mar 2, 2018 at 12:05 AM, Gavin Flower > > > wrote: > > On 02/03/18 06:47, Daevor The Devoted wrote: > > > On Thu, Mar 1, 2018 at 2:07 PM, Rakesh Kumar > > >> 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 --------------989B841C8C94C9A5FC8D97CC Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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> 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>> 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.



Avast logo

El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
www.avast.com


--------------989B841C8C94C9A5FC8D97CC--