Create Trigger to delete

I have 2 tables:

Tbl_Recipes :    recipes_ID, recipes_title, recipes_catID, recipes_proc
and
Tbl_Recipes_Cats :  catID, cat_desc

There is a relationship b/w Tbl_Recipes.recipes_catID   and  Tbl_recipes_cats.catID

I want to set a trigger that will execute when a record in Tbl_recipes is DELETED.
The trigger will delete the corresponding CatID record from Tbl_Recipes_Cats if there are no more records referenceing the deleted catID.
LVL 1
bjv211Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
why no directly use the foreign key to do that for you, without trigger?


ALTER TABLE Tbl_Recipes_Cats
ADD CONSTRAINT fk_catID
FOREIGN KEY (recipes_catID) 
REFERENCES Tbl_Recipes_Cats (catID) ON DELETE CASCADE 

Open in new window

0
bjv211Author Commented:
where in that statement does it reference the Tbl_recipes table or is it understood?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry:
ALTER TABLE Tbl_Recipes
ADD CONSTRAINT fk_catID
FOREIGN KEY (recipes_catID) 
REFERENCES Tbl_Recipes_Cats (catID) ON DELETE CASCADE 

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

bjv211Author Commented:
Ok, I'm not sure what's going on...

I executed your sql with no errors, however, when i delete a recipe record, if it is the last recipe referencing that CatID, it does not delete the Cat record from Tbl_recipes_cats
0
dqmqCommented:
Try this:
Create trigger YourTrigger on tbl_recipes
After Delete
AS
Delete from tbl_recipes_cats c 
inner join deleted on c.catID =  deleted.recipes_catID
where not exists (select x from tbl_recipes r where
r.recipes_catID = deleted.recipes_catID)
 

Open in new window

0
dqmqCommented:
Note: cascade delete is not the correct solution for your requirements--you indeed need a trigger.
0
bjv211Author Commented:
I'm getting the error:  incorrect syntax near 'c'.
0
dqmqCommented:
Slight syntax error, let me try again
Create trigger YourTrigger on tbl_recipes
After Delete
AS
Delete from tbl_recipes_cats 
from tbl_recipes_cats c  
inner join deleted on c.catID =  deleted.recipes_catID
where not exists (select 1 from tbl_recipes r where
r.recipes_catID = deleted.recipes_catID)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjv211Author Commented:
Works great! thanks for the quick replies
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.