Reputation: 58
I have two entity classes, Student and Committee.
I can not post images, but image of current db design can be found here: current db design
or in text:
**Committee**
id
name
**students_committees**
stud_id
com_id
**students**
name
gender
grade
students can be in many committees, and a committee has many students.
Now I want to add a leader to committee (a committee can have more than 1 leader). What is the best way to do this?
committee_leaders(stud_id, com_id)
students_committees(stud_id, com_id, is_leader)
I would assume an extra join table is the best choice. We would not need to traverse the entire current join table to find the leaders for a committee. My team does not agree.
Upvotes: 3
Views: 812
Reputation: 10105
You should go with below.
Add another join table: committee_leaders(stud_id, com_id)
Otherwise you will not be able to add details of committee leader...+ Point 1 will give you more flexibility as told by other users
When moving toward point 2, This may violate 3 Normal Form
Reference
http://databases.about.com/od/specificproducts/a/normalization.htm
You can use cascade delete so deletion of record from parent table will automatically delete the entry from child table...
Upvotes: 2
Reputation: 115540
My choice would be option 1 if the points 2 and 3 below matter in your case. If not, option 2 is simpler.
Pros (vs option 2):
(as you pointed) you would not need to traverse the entire current join table to find the leaders for a committee. You'll just join committee
with committee_leaders
. (not much difference though, you'll be just searching a smaller index, the difference may be negligible.)
you can easily enforce the constraint that a committee has no more than one leader.
if you want to add other attributes for the leaders, like when the leader was chosen (elected), what other title they gain, etc, you'll not be filling the studens_committees
table with columns that will be NULL
almost everywhere.
Cons:
leaving the committe would require DELETEs on 2 tables (or 1 with cascading delete in effect).
inserting a new student as leader will require two INSERTs in 2 tables.
changing the leader in a committee will require one DELETE and one INSERT vs 2 UPDATEs (possibly in one statement).
Upvotes: 3
Reputation: 5273
I'm in favor of adding the attribute to the existing join table too, because:
Upvotes: 2
Reputation: 432271
I'd use "option 2": the extra column.
This means you don't have to worry about
Upvotes: 3