I am trying to figure out a method of creating a shortcut between three tables .
I have a master table, A, a table that references the first table, B, and a table that references B, C. I want to create a shortcut between A and C so I don't have to use table B.
In the below example, I want to ensure that the foreign keys FK_A_ID1 and FK_A_ID2 are always equal to each other, causing it to fail when the last insert statement is executed.
CREATE TABLE A (
ID int unique identity,
num int)
CREATE TABLE B (
ID int unique identity,
A_ID int NOT NULL,
CONSTRAINT FK_A_ID1 FOREIGN KEY (A_ID) REFERENCES A (ID))
CREATE TABLE C (
ID int unique identity,
A_ID int NOT NULL,
B_ID int NOT NULL,
CONSTRAINT FK_A_ID2 FOREIGN KEY (A_ID) REFERENCES A (ID),
CONSTRAINT FK_B_ID FOREIGN KEY (B_ID) REFERENCES B (ID))
INSERT INTO A VALUES (0);
DECLARE @A1 int = SCOPE_IDENTITY();
INSERT INTO A VALUES (1);
DECLARE @A2 int = SCOPE_IDENTITY();
INSERT INTO B Values (@A1);
DECLARE @B1 int = SCOPE_IDENTITY();
INSERT INTO C Values (@A2, @B1);
Is this possible by use of foreign keys or is there another built-in function that I don't know of?
The goal of this is to have a reliable 'shortcut' between tables A and C