0

Details

I have a dropdown that allows multiple options to be selected. enter image description here

I want to easily and quickly search the database for these options. To do so, I have opted to create a column in the DB for each option as set out below.

userid  | car |cdplayer|computer| 
*********************************
   785  |  1  |   0    |    1   |

Each of the columns uses TinyInt(1). If the option is selected I store the value 1, else I store 0. The problem I see with this method is that the table can get quite large if there are a lot of options. If I stored the options in a single column, I assume that it would be much harder to search the database for the columns, but then the database would be much smaller (maybe this doesn't matter at all though).

Question

Do you see any problems with the method I am currently employing? Is there a better way of achieving what I am trying to do?

4

2 回答 2

2

这通常被认为是糟糕的数据库设计。这使得添加新选项变得困难,因为您必须更新数据库模式,并更改所有应用程序以了解此新列。

更好的解决方案是带有列的选项表option_idoption_name. user_options然后,您使用带有列的引用表user_id并将option_id它们链接起来。

于 2013-07-26T02:30:06.757 回答
1

更好的结构可能是有一个用户表和一个选项表,并通过 optionid 链接它们。

userid | optionid |
*******************
  12   |    1     |
*******************
  12   |    3     |
*******************
  35   |    2     |

选项表:

optionid |       value        |
*******************************
   1     |        Car         |
*******************************
   2     |        CD          |
*******************************
   3     |        Computer    |
*******************************
于 2013-07-26T02:31:16.637 回答