Diseño de tablas: ¿una a muchas y una a una al mismo tiempo?

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...


Table Parent

- FavoriteChildID

y


Table 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?

Gracias.

preguntado el 08 de enero de 11 a las 22:01

i dont see any problem with this approach given a few conditions. Are parents and children entirely distinct groups? No grand-childre, grand-parents, etc? There is just one set of parents who are never children, and one set of children who are never parents? children always have exactly one parent? -

just add a colum to child parent table where your can save his favorite child -

2 Respuestas

How about an additional table?

table favourite(
  ,parent_id
  ,child_id
  ,primary key(parent_id)
);

It would help if for all favourite-oriented queries.

Respondido el 09 de enero de 11 a las 01:01

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.

Respondido el 09 de enero de 11 a las 01:01

am I breaking any cardinal rules by keeping the favorite child link in the table? something just doesn't 'feel' right about it to me. - user568377

@user568377 - If you were linking parent to child as in a parent has N children, then you'd probably want either a 3rd table as Ronnis suggested, or you'd want to put a link back to the parent in the child tables. But if there is only one favorite, then you should be fine either way. If you don't vez want more than one favorite, then give the parent a favoriteChildId. It's really up to you and the decision should be based the requirements of your application. - jmort253

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.