Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f77F1q670352 for ; Tue, 7 Aug 2001 11:01:52 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 97958; Tue, 07 Aug 2001 08:01:45 -0700 From: "Josh Berkus" Subject: Re: Are circular REFERENCES possible ? To: Denis Bucher Cc: pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Tue, 07 Aug 2001 08:01:45 -0700 Message-ID: In-Reply-To: <5.1.0.14.0.20010807114609.00a18490@mail.niftycom.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="_===97958====davinci.ethosmedia.com===_" X-Archive-Number: 200108/96 X-Sequence-Number: 4317 This is a multi-part MIME message --_===97958====davinci.ethosmedia.com===_ Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit Denis, > I have a case where I wanted to do circular REFERENCES, is this > impossible ? It can be done. It's just a bad idea. > We deliver to the *shops* of our *customers*. > We have therefore two tables : > - customers (enterprise, financial information, and so on...) > - shop (with a name, street, phone number, name of manager) > > Now, each shop REFERENCES a customer so that we know > to which customer belongs a shop. > > AND, each customer has a DEFAULT shop for deliveries, i.e. most > customers only have one shop, or a main shop and many small ones. > Therefore a customer should REFERENCES the 'main' or 'default' shop. You can do this by applying the constraints *after* table creation. However, you will forever fight the following problems: 1. You will not be able to add any records to Customers without dropping and re-creating the REFERENCES each time. 2. You will never be able to delete a record from either table due to the circular reference check. 3. Some UPDATES will also fail for the same reason. All of this makes circular references a bad idea; references are meant to be heirarchical. Heck, I got into a real mess when I accidentally set up a circular reference among 5 tables ... took me forever to figure out why INSERTS kept failing. So, an alternate solution to your database structure: 1. Each Customer has one to many Shops (Shops.CustomerID REFERENCES Customers(ID)). 2. Each Shop has a Boolean characteristic Default. 3. Of a Customer's shops, only one can have Default=TRUE at any one time. You use triggers or functions to enforce rule 3. This system works quite well for this purpose ... I was able to put it to use for a much more complex CRM system with main and secondary HR and billing addresses. Your third alternative is to create a JOIN table called Default Shops. However, this does not really provide you any additional referential integrity -- it jsut may suit you if you find triggers intimidating. -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco --_===97958====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===97958====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===97958====davinci.ethosmedia.com===_ Content-Type: text/plain Content-Disposition: attachment; filename="" Content-Transfer-Encoding: base64 DQo= --_===97958====davinci.ethosmedia.com===_--