473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Discussion wanted: 'Trigger on Delete' cascade.

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
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 3071
Display all headersTo: "R.Welz" <li***********@gmx.de>
Subject: Re: [GENERAL] Discussion wanted: 'Trigger on Delete' cascade.
Date: Wed, 28 Jul 2004 13:24:26 +0200
From: Pierre-Frédéric Caillaud <li***@boutiquenumerique.com>
Organization: La Boutique Numérique

From what you say, your do not need a link table.
A link table is useful to link several items of a table to several items
of another table (many-to-
many relationship).

Example : items and categories.

1- An item belongs to a category
No need for a link table.
table categories ( id serial primary key );
table items ( category_id integer references categories( id ) on delete
cascade );

1- An item belongs to several categories
table categories ( id serial primary key );
table items ( id serial primary key );
table links (
category_id integer references categories( id ) on delete cascade,
item_id integer references items( id ) on delete cascade,
);
In this case you do need a link table.

You also need triggers to delete items when
- a link is deleted from the links table
- no more links to this item exist
which is, in fact, reference counting.





---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2
Yes.
A magazine can have several e-mail addresses, and one e-mail address
can belong to several magazines. Well, the e-mail addresses belongs to
the publisher but for simplification the e-mail address is linked with
the magazine.
So what is really more important is that I don't know, is there a big
performance penalty of the sort of "trigger calling another trigger" or
would it be noticeable faster if the first trigger does all the
checking and decides itself wheather to delete table B.

I think of writing a really large database, so performance is most
important, or will there be no noticeable performance hit? It is my
first big database.

Thank you
Robert

Am 28.07.2004 um 13:24 schrieb Pierre-Frédéric Caillaud:


From what you say, your do not need a link table.
A link table is useful to link several items of a table to several
items of another table (many-to-many relationship).

Example : items and categories.

1- An item belongs to a category
No need for a link table.
table categories ( id serial primary key );
table items ( category_id integer references categories( id ) on
delete cascade );

1- An item belongs to several categories
table categories ( id serial primary key );
table items ( id serial primary key );
table links (
category_id integer references categories( id ) on delete cascade,
item_id integer references items( id ) on delete cascade,
);
In this case you do need a link table.

You also need triggers to delete items when
- a link is deleted from the links table
- no more links to this item exist
which is, in fact, reference counting.




---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marie-Christine | last post by:
i want to audit transactions done to table TOrig. I created table TAudit same as TOrig in addition to ActionID (1 for insert, 2 for update, 3 for delete), System Date and System User. I created...
4
by: bmccollum | last post by:
I have written a trigger that's supposed to go out and delete corresponding records from multiple tables once I delete a specific record from a table called tblAdmissions. This does not work and...
12
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
3
by: John Rivers | last post by:
Hello, I think this will apply to alot of web applications: users want the ability to delete a record in table x this record is related to records in other tables and those to others in...
5
by: Peter Erickson | last post by:
I am running postgresql 7.4.2 and having problems creating a trigger function properly. I keep getting the following error: ERROR: OLD used in query that is not in rule I have a table called...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
1
by: Robert Fitzpatrick | last post by:
I am running PostgreSQL 7.4.5 and have a trigger on a table called tblriskassessors which inserts, updates or delete a corresponding record in tblinspectors by lookup of a contact id and license...
2
by: dean.cochrane | last post by:
I have inherited a large application. I have a table which contains a hierarchy, like this CREATE TABLE sample_table( sample_id int NOT NULL parent_sample_id int NOT NULL ....lots of other...
3
by: Wojto | last post by:
Hi there! I need to write a trigger that will check referential integrity of my data. I have few FOREIGN KEY constraints but, as You probably konow, the cannot be deferred (in the meaning of SQL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.