| | |
Database design question
Please support our Database Design advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2008
Posts: 1
Reputation:
Solved Threads: 0
I have two tables with foreign keys to the same parent table and a junction table between them to map a many to many relationship. I want to know how best to enforce data integrity in the junction table so that both parents' parent is the same.
Example:
How do I ensure that the Child1 and Child2 in ChildJunction have the same Parent?
The best I have so far is that ChildJunction includes a ParentCode column, I create a UNIQUE constraint on Child1Code + ParentCode in Child1 and a UNIQUE constraint on Child2Code + ParentCode in Child2 and the FOREIGN KEYS from ChildJunction are ChildJunction.Child1Code = Child1.Child1Code AND ChildJunction.ParentCode = Child1.ParentCode and ChildJunction.Child2Code = Child2.Child2Code AND ChildJunction.ParentCode = Child2.ParenCode.
But that leaves me with redundant data in ChildJunction (i.e. ParentCode).
Am I missing something? This must be a common problem? have I designed the Child tables wrong? Or the Parent table?
Any help gratefully received...
Thanks,
Simon
Example:
sql Syntax (Toggle Plain Text)
CREATE TABLE Parent ( ParentCode CHAR(3) NOT NULL, Name VARCHAR(50) NOT NULL, PRIMARY KEY (ParentCode) ); CREATE TABLE Child1 ( Child1Code CHAR(3) NOT NULL, Name VARCHAR(50) NOT NULL, ParentCode CHAR(3) REFERENCES Parent(ParentCode) PRIMARY KEY (Child1Code) ); CREATE TABLE Child2 ( Child2Code CHAR(3) NOT NULL, Name VARCHAR(50) NOT NULL, ParentCode CHAR(3) REFERENCES Parent(ParentCode) PRIMARY KEY (Child1Code) ); CREATE TABLE ChildJunction ( Child1Code CHAR(3) NOT NULL, Child2Code CHAR(3) NOT NULL, Attribute INT NOT NULL, PRIMARY KEY (Child1Code, Child2Code) );
The best I have so far is that ChildJunction includes a ParentCode column, I create a UNIQUE constraint on Child1Code + ParentCode in Child1 and a UNIQUE constraint on Child2Code + ParentCode in Child2 and the FOREIGN KEYS from ChildJunction are ChildJunction.Child1Code = Child1.Child1Code AND ChildJunction.ParentCode = Child1.ParentCode and ChildJunction.Child2Code = Child2.Child2Code AND ChildJunction.ParentCode = Child2.ParenCode.
But that leaves me with redundant data in ChildJunction (i.e. ParentCode).
Am I missing something? This must be a common problem? have I designed the Child tables wrong? Or the Parent table?
Any help gratefully received...
Thanks,
Simon
Last edited by cscgal; Sep 16th, 2008 at 9:48 pm. Reason: Added code tags
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 138
Normally what you would do is have a single child table, with an additional column for child_type_id
populate ChildType with your 2 entries of what child 1 and 2 are, lets say with CHILD_TYPE_ID's of 1 and 2
this way fk are in place, and if you need to query what would be in the old 'Child2' you can use
CREATE TABLE ChildType( CHILD_TYPE_ID int NOT NULL, CHILD_TYPE_NAME VARCHAR(20), PRIMARY_KEY(CHILD_TYPE_ID) ); CREATE TABLE Children( ChildCode char(3) NOT NULL, Name varchar(50) NOT NULL, CHILD_TYPE_ID int NOT NULL REFERENCES ChildType(CHILD_TYPE_ID), ParentCode char(3) REFERENCES Parent(ParentCode) PRIMARY KEY (ChildCode) );
populate ChildType with your 2 entries of what child 1 and 2 are, lets say with CHILD_TYPE_ID's of 1 and 2
this way fk are in place, and if you need to query what would be in the old 'Child2' you can use
-- child 2 query select * from Children where CHILD_TYPE_ID = 2; -- child 1 query select * from Childrean where CHILD_TYPE-ID = 1;
Last edited by dickersonka; Sep 17th, 2008 at 12:02 pm.
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- newbie question about database design (Computer Science)
- Database Design (Computer Science)
- Database Design Question (Database Design)
- Database Design Question (Database Design)
- your ideas on database design??? (Database Design)
- Cities and Districts DataBase - Basic Question (Database Design)
Other Threads in the Database Design Forum
- Previous Thread: Urgent UML Help
- Next Thread: Regarding ASCII data type
Views: 695 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Database Design






