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
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

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…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

732 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