3 primary keys possible in a table?

Discussion in 'PHP' started by shimon ben david, Apr 14, 2009.

  1. #1
    Hello - relative noob trying to resolve a 'many to many' relationships situation with either two or one look up tables.

    I have a hierarchical database Cat, Type and Prod. I can see doing a look up table between Cat.id and Type.id is a good move AND likewise creating a lookup table between Type.id and Prod.id (always with primary keys assigned on the columns)...

    My questions are:
    1.Can I combine the two tables to make a single 3-column lookup table?
    2.Can each of these three columns have Primary Key status? and
    3.Providing I can code this, is this an efficient or really baaad way of solving the relationship lookup?
     
    shimon ben david, Apr 14, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Only one primary key. But you can have any number of additional keys.

    You can keep the tables separate and then create a view to facilitate hierarchical lookups.
     
    SmallPotatoes, Apr 14, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Only one primary per table is allowed. You can have other unique or non-unique keys per table.

    The best way to maintain proper relationships is using foreign keys. The requires that the value in one row of a table must match the value in another row of another table.

    You can use joins to lookup across 3 tables. An Inner Join sounds like the best join for what you are needing to do.
     
    jestep, Apr 15, 2009 IP
  4. blacknet

    blacknet Active Member

    Messages:
    709
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    70
    #4
    you need to do some reading on database normalization :p but you're getting there.

    very short answer is.. yes you can easily combine the 2 tables to make a single 3 column table - but only do it if you *always* have all 3 items of information.

    as for the "3 primary keys" - you don't need 3 primary keys - you need 1 unique index over the 3 columns which will ensure data integrity at db level and give you that lookup :) [this can also be accomplished by simply setting the primary key to be all 3 columns]
     
    blacknet, Apr 15, 2009 IP
  5. manly_ninja

    manly_ninja Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can't use more than 1 primary key in 1 table, only one! Why are you use 3 primary keys in 1 table??? :eek:
     
    manly_ninja, Apr 15, 2009 IP
  6. cyno

    cyno Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    +1 ;)

    Concerning many-to-many relationships you might have to create another table.
     
    cyno, Apr 19, 2009 IP