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
483 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now