vppy
vppy

Reputation: 367

What is better way to create reference within 2 SQL tables?

I have Corporate table which collect Organizations. Each organization could has several branches. If there are many branches it is also the main one - head office.

How is better to make it?

  1. add in Corporate table primary branch foreign key?
  2. or add in Branches table is_primary column to mark it as true.

I suppose a second way could be a preferable but I need than some algorithm to be sure that there are no 2 head_offices for corporate.

Upvotes: 1

Views: 42

Answers (1)

Gauravsa
Gauravsa

Reputation: 6528

You can make three tables:

  1. One is Corporate which has org_id as primary key.
  2. Another is Branch which has branch_id as primary key.
  3. Third is CorporateBranch table, which has org_id, branch_id as primary key

Upvotes: 3

Related Questions