X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 91112D9513 for ; Sun, 25 Sep 2005 00:58:29 -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 69922-01 for ; Sun, 25 Sep 2005 03:58:22 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id A1809D9529 for ; Sun, 25 Sep 2005 00:58:21 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j8P3wLjX026531; Sat, 24 Sep 2005 23:58:21 -0400 (EDT) To: fmiddleton@verizon.net Cc: pgsql-sql@postgresql.org Subject: Re: redundancy in CHECK CONSTRAINTs In-reply-to: <43361AB7.2020701@verizon.net> References: <43361AB7.2020701@verizon.net> Comments: In-reply-to Ferindo Middleton Jr message dated "Sat, 24 Sep 2005 23:34:15 -0400" Date: Sat, 24 Sep 2005 23:58:21 -0400 Message-ID: <26530.1127620701@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 required=5 tests=[AWL=0.006] X-Spam-Level: X-Archive-Number: 200509/246 X-Sequence-Number: 22793 Ferindo Middleton Jr writes: > I have the following table: > CREATE TABLE gyuktnine ( > id SERIAL, > intsystem INTEGER NOT NULL REFERENCES yuksystems(id) CONSTRAINT > int_cannot_equal_ext > CHECK (intsystem != extsystem), > extsystem INTEGER NOT NULL REFERENCES yuksystems(id) CONSTRAINT > ext_cannot_equal_int > CHECK (extsystem != intsystem), > PRIMARY KEY (intsystem, extsystem) > ); > Is this redundant? Yes. I think it's poor style too: a constraint referencing multiple columns should be written as a table constraint not a column constraint. That is, you ought to write CREATE TABLE gyuktnine ( id SERIAL, intsystem INTEGER NOT NULL REFERENCES yuksystems(id), extsystem INTEGER NOT NULL REFERENCES yuksystems(id), PRIMARY KEY (intsystem, extsystem), CONSTRAINT int_cannot_equal_ext CHECK (intsystem != extsystem) ); At least in the earlier versions of the SQL standard, it was actually illegal for a column constraint to reference any other columns. I'm not sure if that's still true in the latest spec. Postgres treats column constraints and table constraints alike, but other SQL databases are likely to be pickier. BTW, is there any actual need for the "id" column here, seeing that you have a natural primary key? regards, tom lane