Reputation: 79
There is a table named Department
, which shows the various departments of a University.
And there is a table named Branch
, which shows various branches (such as electrical, computer science, mechanical, automobile, law, literature etc.)
Conditions:
Department
can have any number of Branch
es.Branch
can be associated to single Department
.It means the cardinality between Department
: Branch
is "one to many".
Now the problem is, in this kind of situation, if I make dept_id
(an attribute of table: Department
) as primary key. How would I be able to associate more than one branch_id
( an attribute of table : Branch
), because if I do it so, I may violate the primary key condition of dept_id
itself.
How to deal with cardinality and making foreign key both go hand in hand?
Upvotes: 0
Views: 2031
Reputation: 55619
Add a foreign key dept_id
to Branch
referencing Department.dept_id
.
Since dept_id
is unique in Department
, each row in Branch
can thus obviously be linked to exactly one row in Department
, and there can be multiple rows in Branch
containing the same dept_id
.
Thus there will be a one-to-many relationship between the two.
To depict a many-to-many relationship, you need to create a third table that contains:
dept_id
referencing Department.dept_id
andbranch_id
referencing Branch.branch_id
Upvotes: 1