Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Two same foreign keys in the one table . is it possible

Two same foreign keys in the one table . is it possible

Scheduled Pinned Locked Moved Database
question
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yousefshokati
    wrote on last edited by
    #1

    Hi Is it possible to have 2 foreign keys that ae thesame in one table ?

    Create table Product
    (
    productID int PK
    )
    Create table Category
    (
    CategoryID int PK ,
    productID1 int fk references Product (productID) ,
    productID2 int fk references Product (productID)
    )

    M P S L 4 Replies Last reply
    0
    • Y yousefshokati

      Hi Is it possible to have 2 foreign keys that ae thesame in one table ?

      Create table Product
      (
      productID int PK
      )
      Create table Category
      (
      CategoryID int PK ,
      productID1 int fk references Product (productID) ,
      productID2 int fk references Product (productID)
      )

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Yes, although your table name is misleading and it looks like a design nasty to me. Do you ALWAYS have EXACTLY 2 products in the Category (name seems wrong) table.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • Y yousefshokati

        Hi Is it possible to have 2 foreign keys that ae thesame in one table ?

        Create table Product
        (
        productID int PK
        )
        Create table Category
        (
        CategoryID int PK ,
        productID1 int fk references Product (productID) ,
        productID2 int fk references Product (productID)
        )

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Yeah, what he said. Your design may be more maintainable if you use a separate table to hold the relationships. Here's a recent thread concerning a similar situation: http://www.codeproject.com/Forums/1649/Csharp.aspx?fid=1649&tid=4132317[^]

        1 Reply Last reply
        0
        • Y yousefshokati

          Hi Is it possible to have 2 foreign keys that ae thesame in one table ?

          Create table Product
          (
          productID int PK
          )
          Create table Category
          (
          CategoryID int PK ,
          productID1 int fk references Product (productID) ,
          productID2 int fk references Product (productID)
          )

          S Offline
          S Offline
          SilimSayo
          wrote on last edited by
          #4

          What you are asking is possible; however, be sure that your design is right. Off the top of my head, I thing a Category can have 1 or several products. If that is the case, then I would have my tables as follows Product(ProductID, CategoryID, ProductDesc,...) Category(CategoryID, CategoryDesc,......) Category ID would be a foregn in the product table

          L 1 Reply Last reply
          0
          • S SilimSayo

            What you are asking is possible; however, be sure that your design is right. Off the top of my head, I thing a Category can have 1 or several products. If that is the case, then I would have my tables as follows Product(ProductID, CategoryID, ProductDesc,...) Category(CategoryID, CategoryDesc,......) Category ID would be a foregn in the product table

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            What if the product can belong to more than one category? (as is true in a real world situation).

            S 1 Reply Last reply
            0
            • Y yousefshokati

              Hi Is it possible to have 2 foreign keys that ae thesame in one table ?

              Create table Product
              (
              productID int PK
              )
              Create table Category
              (
              CategoryID int PK ,
              productID1 int fk references Product (productID) ,
              productID2 int fk references Product (productID)
              )

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              As has already been answered above, yes, it is possible. But your design is flawed. It accomodates only 2 products in a category. What if a category has more than two products? In a real world situation, a category can have hundreds of products and a product can be classified under multiple categories. You should create what is called a bridge table to address this design question.

              Create table Product
              (
              ProductID int PK
              )
              Create table Category
              (
              CategoryID int PK
              )
              Create table CategoryProductBridge
              (
              CategoryID int FK references Category (CategoryID),
              ProductID int FK references Product (ProductID)
              )

              S 1 Reply Last reply
              0
              • L Lost User

                What if the product can belong to more than one category? (as is true in a real world situation).

                S Offline
                S Offline
                SilimSayo
                wrote on last edited by
                #7

                Based on the question, it would be impossible to tell.As you can see my answer was conditional. Of course if there were many products for category and vice-versa, then a many-to-many relationship would exist which would be decomposed to two one-to-many relationships. The idea was to get the author of the question to think about their design carefully. I was not saying the solution must necessarily be what wrote. :((

                1 Reply Last reply
                0
                • L Lost User

                  As has already been answered above, yes, it is possible. But your design is flawed. It accomodates only 2 products in a category. What if a category has more than two products? In a real world situation, a category can have hundreds of products and a product can be classified under multiple categories. You should create what is called a bridge table to address this design question.

                  Create table Product
                  (
                  ProductID int PK
                  )
                  Create table Category
                  (
                  CategoryID int PK
                  )
                  Create table CategoryProductBridge
                  (
                  CategoryID int FK references Category (CategoryID),
                  ProductID int FK references Product (ProductID)
                  )

                  S Offline
                  S Offline
                  SilimSayo
                  wrote on last edited by
                  #8

                  You're rather presumptuous. Although there are situations in which products and category have a many to many relationship, there are many real world situations in which a product belows to one category.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups