pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with triggers
2+ messages / 2 participants
[nested] [flat]
* Help with triggers
@ 2004-02-26 14:22 Michiel Lange <michiel@minas.demon.nl>
2004-02-26 14:41 ` Re: Help with triggers Bruno Wolff III <bruno@wolff.to>
0 siblings, 1 reply; 2+ messages in thread
From: Michiel Lange @ 2004-02-26 14:22 UTC (permalink / raw)
To: pgsql-admin
Hello,
I could use some help in getting to know how triggers are to be made
exactly. Even with the documentation it is hard for me to understand all
of it...
maybe I think about it in a too complex manner, freezing my own brains...
But here it is:
I have a database that is about how ocomputers and phones are connected
in a patch panel.
For that I have three tables:
patches(patch_id(serial, pkey), location(varchar, unique));
computers(computer_id(serial, pkey), name(varchar, unique), patch
(unique,references patches.patch_id), ... and some more);
phones(number(int4, pkey), owner(varchar), patch (unique,references
patches.patch_id), ... and some more);
(the database is not only about this information, there's quite some
more, but this narrows the problem down)
The point is:
I can say that computers.patch is unique
I can also say that phones.patch is unique
but there's also this situation that you cannot connect a computer and a
phone on the same patch_id, so if a patch is used for a computer it is
not possible to use that patch also for the phones. (and vise versa)
As far as I know there is no constraint that enforces this, so I will
have to write a trigger...
I can see a
CREATE TRIGGER unique_patch_trg BEFORE UPDATE OR INSERT ON computers
FOR EACH ROW
but then... it would be easiest if something like
IF EXIST patch IN phones.patch
THEN
ABORT
ENDIF
and about the same for the computers.patch...
anyone who can help me learn how to get this done?
Regards and thanks in advance,
Michiel
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Help with triggers
2004-02-26 14:22 Help with triggers Michiel Lange <michiel@minas.demon.nl>
@ 2004-02-26 14:41 ` Bruno Wolff III <bruno@wolff.to>
0 siblings, 0 replies; 2+ messages in thread
From: Bruno Wolff III @ 2004-02-26 14:41 UTC (permalink / raw)
To: Michiel Lange <michiel@minas.demon.nl>; +Cc: pgsql-admin
On Thu, Feb 26, 2004 at 15:22:37 +0100,
Michiel Lange <michiel@minas.demon.nl> wrote:
> But here it is:
> I have a database that is about how ocomputers and phones are connected
> in a patch panel.
> For that I have three tables:
> patches(patch_id(serial, pkey), location(varchar, unique));
> computers(computer_id(serial, pkey), name(varchar, unique), patch
> (unique,references patches.patch_id), ... and some more);
> phones(number(int4, pkey), owner(varchar), patch (unique,references
> patches.patch_id), ... and some more);
>
> (the database is not only about this information, there's quite some
> more, but this narrows the problem down)
>
> The point is:
> I can say that computers.patch is unique
> I can also say that phones.patch is unique
>
> but there's also this situation that you cannot connect a computer and a
> phone on the same patch_id, so if a patch is used for a computer it is
> not possible to use that patch also for the phones. (and vise versa)
> As far as I know there is no constraint that enforces this, so I will
> have to write a trigger...
You can enforce this without having to write a trigger.
You add a patch type to all three tables constrained in patches to be
one of two types and in the other tables to be the type matching that
table. Then you make the foreign keys into patches to be patch_id
combined with the patch type. This prevents both phones and computers
from having the same patch id.
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2004-02-26 14:41 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-02-26 14:22 Help with triggers Michiel Lange <michiel@minas.demon.nl>
2004-02-26 14:41 ` Bruno Wolff III <bruno@wolff.to>
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