Solved

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

Posted on 2013-12-29
8
433 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
8 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 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 300 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 100 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 300 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 300 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
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.

758 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

17 Experts available now in Live!

Get 1:1 Help Now