简体   繁体   中英

How to save multiple options in sql database?

I want to save the user preference for external login options in SQL Database. Users can select multiple options and custom login is default. The options are fixed as follows:

  1. Custom Login
  2. Facebook
  3. Google
  4. Twitter
  5. LinkedId
    .. etc

Currently I have a Preference table in which I am saving other preferences. So my question is how can I save these options in Preference table.

Means:

  1. Should I save this in single column, how?

  2. Should I have to create a new table ( LoginOptions ) and linked it with Preference table in many to one relationship.

  3. Or any other better option?

You are proposing a many-to-many relationship between users and their login options. Your best option is to have a LoginOption table and create a many-to-many relationship between that and Users (or Preference ) table.

Option 1 is essentially creating a many-to-many relationship with a single field. It's more effort, in the long run, with less pay back compared what I have outlined above.

Alternatives are to:

  • have a Boolean field on the user table for each external login options.
  • have a single Preference record for each login option.

Again you are will need to put effort into these "short cuts", but won't reap the same benefits are a proper many-to-many table relationship.

  1. If you wish to store it in a single column, you'll have to encode your configuration into a single value. You could use binary (custom is 1, Facebook is 2, google is 4 and your perform an OR on the values), or encode it in a string. This is highly unrecommended, you lose all kinds of DB advantages by doing it and there's a performance penalty. This is also not scalable, as it's VERY hard to add additional information for each login preference.
  2. You can create another table, where each user could have multiple rows - each representing a login preference. This is easy to query and update and if you'll like to add more information to each preference - you simply add more columns.
  3. I agree with Adrian, creating a column for each of the login preferences (no matter in which table) is not a good idea. This will force you to change your db schema change just to support another login method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM