pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedSubselects in CHECK clause ...
3+ messages / 2 participants
[nested] [flat]
* Subselects in CHECK clause ...
@ 2006-11-29 17:08 James Robinson <jlrobins@socialserve.com>
0 siblings, 1 reply; 3+ messages in thread
From: James Robinson @ 2006-11-29 17:08 UTC (permalink / raw)
To: pgsql-sql
I see that subselects are not directly supported in check clauses,
but one can work around that by writing a stored function which
returns boolean and performs the subselect. Are there any known
gotchas with doing this?
The CREATE TABLE docs regarding CHECK clauses states:
"Currently, CHECK expressions cannot contain subqueries nor refer to
variables other than columns of the current row."
Is this due to someone not yet writing the necessary code to remove
the subquery clause, or is there a more fundamental reason?
----
James Robinson
Socialserve.com
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Subselects in CHECK clause ...
@ 2006-11-29 17:53 Stephan Szabo <sszabo@megazone.bigpanda.com>
parent: James Robinson <jlrobins@socialserve.com>
0 siblings, 1 reply; 3+ messages in thread
From: Stephan Szabo @ 2006-11-29 17:53 UTC (permalink / raw)
To: James Robinson <jlrobins@socialserve.com>; +Cc: pgsql-sql
On Wed, 29 Nov 2006, James Robinson wrote:
> I see that subselects are not directly supported in check clauses,
> but one can work around that by writing a stored function which
> returns boolean and performs the subselect. Are there any known
> gotchas with doing this?
To completely get the constraint, you have to also apply constraints on
the tables referenced in the function that prevent modifications on those
tables from causing the constraint to be violated. For example, if you
were to do an exists test on another table for a row that matches up with
this row in some fashion (for a specialized referential integrity
constraint) modifications on that other table could also cause the
constraint to be violated, but that isn't caught by the CHECK
function(...) case and you'll probably need triggers or other constraints
on that table.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Subselects in CHECK clause ...
@ 2006-11-29 17:59 James Robinson <jlrobins@socialserve.com>
parent: Stephan Szabo <sszabo@megazone.bigpanda.com>
0 siblings, 0 replies; 3+ messages in thread
From: James Robinson @ 2006-11-29 17:59 UTC (permalink / raw)
To: Stephan Szabo <sszabo@megazone.bigpanda.com>; +Cc: pgsql-sql
Gotcha. This is indeed to ensure a specialized ref integrity bit,
against a column which "ought not ever change". Once some more old
code goes away, then can fully normalize this area, making this check
subselect bit go away, replaced by a regular FK.
Hmm -- probably could even now make it a FK against a column which is
not the pri key of the foreign table, and it'd work better and would
ensure consistence against changes on the foreign table side, eh?
Thanks!
On Nov 29, 2006, at 12:53 PM, Stephan Szabo wrote:
> To completely get the constraint, you have to also apply
> constraints on
> the tables referenced in the function that prevent modifications on
> those
> tables from causing the constraint to be violated. For example, if you
> were to do an exists test on another table for a row that matches
> up with
> this row in some fashion (for a specialized referential integrity
> constraint) modifications on that other table could also cause the
> constraint to be violated, but that isn't caught by the CHECK
> function(...) case and you'll probably need triggers or other
> constraints
> on that table.
----
James Robinson
Socialserve.com
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-11-29 17:59 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-11-29 17:08 Subselects in CHECK clause ... James Robinson <jlrobins@socialserve.com>
2006-11-29 17:53 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2006-11-29 17:59 ` James Robinson <jlrobins@socialserve.com>
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