Solved

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

Posted on 2013-12-29
8
434 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 75

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

914 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