MesutAtasoy
MesutAtasoy

Reputation: 822

How to create a relationship with a table which has a composite primary key?

I have two tables. I want to create a relationship between them. But the first table has two primary keys.

SpaDefinition Table:

BranchId,Id(PK),LanguageId(PK),Name

SpaPhotos Table:

BranchId, Id(PK),SpaDefinitionId,Data

The SpaPhotos table should have a relationship with SpaDefinition table.

Could anyone help me?

Upvotes: 1

Views: 69

Answers (1)

Tab Alleman
Tab Alleman

Reputation: 31795

The only way you can create a foreign key in SpaPhotos that will reference SpaDefinition is to either:

  1. Add LanguageId to SpaPhotos, or
  2. Remove LanguageId from the Primary Key of SpaDefinition

A foreign key must contain ALL of the columns of the primary key of the table it is referencing.

Upvotes: 2

Related Questions