I asked this question at http://dba.stackexchange.com, but it was closed with a note that I should ask it here as it relates to design and not administration.
I'm building a site ("The Site") and I'm considering how to design the DB schema for it.
I have a few scenarios that will affect the outcome:
How should I design the schema of the DB ?
For the first two scenarios, I thought of having a table called Users
that has an id
, and a type
fields. The type reference a different table, such as FacebookUsers
or GoogleUsers
, each has a user_id
field - which has a foreign relation to the Users.id
field - and other fields such as the first_name
, profile_picture
, etc..
But this approach doesn't support the third scenario. So I thought about the following:
Having a base table called Users
and table for each external site I want to allow the user to connect with my site (which is the site).
The Users
table will hold basic information about the user (such as first name, last name, username, password, email
- all optional) with a column named accounts
that will hold values such as native|facebook|twitter
, or a serialized array of these values.
The external sites tables (e.g. FlickrUsers
) will hold the relevant data for that external site relevant to the user, and a reference to the Users
table' user_id
field.
When a user connects his account with an external site (such as flickr, instagram), I populate that external site table (e.g. FlickrUsers
) with the relevant data for that external site, and add the name of this external site as a another value to the accounts
field in the Users
table.
I am asking this as a general best-practice question, for a site that uses a multi-account login.
Having that said, I'm not sure if the above are good approaches/designs for the site I described above. I'm fully aware that there isn't just one correct answer, but I know there are better solutions and not-so-ideal solutions. I'm hoping to get to the better solutions.
For the sake of this discussion, my RDBMS is mysql and my language is php.