[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

One-to-Many or Many-to-Many MySQL relationship

Posted on 2013-12-29
8
Medium Priority
?
446 Views
Last Modified: 2013-12-30
Hello Experts,

I have a question (I am probably over-thinking this, too). I am creating a PHP database driven quiz application. I have four tables: users, tests, questions, and answers. I am a little confused on how to relate the users table to the tests table, and here is why...

Is it a one-to-many (one user can take multiple tests) relationship or is it a many-to-many (many users can take multiple tests and many tests can have multiple users) relationship?

I have the tests related to their questions via the test_id and the questions related to their answers with the question_id. However, how do I structure my users table correctly to relate the tests to the users and the users to the tests? Again, I am most likely over-thinking this and making it WAY more complex than it is supposed to be, but that's why I'm here, right?
0
Comment
Question by:extremedesign413
[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
8 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 39745584
It sounds like you don't have set tests--that is, there is no Test A is one set of questions, Test B is a second set of question, etc. If you had something like that, then you would have a many-to-many relationship. What it sounds like you have is a pool of questions that get randomly selected and grouped together as a test. It could happen that two tests end up having the same exact questions, but it is not guaranteed (since it's random). This would be a one (user) to many (tests) relationship.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 1200 total points
ID: 39745585
AFAIK, the users and tests table should be a many to many relationship as a single user can take the test multiple times(Failing and retaking the test again) and several users can take the same test.
And hence I would request you to create an associative table which joins the userid from users table and testid from tests table so that it would be properly normalized.
kindly let me know if you think otherwise.
0
 

Assisted Solution

by:shinuster
shinuster earned 400 total points
ID: 39745612
According to Raja, it is right suggestion to have many to many relationship whereas you should have unique ID in order to avoid selecting a bulk of users at a time.
0
Independent Software Vendors: 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!

 

Author Comment

by:extremedesign413
ID: 39745618
Okay, I think I have it figured out. Correct me if I am wrong, but the primary key from the users table will become a foreign key in the tests table and the primary key from the tests table will become a foreign key in the users table, right?
0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 1200 total points
ID: 39745649
Small correction, I recommended to create a new Table to hold the testId and UserId combination named Map_tests_users. And primary key from the users table will become a foreign key in the Map_tests_users table and the primary key from the tests table will become a foreign key in the Map_tests_users table.
0
 

Author Comment

by:extremedesign413
ID: 39745654
Okay Raja, thanks! Funny you mentioned that! I was actually signing on to ask that very question...So, then I don't need to worry about having a foreign key in the tests table and vice versa, right? This is because the primary keys from the users table and tests table will become foreign keys in the new table, right?
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 1200 total points
ID: 39745667
Yes, that's correct..
0
 

Author Closing Comment

by:extremedesign413
ID: 39745697
Thank you everyone, who replied to my post. I really appreciate it a lot. Please check out my other question I posted, in regards to installing the 'designer' mode in MAMP. I hope everyone is satisfied with the points I awarded you.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

650 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