Solved

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

Posted on 2013-12-29
8
440 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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…

820 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