In MySQL, I was advised to store the multiple choice options for "Drugs" as a separate table user_drug
where each row is one of the options selected by a particular user. I was also advised to create a 3rd table drug
that describes each option selected in table user_drug
. Here is an example:
user
id name income
1 Foo 10000
2 Bar 20000
3 Baz 30000
drug
id name
1 Marijuana
2 Cocaine
3 Heroin
user_drug
user_id drug_id
1 1
1 2
2 1
2 3
3 3
As you can see, table user_drug
can contain the multiple drugs selected by a particular user, and table drug
tells you what drug each drug_id
is referring to.
I was told a Foreign Key should tie tables user_drug
and drug
together, but I've never dealt with Foreign Key's so I'm not sure how to do that.
Wouldn't it be easier to get rid of the drug table and simply store the TEXT value of each drug in user_drug? Why or why not?
If adding the 3rd table drug
is better, then how would I implement the Foreign Key structure, and how would I normally retrieve the respective values using those Foreign Keys?
(I find it far easier to use just 2 tables, but I've heard Foreign Keys are helpful in that they ensure a proper value is entered, and that it is also a lot faster to search and sort for a drug_id
than a text value, so I want to be sure.)