473,672 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trigger to delete record from one table with check on another table.

Hi. I want to create a trigger that only allows delete from table A if
corresponding record in table B does not exist. Any idea on how this
can be done? Thanks, Kenneth.
Jul 19 '05 #1
6 7643
Is there a column in table B that references the primary key of table A?

If so, won't the constraint prevent the deletes you want to prevent?
Jul 19 '05 #2
Don't you wanna use referential integrity constraints to achieve this?

ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ***@posting.goo gle.com>...
Hi. I want to create a trigger that only allows delete from table A if
corresponding record in table B does not exist. Any idea on how this
can be done? Thanks, Kenneth.

Jul 19 '05 #3
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ***@posting.goo gle.com>...
Hi. I want to create a trigger that only allows delete from table A if
corresponding record in table B does not exist. Any idea on how this
can be done? Thanks, Kenneth.


Sounds more like you want a reference constraint.
Do you have Primary keys and Foreign Keys set up on the corredsponding
tables? Looks like from the description table A is the parent table
(wiht a primary key) and table B is the child table (with a foreign
key that references the primary key of table A)

HTH,
ed
Jul 19 '05 #4
ed********@magi cinterface.com (Ed prochak) wrote in message news:<4b******* *************** ****@posting.go ogle.com>...
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ***@posting.goo gle.com>...
Hi. I want to create a trigger that only allows delete from table A if
corresponding record in table B does not exist. Any idea on how this
can be done? Thanks, Kenneth.


Sounds more like you want a reference constraint.
Do you have Primary keys and Foreign Keys set up on the corredsponding
tables? Looks like from the description table A is the parent table
(wiht a primary key) and table B is the child table (with a foreign
key that references the primary key of table A)

HTH,
ed


Hi, and thanks for your reply.

The thing is that it is not possible to use constraints based on PK's
and FK's in this database (the relations are placed on the applicaton
layer - hence it doesn't exist any PK's or FK's).

So: I figure trigger is the only solution to ensure the above
scenario. Any ideas on how the trigger would look?

Thanks,
Kenneth
Jul 19 '05 #5
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ****@posting.go ogle.com>...
ed********@magi cinterface.com (Ed prochak) wrote in message news:<4b******* *************** ****@posting.go ogle.com>...
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ***@posting.goo gle.com>...
Hi. I want to create a trigger that only allows delete from table A if
corresponding record in table B does not exist. Any idea on how this
can be done? Thanks, Kenneth.


Sounds more like you want a reference constraint.
Do you have Primary keys and Foreign Keys set up on the corredsponding
tables? Looks like from the description table A is the parent table
(wiht a primary key) and table B is the child table (with a foreign
key that references the primary key of table A)

HTH,
ed


Hi, and thanks for your reply.

The thing is that it is not possible to use constraints based on PK's
and FK's in this database (the relations are placed on the applicaton
layer - hence it doesn't exist any PK's or FK's).

So: I figure trigger is the only solution to ensure the above
scenario. Any ideas on how the trigger would look?

Thanks,
Kenneth


I cannot think of any way for a trigger to STOP a delete, other than
maybe throwing an exception?

What do you mean the relations are in the application layer?
(CAUTION: That's the path to hell!)

Do you have two tables in the database or not?

It looks like you are using a screwdriver as a hammer. It doesn't work
well and in the end both the nail and the screwdriver are in bad
shape.

Sorry I cannot be more help, but I have a genetic disorder that
debilitates my programming skills when faced with an absurd
assignment. 8^)

Good luck! you are going to need it.
Ed
Jul 19 '05 #6
ed********@magi cinterface.com (Ed prochak) wrote in message news:<4b******* *************** ****@posting.go ogle.com>...
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ****@posting.go ogle.com>...
ed********@magi cinterface.com (Ed prochak) wrote in message news:<4b******* *************** ****@posting.go ogle.com>...
ke************* **@telenor.com (Kenneth Osenbroch) wrote in message news:<ce******* *************** ***@posting.goo gle.com>...
> Hi. I want to create a trigger that only allows delete from table A if
> corresponding record in table B does not exist. Any idea on how this
> can be done? Thanks, Kenneth.

Sounds more like you want a reference constraint.
Do you have Primary keys and Foreign Keys set up on the corredsponding
tables? Looks like from the description table A is the parent table
(wiht a primary key) and table B is the child table (with a foreign
key that references the primary key of table A)

HTH,
ed


Hi, and thanks for your reply.

The thing is that it is not possible to use constraints based on PK's
and FK's in this database (the relations are placed on the applicaton
layer - hence it doesn't exist any PK's or FK's).

So: I figure trigger is the only solution to ensure the above
scenario. Any ideas on how the trigger would look?

Thanks,
Kenneth


I cannot think of any way for a trigger to STOP a delete, other than
maybe throwing an exception?

What do you mean the relations are in the application layer?
(CAUTION: That's the path to hell!)

Do you have two tables in the database or not?

It looks like you are using a screwdriver as a hammer. It doesn't work
well and in the end both the nail and the screwdriver are in bad
shape.

Sorry I cannot be more help, but I have a genetic disorder that
debilitates my programming skills when faced with an absurd
assignment. 8^)

Good luck! you are going to need it.
Ed


:o)

I know! Consider the case closed.

Cheers,
Kenneth
Jul 19 '05 #7

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

Similar topics

5
3528
by: Neil Rutherford | last post by:
During testing of an application, i noticed a difference between SQL 2000 and SQL 7, both with identical config. In a nutshell: A table has a trigger for UPDATE and DELETE. When a column in the table is UPDATED the following happens: In autocommit mode, when entering a trigger the trancount equals 1 for both SQL 7 and 2000.
5
3956
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.
2
3093
by: R.Welz | last post by:
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...
1
4573
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...
6
3617
by: Jchick | last post by:
Im a newbie with a sql table in a MSDE database that contains fields of CustName, BrokerName, Type, Status. I am trying to write a trigger that does this: 1. When a new record in a table is added, I would like the selected fields to be copied to another table (call it Master). 2. If that Master table already contains a record where the CustName = CustName, then simply ignore. If CustName does not match anything, then add the record to...
8
7873
by: Benzine | last post by:
Hi, I have an issue with my replication at the moment. I will try to describe the scenario accurately. I am using MS SQL 2000 SP4 with Merge Replication. Subscribers connect to the publisher to upload/download changes. I have a trigger set up on one table which updates another, here is an example of the trigger: "CREATE TRIGGER qt_t_projTotal ON dbo.qt_quotes
3
16902
by: orakelvin | last post by:
Hi I am geting an error ORA-04091 table is mutuating, trigger cannot seen it. The situation arise because I am having two table employee Id empcol 101 111 101 222
1
2275
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on inserting to this table and I do next manual insert data into the last row in Enterprise Manager, I...
6
1377
by: Kenneth Osenbroch | last post by:
Hi. I want to create a trigger that only allows delete from table A if corresponding record in table B does not exist. Any idea on how this can be done? Thanks, Kenneth.
0
8832
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
8611
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
8685
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7449
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
6240
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
5709
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
4421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2067
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1819
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.