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:
- A
Departmentcan have any number ofBranches. - A
Branchcan be associated to singleDepartment.
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?
Add a foreign key
dept_idtoBranchreferencingDepartment.dept_id.Since
dept_idis unique inDepartment, each row inBranchcan thus obviously be linked to exactly one row inDepartment, and there can be multiple rows inBranchcontaining the samedept_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_idreferencingDepartment.dept_idandbranch_idreferencingBranch.branch_id