Skip site navigation (1) Skip section navigation (2)

Discussion wanted: 'Trigger on Delete' cascade.

From: R(dot)Welz <linuxprodukte(at)gmx(dot)de>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Discussion wanted: 'Trigger on Delete' cascade.
Date: 2004-07-28 09:36:47
Message-ID: A55D7110-E079-11D8-B36A-0003936EF152@gmx.de (view raw or flat)
Thread:
Lists: pgsql-general
Hello.
I want to discuss a problem I have with my database design becourse I 
feel I cannot decide wheather I am on the right way of doing things.

First of all, I am writing a literature and magazine database with web 
(PHP) and C++ Interface, serving over the web and in a very fast LAN. 
So my concern is about performance (and aestaetic by doing the things 
as optimal as possible.) This is my first database at all, but I have 
read a lot of books about it and I am a reasonable good C++ programmer.

So here is my problem...

I have table A and B and a linking table. I want to delete the linking 
tables record, this should additionally delete a record in table A. 
This can't be done with foreign key constraint, since the linking 
tables record has the foreign key of table A, not the primary key.

Then table A depends on table B, and here is the same problem, table A 
has the foreign key of table B, not the primary key.

and so on.
Actually I have several complex cases which have to do with automatic 
deletion of tables with some tables involved, for example:
When you delete a magazine, you delete a corresponding e-mail from the 
publisher, but only when this e-mail is not referenced anymore by other 
tables, for example books , data storage disks, cds or the like.

So what I did was the following:

Delete linking table's record, this fires a trigger that deletes a 
record in table A. Deletion in table A fires a trigger which checks if 
table A's record references table B and is is allowed to be deleted. If 
not, I RETURN NULL, if yes, I deletion is allowed to take place.

What do you think? Would it be better to have the trigger in the 
linking table to check if it can safely delete record in A by checking 
table B? Would that be a real performance gain?

Or are there ways of doing things, which I havn't thought about?

Thank you for thoughts on this subject.

Greetings,
Robert Welz


Responses

pgsql-general by date

Next:From: John Sidney-WoollettDate: 2004-07-28 09:49:37
Subject: Re: Data model for Postfix v2
Previous:From: Lincoln YeohDate: 2004-07-28 09:23:16
Subject: Re: Sql injection attacks

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group