X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C3E4E5E46BF for ; Fri, 6 Aug 2004 19:41:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 41662-04 for ; Fri, 6 Aug 2004 22:41:44 +0000 (GMT) Received: from davinci.ethosmedia.com (server226.ethosmedia.com [209.128.84.226]) by svr1.postgresql.org (Postfix) with ESMTP id D902E5E3AC5 for ; Fri, 6 Aug 2004 19:41:39 -0300 (ADT) Received: from [64.81.245.111] (HELO temoku.sf.agliodbs.com) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.1.8) with ESMTP id 6011771; Fri, 06 Aug 2004 15:42:58 -0700 From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: David Garamond , pgsql-sql@postgresql.org Subject: Re: surrogate key or not? Date: Fri, 6 Aug 2004 15:42:04 -0700 User-Agent: KMail/1.5.4 References: <1090325769.3241.27.camel@dicaprio.akademie1.de> <200407210001.00102.josh@agliodbs.com> <41026D55.3040002@zara.6.isreserved.com> In-Reply-To: <41026D55.3040002@zara.6.isreserved.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200408061542.04336.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/44 X-Sequence-Number: 18541 David, > But, once a surrogate key is assigned to a row, doesn't it become a > "real" data? For example, I have a bunch of invoices/receipts and I > write down a unique number on each of them. Doesn't the unique number > become part of the information contained by the invoice/receipt itself > (at least as long as I'm concerned)? Sure ... *if* it's being used that way. If, however, your table has that Invoice # *and* a seperate surrogate key that's redundant and can cause problems. > Change management IMO is perhaps the main reason of surrogate/artificial > key. We often need a PK that _never_ needs to change (because it can be > a royal PITA or downright impossibility to make this change; the PK > might already be printed on a form/card/document, recorded on some > permanent database, tattoed/embedded in someone's forehead, etc). Sure. But surrogate keys don't fix this problem; only good change management does. This is precisely why I say "use with caution"; all too often project leaders regard surrogate keys as a substitute for good change management and don't do any further work. > Meanwhile, every other aspect of the data can change (e.g. a person can > change his name, sex, age, email, address, even date & place of birth). > Not to mention data entry mistakes. So it's impossible to use any > "real"/natural key in this case. Absolutely false. It's quite possible, it's just a performance/schema/data management issue. This also applies to my comment above. > Okay, so surrogate key makes it easy for stupid people to design a > database that is prone to data duplication (because he doesn't install > enough unique constraints to prevent this). But I don't see how a > relation with a surrogate key is harder to "normalize" (that is, for the > duplicates to be removed) than a relation with no key at all. Compare: You're right here, both are equally hard to normalize. What I'm criticizing is the tendency of a lot of beginning DBAs -- and even some books on database design -- to say: "If you've created an integer key, you're done." Had I my way, I would automatically issue a WARNING on any time you create a table in PG without a key. -- -Josh Berkus Aglio Database Solutions San Francisco