Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to use delete trigger to maintain integrity

Author  Topic 

sha_agrawal
Starting Member

24 Posts

Posted - 2009-03-17 : 09:40:47
I have 2 tables e.g.
(1)Cust (contains a field named CustID
(2)Sale (Contains a filed named Custid as Foreign key)

I want to use delete trigger to prevent deletion of such CustID from Cust which are already existing in Sale. How to do it, pls.help me.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-17 : 09:56:41
You dont need a trigger I think....Just use a DELETE constraint on your table for the foreign key.

"ON DELETE NO ACTION". This would ensure that you cannot delete a row to which there is an existing foreign key.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-03-17 : 09:59:17
Your foreign key is a CONSTRAINT which by it's nature won't allow you to delete a cust row that is referenced in Sale. You don't need a trigger.

EDIT:


See for yourself:

create table cust (custid int primary key clustered)
go
create table sale (custid int references cust(custid), i int)
insert cust values (1)
insert sale values (1, 1)
go
--This will cause an error
print 'try the delete'
delete cust where custid = 1
go
drop table sale
drop table cust

OUTPUT:
(1 row(s) affected)

(1 row(s) affected)
try the delete
Msg 547, Level 16, State 0, Line 3
The DELETE statement conflicted with the REFERENCE constraint "FK__sale__custid__40F9A68C". The conflict occurred in database "junk", table "dbo.sale", column 'custid'.
The statement has been terminated.


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-17 : 10:28:46
quote:
Originally posted by vijayisonly

You dont need a trigger I think....Just use a DELETE constraint on your table for the foreign key.

"ON DELETE NO ACTION". This would ensure that you cannot delete a row to which there is an existing foreign key.


whats the need of DELETE constraint? As TG suggested the FK itself will make sure you cant delete values from Cust when its referd to in Sale. So you dont need to do anything additional
Go to Top of Page

sha_agrawal
Starting Member

24 Posts

Posted - 2009-03-18 : 11:24:00
Hello Vijayisonly and TG, thanks for reply.
But my front-end is VB6. If I use constraint, it will show SQL Server Constraint violation message and terminate programme. I want to show message also to user.Pls.give me delete trigger code.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-19 : 11:07:22
quote:
Originally posted by sha_agrawal

Hello Vijayisonly and TG, thanks for reply.
But my front-end is VB6. If I use constraint, it will show SQL Server Constraint violation message and terminate programme. I want to show message also to user.Pls.give me delete trigger code.


then what you should be doing is to check whether any instance of value to be deleted exists in child table (Sale) and raise error from vb application when found without doing the delete.
Go to Top of Page
   

- Advertisement -