I have two tables, say Table A and Table B.
I want to have there to be a 1-* relationship between A and B, so for every A, there’s one specific B, and possible multiple other Bs.
So I have tables like this:
Table A
AKey INT IDENTITY PRIMARY KEY,
PrerequisiteKey INT FOREIGN KEY REFERENCES B(BKey)
Table B
BKey INT IDENTITY PRIMARY KEY,
RelationalAKey INT FOREIGN KEY REFERENCES A(AKey)
I want there to always be 1 B for every A. Should I try to do it this way (above) or should I programatically decide that the first entry in B with the A key = is the “prerequisite” B?
I’d like to be able to store that PrerequisiteKey as a separate field, but SQL doesn’t let me define tables this way because both tables need to exist before the constraints can be created.
EDIT: Or, is adding the first constraint after the tables are created the answer?
In these cases, you usually create the tables, then add the constraints. Use the ALTER TABLE syntax to change the FKs after creation.