pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedtrigger to enforce FK with nulls?
3+ messages / 3 participants
[nested] [flat]
* trigger to enforce FK with nulls?
@ 2006-04-13 01:11 George Young <gry@ll.mit.edu>
2006-04-13 01:22 ` Re: trigger to enforce FK with nulls? Alvaro Herrera <alvherre@commandprompt.com>
2006-04-13 01:26 ` Re: trigger to enforce FK with nulls? Stephan Szabo <sszabo@megazone.bigpanda.com>
0 siblings, 2 replies; 3+ messages in thread
From: George Young @ 2006-04-13 01:11 UTC (permalink / raw)
To: pgsql-sql
[PostgreSQL 8.1.3 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.0.1]
I'm starting to use lots of foreign key constraints to keep my
data clean. In one case, however, I need to allow null values
for the key. E.g.:
create table opset_steps(opset text, step text, step_num int);
create table steps(run text, step text, opset text, user text, step_num int);
The constraint on steps should be:
steps.(opset,step) must be found in opset_steps.(opset,step) UNLESS steps.opset is null.
I started to put dummy rows in opset_steps with opset='NO-OPSET' for each
step with no real opset, but this looks really kludgy.
Since foreign keys per se can't do this, I presume the way
is to use triggers, though I have not used triggers before. Any
suggestions, caveats?
Opset_steps has only 4400 rows, and inserts to steps are not real
frequent, so I don't *think* performace should be a problem. No
updates are ever made to steps.opset and steps.step, or to
opset_steps.(opset,step)
[though updates are often made to *other* fields of steps].
-- George Young
--
"Are the gods not just?" "Oh no, child.
What would become of us if they were?" (CSL)
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: trigger to enforce FK with nulls?
2006-04-13 01:11 trigger to enforce FK with nulls? George Young <gry@ll.mit.edu>
@ 2006-04-13 01:22 ` Alvaro Herrera <alvherre@commandprompt.com>
1 sibling, 0 replies; 3+ messages in thread
From: Alvaro Herrera @ 2006-04-13 01:22 UTC (permalink / raw)
To: George Young <gry@ll.mit.edu>; +Cc: pgsql-sql
George Young wrote:
> Since foreign keys per se can't do this, I presume the way
> is to use triggers, though I have not used triggers before.
Says who? Just don't specify NOT NULL on the referencing column.
--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: trigger to enforce FK with nulls?
2006-04-13 01:11 trigger to enforce FK with nulls? George Young <gry@ll.mit.edu>
@ 2006-04-13 01:26 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
1 sibling, 0 replies; 3+ messages in thread
From: Stephan Szabo @ 2006-04-13 01:26 UTC (permalink / raw)
To: George Young <gry@ll.mit.edu>; +Cc: pgsql-sql
On Wed, 12 Apr 2006, George Young wrote:
> [PostgreSQL 8.1.3 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.0.1]
>
> I'm starting to use lots of foreign key constraints to keep my
> data clean. In one case, however, I need to allow null values
> for the key. E.g.:
>
> create table opset_steps(opset text, step text, step_num int);
> create table steps(run text, step text, opset text, user text, step_num int);
>
> The constraint on steps should be:
> steps.(opset,step) must be found in opset_steps.(opset,step) UNLESS steps.opset is null.
The default foreign key case should not error if either steps.opset or
steps.step is null. If you're seeing something else, can you give a
complete test case?
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2006-04-13 01:26 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-04-13 01:11 trigger to enforce FK with nulls? George Young <gry@ll.mit.edu>
2006-04-13 01:22 ` Alvaro Herrera <alvherre@commandprompt.com>
2006-04-13 01:26 ` Stephan Szabo <sszabo@megazone.bigpanda.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