I'm not sure if I'm modeling this type of relationship correctly, perhaps someone could provide some insight into whether or not this is sound:
Say we have a typical parent-children type relationship where each one parent could have many children, but we need to keep track of the parent's single (gasp) favorite child...
- Identificación de los padres
so the links criss-cross, where one is a many to one, and the other is one to one. is this considered good design? Would it be better to have a column in the child table where only one row could be flagged a favorite at any time? Something else?
preguntado el 08 de enero de 11 a las 22:01
How about an additional table?
table favourite( ,parent_id ,child_id ,primary key(parent_id) );
It would help if for all favourite-oriented queries.
The best design in my opinion is the one that will prevent data inconsistencies and prevent people from having enough rope to hang themselves.
If you only want one favorite for a parent, then perhaps the parent should have a favoriteChildId column.
If you put a column for a favorite flag in the child, you'll have to do more work to ensure that two child records of the same parent can't both be marked as favorites.
If you suspect that in the future you may have more than one favorite, then the extra work may of course be worth it.
The design really depends on the relationships and whether or not there could be changes in the future to the possibility of different "favorite" relationships.