I have a database with four tables as follows:
Addressbook
--------------------
id
more fields
Contact
---------------------
id
addressbook id
more fields
Group
---------------------
id
addressbook id
more fields
Group to Contact
---------------------
Composite key
Group id
Contact id
My relationships are one to many for addressbook > contact, one to many for addressbook > group and many to many between contact and groups.
So in summary, I have an addressbook. Contacts and groups can be stored within it and they cannot be stored in more than one addressbook. Furthermore as many contacts that are needed can be added to as many groups as are needed.
My question now poses as follows. I wish to add the constraint that a contact can only be a member of a group if both of them have the same addressbook id.
As I am not a database person this is boggling my brain. Does this mean I have designed my table structure wrong? Or does this mean that I have to add a check somewhere before inserting into the group to contact table? This seems wrong to me because I would want it to be impossible for SQL queries to link contacts to groups if they do not have the same id.
As A CHECK Constraint can't include sub-queries. You could create a trigger that checks that the group and contact have the same addressbookid and generate an error if they do not.
Although a database trigger defined to enforce an integrity rule does not check the data already in the table, I would recommended that you use a trigger only when the integrity rule cannot be enforced by an integrity constraint.
Note:(This is from memory so probably not syntactically correct)
As BonyT suggestion:
You should be able to accomplish this by adding a addressbook_id column to your Group to Contact bridge table, then using a compound foreign key to both the Contacts and Groups tables.
In PostgreSQL (but easily adaptable to any DB, or at least any DB that supports compound FKs):
By using the same addressbook_id column in both constraints, you are of course enforcing that they are the same in both referenced tables.
OK - the Many to Many is governed by the GroupToContact table.
So the constraints are between Group and GroupToContact and between Contact and GroupToContact (GTC)
Namely
And
So you will need to add AddressBookId to GroupToContact table
One further note - you should not define any relationship between Contact and Group directly - instead you just define the OneToMany relationships each has with the GroupToContact table.
In your E-R (Entity-Relationship) model, the entities Group and Contact are (or should be) "dependent entities", which is to say that the existence of a Group or Contact is predicated upon that of 1 or more other entities, in this case AddressBook, that contributes to the identity of the dependent entity. The primary key of a dependent entity is composite and includes foreign keys to the entity(ies) upon which it is dependent.
The primary key of both Contact and Group include the primary key of the AddressBook to which they belong. Once you do that, everything falls into place:
Cheers.