pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: fmiddleton@verizon.net
Cc: pgsql-sql@postgresql.org
Subject: Re: redundancy in CHECK CONSTRAINTs
Date: Sat, 24 Sep 2005 23:58:21 -0400
Message-ID: <26530.1127620701@sss.pgh.pa.us> (raw)
In-Reply-To: <43361AB7.2020701@verizon.net>
References: <43361AB7.2020701@verizon.net>

Ferindo Middleton Jr <fmiddleton@verizon.net> 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



view thread (3+ messages)  latest in thread

Message-ID: <26530.1127620701@sss.pgh.pa.us>
Permalink:  ../26530.1127620701@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/26530.1127620701@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, fmiddleton@verizon.net
  Subject: Re: redundancy in CHECK CONSTRAINTs
  In-Reply-To: <26530.1127620701@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox