I create four tables in sql 2005 as
StudentTbl
s_id(PK) | int |
s_name | varchar(30) |
s_add | varchar(50) |
|
|
DeptTbl
d_id(PK) | int |
d_name | varchar(50) |
s_id(FK) | int |
STbl_backup
s_id(PK) | int |
s_name | varchar(30) |
s_add | varchar(50) |
|
DTbl_backup
d_id(PK) | int |
d_name | varchar(50) |
s_id(FK) | int |
and I set StudentTbl
cascade with DeptTbl and STbl_backup
cascade with DTbl_backup.
I write Trigger for Delete on StudentTbl and DeptTbl like this way:
create trigger
Backup_stblon
StudentTbl for delete
as
begin
declare @s_id int
declare @s_name varchar(30)
declare @s_add varchar(50)
select @s_id=s_id , @s_name=s_name,@s_add=s_add from deletedinsert into STbl_backup values(@s_id,@s_name,@s_add)end
create trigger
Backup_dtblon
DeptTbl for delete
as
begin
declare @d_id int
declare @d_name varchar(30)
declare @s_id int
select @d_id=d_id , @d_name=d_name,@s_id=s_id from deletedinsert into DTbl_backup values(@d_id,@d_name,@s_id)end
Finally I inserted four records .
Expectation of trigger Backup_stbl is to get deleted records of StudentTbl and insert into the STbl_backup table
same is the case for Backup_dtbl.
but when I write following query:
delete from StudentTbl where s_id=2then It display me error like :
Msg 547, Level 16, State 0, Procedure
Backup_dtbl, Line 15
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_DTbl_STbl". The conflict occurred in database "test", table "dbo.STbl", column 's_Id'.
The statement has been terminated.
I Provide You all details If you understand my problem then please suggest me solution. Thanks in advance.