X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id DB6E7D1C95C for ; Wed, 23 Jul 2003 14:44:46 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 34456-01 for ; Wed, 23 Jul 2003 11:44:04 -0300 (ADT) Received: from mta06-svc.ntlworld.com (mta06-svc.ntlworld.com [62.253.162.46]) by svr1.postgresql.org (Postfix) with ESMTP id A25ECD1C944 for ; Wed, 23 Jul 2003 11:44:44 -0300 (ADT) Received: from geog.leeds.ac.uk ([80.1.105.8]) by mta06-svc.ntlworld.com (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP id <20030723144443.LPDT16215.mta06-svc.ntlworld.com@geog.leeds.ac.uk> for ; Wed, 23 Jul 2003 15:44:43 +0100 Message-ID: <3F1E9F6E.2090600@geog.leeds.ac.uk> Date: Wed, 23 Jul 2003 15:45:02 +0100 From: Oliver Duke-Williams User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Can a table have a reference to itself? Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.4 tagged_above=0.0 required=5.0 tests=RCVD_IN_UNCONFIRMED_DSBL, USER_AGENT_MOZILLA_UA, X_ACCEPT_LANG X-Spam-Level: X-Archive-Number: 200307/404 X-Sequence-Number: 14518 Hi, I'd like to have a table in which one column has an integrity reference to another column within the same table, and for updates to the primary column to be cascaded. The former aspect seems to work OK, but the latter does not. For example: > create table foo (a int primary key, b int constraint chk_a references foo(a) match full on update cascade); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo' NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY check(s) CREATE Inserting values into foo works as expected (and hoped); setting b to be equal to a or to an existing value of a is fine: > insert into foo values (1,1); INSERT 141920621 1 > insert into foo values (2,2); INSERT 141920622 1 > insert into foo values (3,2); INSERT 141920623 1 but setting b to be a value not yet present in a fails: > insert into foo values (4,5); ERROR: chk_a referential integrity violation - key referenced from foo not found in foo So far so good, but what I'd like to do is to be able to change a value of a, and have this cascaded to b; however this gives an integrity violation error: > update foo set a = 5 where a = 2; ERROR: chk_a referential integrity violation - key referenced from foo not found in foo Is what I'm trying to do possible? In practice, the table I wish to create is a list of data sets; a is the id of each data set, and b indicates whether or not a data set is 'original' (in which case b should equal a) or derived from an existing data set (in which case b should equal the id of the data set from from which it was derived i.e. an existing value of a from elsewhere in the table). (I'm currently using postgres 7.2.1 on Solaris 5.7) Cheers, Oliver -- Oliver Duke-Williams School of Geography, University of Leeds