tags:

views:

54

answers:

1

I've got two tables.

One is "Corporations" (e.g., one record is for Taco Bell). The index and PK for this table is named "Id".

I've got another table: "Branches" (e.g., one record is for Los Angeles). The Branch table has a column named "Corporation". This column should only accept an "Id" value that corresponds with an "Id" in the "Corporations" table.

How do I enforce this? Do I add a constraint? How do I do that?

If I'm barking up the wrong tree, how do I define this relationship between Corporations and Branches?

+5  A: 

Add a FOREIGN KEY to Branches that references Corporations.

i.e. in the CREATE TABLE for Branches:

CREATE TABLE Branches
(
  ...
  CorporationId int NOT NULL
    CONSTRAINT FOREIGN KEY FK_Branches_Corporations REFERENCES Corporations(Id)
  ...
)
Mitch Wheat
or to modify your existing table:ALTER TABLE BranchesALTER CorporationId int NOT NULL CONSTRAINT FOREIGN KEY FK_Branches_Corporations REFERENCES Corporations(Id)
ck