Solved

can there be two foriegn keys in a table - both the foriegn keys reference a primary key in other table

Posted on 2011-09-12
6
503 Views
Last Modified: 2012-05-12
can there be two foriegn keys in a table - both the foriegn keys reference a primary key in other table. (The table is going to be built on MYSQL backend - connected to MS access front end)



I have a group of items that I need to mark different categories. Some items belong to both the categories.
Hence I am trying to build a table like
Example:
table name - Item-categories
Itemid, categoryid1, categoryid2
1           1                       2
2           2                  
3           1                      2

(Categroyid and categoryid2 are the foriegn keys and represent the catgegoryID in the category table).

If the above is not legitimate design, how to acheive the best.
0
Comment
Question by:Jsara
6 Comments
 
LVL 57
ID: 36523420
 
  While you can, it's not a great idea. You'll have all kinds of problems when it comes to queries against the tables.

  Instead, create another table, which for each Item will have one record for each category that the item belongs to.

Jim.
0
 
LVL 18

Expert Comment

by:lludden
ID: 36523441
You shouldn't have columns that repeat the same data.  The proper way to have this (called a Many to many join) is with three tables

Table1 - ItemList
Itemid, ItemName, etc

Table 2 - Categories
CategoryID, CategoryName

Table3 - Join table
ItemID, CategoryID

Table 3 has a primary key of ItemID, CategoryID, and one entry for each pair
0
 

Author Comment

by:Jsara
ID: 36523607
Definitely, sorry I was not clear.
What I wrote was to have a 3rd table.
I have items, categories and a 3rd table called item_categories.

So are you suggesting in table - it should be just 2 columns of itemid and categoryid only
and not 3 columns with Item id and 2 other category id columns.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 57
ID: 36523795
<<So are you suggesting in table - it should be just 2 columns of itemid and categoryid only>>

 Exactly.  An item in multiple categories is represented with multiple records rather then multiple fields in a single record.

  This makes it simple to answer questions such as:

How many categories is an item in?
How many items have category xyz?

  If the data were in multiple fields, you'd have to visit each item record and look in every category field to answer questions such as those.

Jim.
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 36523805

 and what happens when someone says "We've got a new item and it belongs in three categories."

  WIth the data in fields, your design would need to change.  With each category expressed as records, the user just needs to add another record and your design does not change at all.

Jim.
0
 
LVL 22

Expert Comment

by:dportas
ID: 36536145
It is perfectly reasonable and correct for a table to have multiple foreign keys referencing another table.

In your specific case however I suggest you change the design along the lines that JDettman suggested. It appears to make more sense to allow any number of categories per item and not restrict yourself to two.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question