I have following database design (by => foreign key constraints are depicted):
Company[id] CompanyRealm[id, company_id=>Company.id]many2many Project[id, company_id=>Company.id] ProjectRealm[id, project_id=>Project.id, company_realm_id=>CompanyRealm.id]many2many
Problem is that provided database design allows inconsistent data. For example:
Company1(id=1) Company2(id=2) CompanyRealm(id=11, company_id=1) Project(id=33, company_id=2) ProjectRealm(id=44, project_id=33, company_realm_id=11)
(Company can have many Realms, Project belong to Company and may concern any number of company's Realms)
ProjectRealm refers to two different companies:
- Company1 (via CompanyRealm); and
- Company2 (via Project).
Is something wrong with my DB design?
If yes - which prescriptions are violated?
If no - how to prevent incorrect data insertion (via constraint? trigger with exception?)
redundantdata. In my opinion it is better than allowing inconsistent data. But in the name of art: are you solution violate some of NF? – kakabomba May 14 '16 at 16:26company_idis redundant in theProjectRealm, true. In a way it is but since it's part of aUNIQUEkey ((project_id, company_id, realm_id)is unique I assume) it doesn't violate any NF. I think I have discussed this is another question extensively, let me find the link. By the way, I would remove theCompanyRealm (id)and theProjectRealm (id)if it was my design, unless they are there for specific, performance issues, with the DBMS you plan to implement this in. – ypercubeᵀᴹ May 14 '16 at 18:17