Note that there are some explanatory texts on larger screens.

plurals
  1. POData Modeling: What is a good relational design when a table has several foreign key constrainst to a single table?
    primarykey
    data
    text
    <p>I have 2 tables: 1. Employees 2. Vouchers</p> <p>Employees table has a single primary key. Vouchers table has 3 foreign key constraints referencing the Employees table.</p> <p>The following is a sample T-SQL script (not the actual table script) to create both tables and their relationship in SQL Server:</p> <pre><code>IF OBJECT_ID('dbo.Vouchers') IS NOT NULL DROP TABLE dbo.Vouchers IF OBJECT_ID('dbo.Employees') IS NOT NULL DROP TABLE dbo.Employees GO CREATE TABLE Employees ( ObjectID INT NOT NULL PRIMARY KEY IDENTITY ) CREATE TABLE Vouchers ( ObjectID INT NOT NULL PRIMARY KEY IDENTITY, IssuedBy INT, ReceivedBy INT, ApprovedBy INT, CONSTRAINT fk_Vouchers_Employees_IssuedBy FOREIGN KEY (IssuedBy) REFERENCES Employees (ObjectID) ON UPDATE CASCADE ON DELETE NO ACTION, CONSTRAINT fk_Vouchers_Employees_ReceivedBy FOREIGN KEY (ReceivedBy) REFERENCES Employees (ObjectID) ON UPDATE CASCADE ON DELETE NO ACTION, CONSTRAINT fk_Vouchers_Employees_ApprovedBy FOREIGN KEY (ApprovedBy) REFERENCES Employees (ObjectID) ON UPDATE CASCADE ON DELETE NO ACTION ) </code></pre> <p>But an error is thrown:</p> <pre><code>Msg 1785, Level 16, State 0, Line 7 Introducing FOREIGN KEY constraint 'fk_Vouchers_Employees_ReceivedBy' on table 'Vouchers' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. </code></pre> <p>I don't have an idea of what efficient solution is available here. The requirements on the relationship is that: whenever an Employee is deleted, the Voucher that references some of its columns to the Employee does not get deleted (ON DELETE CASCADE is not an option). Instead, the values of the columns (IssuedBy, ReceivedBy and/or ApprovedBy) that are referenced to the deleted Employee should be set to NULL (since the columns are NULLABLE).</p> <p>Many thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload