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
513 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

740 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