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 41B9BD1B269 for ; Wed, 21 Jul 2004 05:36:21 -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 96600-05 for ; Wed, 21 Jul 2004 08:36:02 +0000 (GMT) Received: from rock.spider.ru (rock.spider.ru [62.183.34.226]) by svr1.postgresql.org (Postfix) with ESMTP id B9A13D1B16A for ; Wed, 21 Jul 2004 05:35:58 -0300 (ADT) Received: from rock (localhost [127.0.0.1]) by rock (Postfix) with SMTP id 6B31C27672 for ; Wed, 21 Jul 2004 12:35:59 +0400 (MSD) Received: from sad.localnet.ru (sad.localnet.ru [10.0.0.72]) by rock.spider.ru (Postfix) with ESMTP id DBF2E27645 for ; Wed, 21 Jul 2004 12:35:57 +0400 (MSD) Content-Type: text/plain; charset="iso-2022-jp" From: sad Organization: IT To: Subject: Re: surrogate key or not? Date: Wed, 21 Jul 2004 11:44:00 +0400 User-Agent: KMail/1.4.3 References: <1090325769.3241.27.camel@dicaprio.akademie1.de> <004101c46ec2$631ca930$7201a8c0@mst1x5r347kymb> <200407210001.00102.josh@agliodbs.com> In-Reply-To: <200407210001.00102.josh@agliodbs.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200407211144.01001.sad@bankir.ru> 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: 200407/144 X-Sequence-Number: 18396 Thnx, Josh you are very helpful. > There are, in fact, three very good reasons to use surrogate keys, all of > which are strictly due to limitations of technology; that is, > implementation and performance issues, NOT business logic. They are: > > 1) Convenience: It's very annoying to have to refer to a 4-column foriegn > key whenever you do a join in queries or want to delete a record, as well > as tracking a 4-element composite in your client software. > > 2) Performance: INT and BIGINT data types are among the most compact and > efficient stored in most RDBMSs. So using anything else as a key would > likely result in a loss of performance on large-table joins. > > 3) Mutability: Most RDBMSs are very inefficient about CASCADE deletes and > updates. Some RDBMSs do not support CASCADE, forcing the client software > to fix all the dependant rows. This means that DBAs are very reluctant to > use columns which change frequently as join keys. > Now, you're probably wondering "why does this guy regard surrogate keys as > a problem?" I'll tell you: I absolutely cannot count the number of "bad > databases" I've encountered which contained tables with a surrogate key, > and NO REAL KEY of any kind. This makes data normalization impossible, > and cleanup of the database becomes a labor-intensive process requiring > hand-examination of each row. The surrogate keys is a real big problem. But i want to add another two reasons to have a surrogate keys 4) Replication: to identify an object of ANY type (record of any table=20 regardless to datamodel), to store lists of deleted or modified objects=20 (regardless to datamodel) 5) Making a primary key: if there is no real key at all. the sentence (5) is debatable. in theory every relation has a real key, but= in=20 the practice we have historical datamodels without PK !!! it is impossible= =20 but it exists. For example: Here in Russia we have a lot of different but identically named streets wit= hin=20 one city. They has absoluetly identical attributes. Historically only human= =20 not machines work on that datamodel and they all used to call such streets = by=20 name adding some spechial non-formal explainations, for example: "deliver this message please to the house 35 on the Green street, that is t= o=20 the west of the center of the city."=20 "deliver this message please to the house 12 on the Green street, that is= =20 shortest of the all Green streets in the town." Another exaple is a table of user messages: user has a number of messages they have no mean attribute and also could be= =20 identical, so you force this user to numbering his messages, or number them= =20 yourself.