I'm new to setting up relational databases.
I'm trying to create two tables in MySQL, a USER table and a COMPANY table. Both should be able to have multiple phone numbers associated with them, so I created a PHONE table and wanted to have a one-to-many relationship from both COMPANY to PHONE, and from USER to PHONE.
There only seem to two options in what I am trying to do:
Keep two foreign keys in PHONE, one referencing COMPANY, and one referencing USER. They would both default to NULL and each and when creating a new row in PHONE, I would only fill the one that I need.
Have two different tables, USER_PHONE and COMPANY_PHONE.
Neither option seems optimal to me. Option 1 seems hacky and prone to redundancy issues. Option two seems pretty repetitive and unnecessary. I'm inclined to think that option 2 is the "official" way of doing things (starting to wonder if this is why I hear negative things about MySQL).
Anyone? Thanks,
-Matt
I do like to suggest the following design(much like yours):
First of all, we will have three tables
Then have two tables for storing the relation
Having two different tables for phone numbers raises some problems.
Instead, use a supertype/subtype schema. (Linked answer includes example SQL to implement the schema.) This kind of schema recognizes that users and companies aren't exactly alike, but they're not utterly different.