?
Solved

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

Posted on 2013-12-29
8
Medium Priority
?
444 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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