combined primary key like this

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    combined primary key like this

    Hii guys,
    I have a small doubt . Can i create a primary key for a table like in the following example
    Code:
    PRIMARY KEY (OrderID, ModelID)
    where in the OrderID and ModelID are foreign Keys referenced from 2 different tables.

    Code:
    CREATE TABLE myTable
        (
            OrderID     SMALLINT UNSIGNED NOT NULL,
            ModelID     SMALLINT UNSIGNED NOT NULL,
            Description VARCHAR(40),
          FOREIGN KEY(OrderID) REFERENCES order(OrderID),
          FOREIGN KEY(ModelID) REFERENCES model(ModelID)
           PRIMARY KEY (OrderID, ModelID)
         );
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Sure. In fact, in standard N:M intermediary tables, that is usually what is done.

    Just out of curiosity, though, why didn't you just try it and see for yourself? I mean, since you already wrote the command... Just seems it would have taken less time to execute the command to see if it worked, then it took to post it here xD

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      I wanted to know if i was doing according to rules . I had executed it . :)

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ahh OK, I see :)

        Assuming you conform with the first three normalization rules, the aren't any major design "rules" you need to worry to much about.

        There are, of course, various minor things most people try to keep in mind - like not to using string data as PKs - but those are always up for interpretation. Usually nothing that can't be, or even needs to be, fixed.

        We are always happy to offer our advice, though ;-)

        Comment

        Working...