Composite Foreign Key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scoarescoare
    New Member
    • Apr 2007
    • 5

    Composite Foreign Key

    I have been looking and looking and cannot come up with a clear, solid solution.

    I have a table and that table uses 3 columns to create a composite PK. I need to use this PK as a foreign key in another table. That means I need to add these three columns to the child table which...quite frankly..i think is kind of a pain. I cannot believe there is not a simpler method. If anyone has any ideas or alternative solutions, pleaes give me a hollar. Thanks a lot.
  • frozenmist
    Recognized Expert New Member
    • May 2007
    • 179

    #2
    Hi,
    I dont know how good this is but why dont you try using an identity column.
    For each combination of the three keys the identity column will have a value.
    Pust this ID column value in the child table.
    Just a thought.
    Hope I could give something meaningful
    Cheers

    Comment

    • chandu031
      Recognized Expert New Member
      • Mar 2007
      • 77

      #3
      Originally posted by scoarescoare
      I have been looking and looking and cannot come up with a clear, solid solution.

      I have a table and that table uses 3 columns to create a composite PK. I need to use this PK as a foreign key in another table. That means I need to add these three columns to the child table which...quite frankly..i think is kind of a pain. I cannot believe there is not a simpler method. If anyone has any ideas or alternative solutions, pleaes give me a hollar. Thanks a lot.
      Hi,

      You can have a surrogate key on the parent table which as frozen pointed out would be an identity column. This surrogate key can then be used as a foreign key in the child table.

      Comment

      • scoarescoare
        New Member
        • Apr 2007
        • 5

        #4
        Right..thanks.. I see what you two are saying. However..wouldn 't this allow for the three columns to have redundant data and not be unique? I kind of needs this composite PK be unique. Any ideas?

        Comment

        • pradeep kaltari
          Recognized Expert New Member
          • May 2007
          • 102

          #5
          Originally posted by scoarescoare
          Right..thanks.. I see what you two are saying. However..wouldn 't this allow for the three columns to have redundant data and not be unique? I kind of needs this composite PK be unique. Any ideas?
          Hi,
          You can still have a unique constraint on the combination of those three columns and the identity column can be the primary key.
          [code=sql]
          ALTER TABLE <table_name> ADD CONSTRAINT unique_col UNIQUE(col1,col 2,col2)
          [/code]

          Regards,
          Pradeep

          Comment

          • scoarescoare
            New Member
            • Apr 2007
            • 5

            #6
            yes yes yes! that is perfect. Thanks for opening up my eyes everyone. Really, thanks a lot!

            Comment

            Working...