¿Cómo se pueden anular las propiedades de una tabla por las propiedades del grupo al que pertenece, mientras se diseña el esquema de la base de datos?

I have a table called Person:
person_id | first_name | middle_name | last_name | group_id | property_id
(group_id will refer to the group_id column of table GroupOfPerson and property_id will refer to the property_id column of table Property)

And another table called GroupOfPerson:
group_id | group_name | property_id
(property_id will refer to the property_id column of table Property)

And another table called Property:
property_id | property_name

Here, if the Person belongs to GroupOfPerson, the property_id from Person table will be overridden by the property_id from GroupOfPerson table for the corresponding Person. How can this be done effectively done while designing the database schema? Thanks in advance.

preguntado el 09 de marzo de 12 a las 15:03

Not sure what you are asking. What does this "overridding" mean? E.g.: does a Person't property is always the same as his GroupOfPerson's property? -

@ypercube : It should, but how do you achieve it design wise while designing the database tables? -

If a Person always belongs to a Group and you want that Group's Property to define that Person's property, then you simply remove the Person.property_id de la mesa. -

@ypercube : But what when the Person does not belong to any group? It still needs to have a property which will be only his/her own property. And hence we need the Person.property_id. Well that's how it is designed right now, but any better ideas are greatly appreciated. -

In that case (where Person.group_id can be Null), your design is fine. You can use the LEFT JOIN / COALESCE() solution by Raihan. -

1 Respuestas

I don't think you can define it in the table design level.

However you can enforce it with insert/update triggers on Person table. The trigger will copy the value of property_id desde GroupOfPerson mesa para Person mesa si el group_id value is not null in the Person mesa.

Alternately you can do it at the query level. Whenever you select data from Person table, join it with GroupOfPerson table and select the aprropriate source for property_id column based on the null/notnull value in group_id columna.

respondido 09 mar '12, 16:03

For the alternate solution, you mean to say I can use COALESCE? That sounds good. Actually I was wondering if there is a way to design the schema in such a manner that this can be achieved. But it doesn't seems to be possible at the design level as you suggested. Thanks a lot for your valuable inputs. I am gonna try this and see how effective it is. - bns007

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