Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2011
    Location
    Indonesia
    Posts
    2

    I need help regarding constraints concept

    When modeling many-to-many relationship in ERD we transform it into a separated table in relational model. That table must contains PK of both entities and foreign keys to that PKs.

    Let's just say I have
    entity1 = id, attributes
    entity2 = id, attributes

    So, the many-to-many relationship would be
    relationship = id1, id2, FK id1=entity.id, FK id2=entity2.id, PK id1,id2

    I don't know whether we need to add PK constraint to that table or not. I suppose we need because a table need a PK. Is my way above the correct way to do many-to-many relationship?

    Or make it like this?
    relationship = id, id1, id2, FK id1=entity.id, FK id2=entity2.id, PK id

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    no, you should not have a separate id for the PK

    do it the first way you said --

    CREATE TABLE relationship
    ( id1 INTEGER NOT NULL
    , id2 INTEGER NOT NULL
    , PRIMARY KEY ( id1, id2 )
    );
    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
  •