Re: sub-selects in CHECK

From: nconway(at)klamath(dot)dyndns(dot)org (Neil Conway)
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: sub-selects in CHECK
Date: 2002-07-27 23:36:27
Message-ID: 20020727233627.GA1254@klamath.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Jul 27, 2002 at 07:07:13PM -0400, Tom Lane wrote:
> nconway(at)klamath(dot)dyndns(dot)org (Neil Conway) writes:
> > I'd like to add the ability to use a sub-select in a CHECK constraint.
> > Can someone elaborate on what changes would be needed to support
> > this?
>
> Define what you think should happen when the other rows referenced
> by the subselect change.

Good point -- but given that SQL99 specifically mentions that this
functionality should be available (Feature 671, "Subqueries in
CHECK constraints"), there must be some reasonable behavior
adopted by another DBMS...

In any case, there are already plenty of ways to create non-sensical
constraints. For example:

CHECK ( foo < random() )

or even:

CREATE FUNCTION check_func() returns int as 'select ...' language 'sql';

ALTER TABLE foo ADD CONSTRAINT check_x CHECK (x > check_func() );

(which is effectively a sub-select with a different syntax)

So the restrictions "no sub-selects or aggregates in a CHECK constraint"
is quite insufficient, if we actually want to prevent an application
developer from creating dubious constraints.

Cheers,

Neil

--
Neil Conway <neilconway(at)rogers(dot)com>
PGP Key ID: DB3C29FC

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2002-07-28 00:48:24 Re: sub-selects in CHECK
Previous Message Tom Lane 2002-07-27 23:07:13 Re: sub-selects in CHECK