473,769 Members | 5,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*******@postg resql.org

Nov 23 '05 #1
2 3103
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***@boutique numerique.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*******@postg resql.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*******@postg resql.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
1537
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 triggers on TOrigto insert into TAudit in case of insert, update & delete. TOrig contains text column. So i created instead of Trigger like: Create TRIGGER TrigDelete ON dbo.TOrig
4
2780
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 I'm not sure why... Here's the code that's supposed to run, let's say, if a user (via a VB 6.0 interface) decides to delete a record. If the record in the tblAdmissions table has the primary key (AdmissionID) of "123", then the code below is...
12
4763
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 (N.act_recov_date IS NOT NULL) BEGIN ATOMIC select count(*) into v_n from is3.flushes
3
2102
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 other tables etc. in other words we must use cascade delete to do
5
3967
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 journal_entries with a foreign key to a table called journals. When a entry is added to journal_entries, I am trying to get it to update the 'mtime' field of the corresponding entry in the journals table.
8
2636
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 something entirely different? The docs only mention regular triggers being executed alfabetically.
1
4587
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 number match. The INSERT and DELETE work fine. The UPDATE works good unless I update the license number. The error, at the bottom of this message, suggests the primary key violation. But my UPDATE in no way alters the primary key, which is...
2
2395
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 cols...) DELETEs on all tables are handled by the front end code, which just
3
5969
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 92 standard). So I decided to add NOCHECK CONSTRAINT ALL to the modified table and then run a trigger (after secon altertion of my table). But I cannot write a trigger for ALTER. I found something on msdn, byt their example doesn't work. To show...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.