Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:17+00:00 2026-05-26T02:52:17+00:00

In MySQL, I was advised to store the multiple choice options for Drugs as

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:52:17+00:00Added an answer on May 26, 2026 at 2:52 am

    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?

    Easier, yes.
    But not better.

    1. Your data would not be normalized, wasting lots of space to store the table.
    2. The index on that field would occupy way more space again wasting space and slowing things down.
    3. If you want to query a drop-down list of possible values, that’s trivial with a separate table, hard (read: slow) with just text in a field.
    4. If you just drop text fields in 1 table, it’s hard to ensure misspellings do not get in there, with a separate link table preventing misspellings is easy.

    If adding the 3rd table drug is better, then how would I implement the Foreign Key structure

    ALTER TABLE user_drug ADD FOREIGN KEY fk_drug(drug_id) REFERENCES drug(id);
    

    and how would I normally retrieve the respective values using those Foreign Keys?

    SELECT u.name, d.name as drug
    FROM user u
    INNER JOIN user_drug ud ON (ud.user_id = u.id)
    INNER JOIN drug d ON (d.id = ud.drug_id)
    

    Don’t forget to declare the primary key for table user_drug as

    PRIMARY KEY (user_id, drug_id)
    

    Alternatively
    You can use an enum

    CREATE TABLE example (
      id UNSIGNED INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
      example ENUM('value1','value2','value3'),
      other_fields .....
    

    You don’t get all the benefits of a separate table, but if you just want a few values (e.g. yes/no or male/female/unknown) and you want to make sure it’s limited to only those values it’s a good compromise.
    And much more self documenting and robust than magic constants (1=male, 2=female, 3= unknown,… but what happens if we insert 4?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

mysql> select count(*) from table where relation_title='xxxxxxxxx'; +----------+ | count(*) | +----------+ | 1291958
mysql> ALTER TABLE category ADD CONSTRAINT category_parent_category_id FOREIGN KEY (parent) REFERENCES category(id); ERROR 1005
Hello I have a problem with fetching required rows from MySQL table correctly within
I have just added a null password field to a mysql table using this
After being advised that i MUST validate my form so that no-one could hack
So I've inherited some django. The mySQL table is simple enough where parent is
I have a MySQL table named users that has the columns firstname and surname.
I have the following table in a MYSQL database: Messages MessageId (PK) int(10) -
I'm currently fetching data from a MySQL database using JDBC and executeQuery . One
Mysql: Table_A ------------ id name 1 Name1 2 Name2 Table_B ------------ a_id type value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.