I have Object1 and junction table and Object2. Object2 is table that has many junction tables, but can have only one junction table torefrencing to it. When table Object1 is removed, then junction table and Object2 should be removed. How can I make foreign keys in this situation? But when Object2 is removed, then only junction table should be removed, not Object1. I'm using SQL Server 2008.
+1
A:
I suggest:
When row in Table 1 is removed then junction table (JT) and row in Table 2 should be removed. Answer: Set up FK in JT referencing Table 1 with ON DELETE CASCADE set to on. Set up FK in Table 2 referencing JT with ON DELETE CASCADE set to on.
When row in Table 2 is removed then row in JT should be removed. Answer: You might have to set up a trigger for this.
Noel Abrahams
2010-10-06 06:56:37
+1
A:
You could
- apply CASCADING DELETES to your foreign key from your
junction
table toObject2
. - add a Trigger to
Object1
, check for deletion and delete the corresponding records in yourjunction
table andObject2
.
Lieven
2010-10-06 06:57:04