1

Guys i currently have 2 tables that reference a lookup table. Ive been tasked with modifiying the database to allow the tables to reference more than one value in the lookup table.

My idea is to create a new table that has a new PK and a column that references the lookup table. The PK of the new table is then dumped into the parent tables thus maintaining referential integrity E.G

Current

 Contracts
 =========
 PK Id
 FK lookupId

 Warranties
 ==========
 PK Id
 FK lookupId

New

 Contracts
 =========
 PK Id
 FK LinkingTableId

 Warranties
 ==========
 PK Id
 FK LinkingTableId

 LinkingTable
 ============
 PK Id
 FK LookupId

Is this a good way of handling the multiple references?

3
  • So you have a lookup table to the lookup table now? It looks ok but it can't be fully interpreted without knowing wht the lookup table does. Commented Aug 3, 2011 at 13:59
  • @cularis the lookup table is a list of disciplines. nothing more. My company now want the user to be able to select a number of disciplines rather than just one and yes i guess it would be a lookup table to another lookup table. Any thoughts? Commented Aug 3, 2011 at 14:05
  • 1
    Ok then it's standard. To change a one-to-one relation to a one-to-many you need something like your linking table so your solution is ok. Commented Aug 3, 2011 at 14:07

2 Answers 2

2

Your NEW proposed table structure cannot handle multiple references, because each Contract and Warranty row is linked to just one single LinkingTable row.

I see two ways to do this:

  1. Add two distinct linking tables, one for each many-to-many relationship:

    Contracts
    =========
    PK Id
    Other_Contract_Stuff
    
    ContractLinkingTable
    ====================
    PK ContractId, LookupId
    FK ContractId
    FK LookupId
    
    Warranties
    ==========
    PK Id
    Other_Warranty_Stuff
    
    WarrantyLinkingTable
    ====================
    PK WarrantyId, LookupId
    FK WarrantyId
    FK LookupId
    
  2. Add a single new linking table, to handle both many-to-many relationships:

    Contracts
    =========
    PK Id
    Other_Contract_Stuff
    
    Warranties
    ==========
    PK Id
    Other_Warranty_Stuff
    
    LinkingTable
    ====================
    PK LinkedType, LinkedId, LookupId
       LinkedType ("C" for Contract, "W" for Warranty)
       LinkedId (either a ContractId or a WarrantyId)
    FK LookupId
    

The first is the correct and preferrable solution, as the engine can enforce the table foreign keys and data integrity is less at risk. I have seen the latter used in some real world projects. It works, but it is not a good solution... and sooner or later you will have problems with it.

Sign up to request clarification or add additional context in comments.

2 Comments

yes your right. Looking at back at my solution it is obvious now. Thanks for pointing that out. In your first solution are you saying that i need four columns in the linking table? How do i set the lookupid and the warrantyId to both be primary keys
You only need 2 columns, but you can and should define 3 constraints: 1 PK and 2 FK. 'ALTER TABLE ContractLinkingTable ADD CONSTRAINT pk_ContractLinkingTable PRIMARY (ContractId,LookupId)'
1

The approach you suggest is the standard one. The linking table is known as a "Junction" table. http://en.wikipedia.org/wiki/Junction_table

I would suggest a change to your naming conventions. Also your main data tables don't need the FK any more and the junction table doesn't need its own primary key but rather a compound key on the two foreign keys.

 Contracts
 =========
 PK Id
 Other_Stuff

 Warranties
 ==========
 PK Id
 Other_Stuff

 Contract_Warranties
 ============
 FK ContactId
 FK WarrantyId

2 Comments

The linking or junction table should still have a primary key. Perhaps you meant that it doesn't need a surrogate single-column key?
@Tom H. Thanks. Answer modified accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.