Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have 4 tables Table1, Table2, Table3, Table4. Whenever data is inserted in Table1, data is also insert in Table4. Similarly When data is inserted in Table2, and Table3 data is also insert in Table4. so we cannot create Foreign key on Table4.

so Please tell me, how to create trigger on Table1, Table2 and Table3 to preserve rows to delete it. if it is exists in Table4.

Thanks in advance.

What I have tried:

Create trigger trgDeleteDebitVoucher on TblDebitVoucher For Delete
As
IF EXISTS (SELECT * FROM TblStatement Where ProjectNo In (Select NoteNo From TblDebitVoucher))
BEGIN
RAISERROR ('This Voucher is used in Statement Table, So Cannot Delete It.', 16, 1);
ROLLBACK TRANSACTION;
END;
Posted
Updated 30-Nov-16 10:06am
Comments
Tomas Takac 29-Nov-16 9:33am    
I fail to see why you cannot create a foreign key, could you please elaborate?
Peter Leow 29-Nov-16 10:46am    
Possible. For example, a foreign key value exists in table 1 cannot be inserted into table 4 if it does not also exist in the other tables.

Take a step back and rethink your design. Any other way to get around this foreign key referencing multiple tables issue? As it is, there is also the danger of inserting duplicate primary key into multiple tables. I am proposing a separate table to hold the primary key and for the other four tables to link to it through foreign key. What do you think?
 
Share this answer
 
Comments
kantagrawal 30-Nov-16 0:09am    
Dear Peter Leow,
we are not inserting the same primary key in Table4. the Primary key column values of Table1, Table2 and Table3 are different. and we are inserting these values in Table4. so they are different in column. but we cannot set this column of Table4 as foreign key, because we can set only one foreign key in a column of table to references the primary key. so i think we need to create trigger on Table1, Table2, Table3 for Delete. if that primary key value exists in Table4. if there is any other procedure, then please tell me.
Peter Leow 30-Nov-16 0:26am    
Yes, the reason why you can't use foreign key is because that intended foreign key column in table 4 is referencing multiple tables 1, 2, 3, right? Even they are different in values in tables 1, 2, 3. I still stand by my suggestion. Do let me know if you figure out the trigger solution. I am always prepare to learn.
In your trigger change this

(SELECT * FROM TblStatement Where ProjectNo In (Select NoteNo From TblDebitVoucher))

TO

(SELECT * FROM TblStatement Where ProjectNo In (Select NoteNo From deleted))


You could also switch to an INSTEAD OF trigger. Then you don't need to rollback the transaction, just don't run the delete at all.

Good Luck
Brent
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900