This is a rather common problem, when the design has a "diamond" shape. See similar questions:
Many to Many and Weak Entities
With MySQL, I'd use something like this:
(0) Note that I prefer user_id as name for the primary key of users and not id for all the tables. I find the SQL code totally confusing otherwise (plus you can use the JOIN ... USING (tablename_id) syntax).
(1) The extra UNIQUE constraints in tables hosts and contacts are needed for the foreign keys from the domains table.
(2) The two FOREIGN KEY constraints from the domains table are changed to use composite keys (include the user_id).
Table users
CREATE TABLE users
( user_id INT NOT NULL AUTO_INCREMENT
, username VARCHAR(45) NOT NULL
-- other columns
, PRIMARY KEY (user_id)
) ;
Table contacts
CREATE TABLE contacts
( contact_id INT NOT NULL AUTO_INCREMENT
, user_id INT NOT NULL
, info TEXT
, PRIMARY KEY (contact_id)
, UNIQUE INDEX (user_id, contact_id) -- added, see comment 1 above
, INDEX (user_id)
, FOREIGN KEY (user_id)
REFERENCES users (user_id)
) ;
Table hosts
CREATE TABLE hosts
( host_id INT NOT NULL AUTO_INCREMENT
, user_id INT NOT NULL
, name VARCHAR(45) NOT NULL
--
, PRIMARY KEY (host_id)
, UNIQUE INDEX (user_id, host_id) -- added, see comment 1 above
, INDEX (user_id)
, FOREIGN KEY (user_id)
REFERENCES users (user_id)
) ;
Table domains
CREATE TABLE domains
( domain_id INT NOT NULL AUTO_INCREMENT
, user_id INT NOT NULL
, contact_id INT NOT NULL
, host_id INT NULL -- nullable based on comments
, name VARCHAR(45) NOT NULL
-- other columns
, PRIMARY KEY (domain_id)
, FOREIGN KEY (user_id, contact_id) -- composite FK, see comment 2
REFERENCES contacts (user_id, contact_id)
, FOREIGN KEY (user_id, host_id) -- composite FK, see comment 2
REFERENCES hosts (user_id, host_id)
) ;
1 artist : many albumsactually. It's the same diamond shape with user (or artist) at the top. The only differences may lie with whether an album track can exist without a track or an album (it cannot) which may not be the case with your requirements. – ypercubeᵀᴹ Sep 09 '13 at 11:27albumsandtracks. I see amany to manyrelationship in this example,while in mine I can't see one. – giannis christofakis Sep 09 '13 at 11:29domainsentity is basically a many to many relationship between contacts and hosts. – ypercubeᵀᴹ Sep 09 '13 at 11:30one to manyrelationship? Or I am fundamentally wrong? – giannis christofakis Sep 09 '13 at 11:38