I have have 2 tables User and Group.
I have a table Attributes shared by user and group with columns:
attributeName.AttributeValue.ObjectID.
ObjectID points to either the primary key of user or the primary key of Group.
I have added a foreign constraint with Cascade on Delete in order to delete automatically the attributes when user or a group is deleted.
The problem now is when I insert an attribute for the user, I have a foreign key constraint because the group does not exist.
How should I proceed?
You have basically 3 options:
Keep your current design, but replace
Attribute.ObjectIDwithUserIDandGroupID, attach a separate FK to each of them (one towardsGroupand the other towardsUser) and allow either to be NULL. You’d also want a CHECK constraint to ensure not both of them are NULL.Split
Attributetable toUserAttributeandGroupAttribute, thus separating each foreign key into its own table.Use inheritance, like this:
The solution (1) is highly dependent on how your DBMS handles UNIQUE on NULLs and both (1) and (2) allow the same
AttributeNameto be used for two different attributes, one for user an the other for group.