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 1erRfu-00080P-Th for pgsql-general@arkaria.postgresql.org; Thu, 01 Mar 2018 17:07:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erRft-0001ai-JI for pgsql-general@arkaria.postgresql.org; Thu, 01 Mar 2018 17:07:21 +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 1erRft-0001aM-7x for pgsql-general@lists.postgresql.org; Thu, 01 Mar 2018 17:07:21 +0000 Received: from mail.wordtothewise.com ([2001:470:1:6d::9a]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erRfq-0005ro-1l for pgsql-general@lists.postgresql.org; Thu, 01 Mar 2018 17:07:20 +0000 Received: from satsuke.wordtothewise.com (204.11.227.194.static.etheric.net [204.11.227.194]) by mail.wordtothewise.com (Postfix) with ESMTPSA id 586E823379 for ; Thu, 1 Mar 2018 09:08:07 -0800 (PST) From: Steve Atkins Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: Enforce primary key on every table during dev? Date: Thu, 1 Mar 2018 09:07:16 -0800 References: <87vaegq0v4.fsf@gmail.com> <84f6f201-8d70-b5a2-503a-62e0a452cc95@cox.net> <20180301102258.485d09aa@wrkhors.com> To: "pgsql-generallists.postgresql.org" In-Reply-To: Message-Id: X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk (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 = wrote: >=20 >=20 > On Thu, Mar 1, 2018 at 11:43 AM, Alban Hertroys = wrote: >=20 >=20 >> On 1 March 2018 at 17:22, Steven Lembark 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. >>=20 >> 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. >>=20 >> 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. >>=20 >> More generally: For every rule there are exceptions. Even for this = one. >=20 > 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=20= 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=