SQL Server: How to implement 3-way relationship with cascades
0
votes
1
answer
78
views
This is the schema I'm working with:
Here's the DBML to recreate in https://dbdiagram.io/d :
Table University {
ID integer [primary key]
}
Table Professor {
ID integer [primary key]
UniversityID integer [primary key, ref: > University.ID]
}
Table Class {
ID integer [primary key]
UniversityID integer [primary key, ref: > University.ID]
ProfessorID integer [null]
}
Ref: Class.(ProfessorID, UniversityID) - Professor.(ID, UniversityID)
My goal:
- A University can have multiple Classes and Professors.
- A Class can have one or no Professors.
- Deleting a University deletes all of its Professors and Classes
- **Deleting a Professor sets Class.ProfessorID to NULL**
The last goal is causing us problems. SQL Server prevents adding another cascading foreign key constraint due to multiple cascade paths, and a trigger can't work because:
1) the delete query is rejected due to the foreign key constraint on Professor, so a FOR DELETE trigger will never run
2) SQL cannot create an INSTEAD OF DELETE because of the cascading relationship to the University
How can I achieve this?

Asked by Carl Schmidt
(3 rep)
Jan 23, 2025, 12:48 AM
Last activity: Jan 23, 2025, 10:42 AM
Last activity: Jan 23, 2025, 10:42 AM