Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    24

    How do I resolve this situation with many to many

    Hello, database is on a pet shop, heres my format on three tables that are related on the ERD our rule is assigning 3 main colors to 1 pet for identification. It is a many-to-many relationship between pet and colors.

    Pet table
    ---------
    pet_id PK



    Composite table
    -----------
    pet_id FK
    color_id FK


    Color table
    -----------
    color_id PK
    color desc


    Is this right? or should I have in the composite table pet_id FK and 3 FK color_id fields that would have each a color referencing the color table? Appreciate your help.

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    What you've described so far appears AOK to me
    George
    Home | Blog

  3. #3
    Join Date
    Oct 2008
    Posts
    24
    How would I go about coding the composite table?

  4. #4
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    Something along the lines of ...
    Code:
    Create Table PetColor
      (ColumnA  ColumnType Not Null,
       ColumnB  ColumnType Not Null)
    add foreign key relationships to ColumnA and ColumnB to reference your two MANY tables, and your compound primary key.

    ref http://www.1keydata.com/sql/sqlcreate.html
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    CREATE TABLE petcolours
    ( pet_id      INTEGER NOT NULL 
    , colour_id   INTEGER NOT NULL 
    , CONSTRAINT pet_fk
        FOREIGN KEY ( pet_id ) REFERENCES pets ( pet_id )
    , CONSTRAINT colour_fk
        FOREIGN KEY ( colour_id ) REFERENCES colours ( colour_id )
    , PRIMARY KEY ( pet_id, colour_id )
    );
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •