简体   繁体   English

用于多个用户属性的数据库设计

[英]Database design for multiple user attributes

i am building an app with Laravel, and i have users which also have a profile. 我正在使用Laravel构建应用,并且我的用户也有个人资料。 I am unsure as the best way to go about setting up the database for the user attributes for example 我不确定为用户属性设置数据库的最佳方法,例如

user_ud  |   name  |  gender  |  haircolor
   1         josh       1           3

as you can see from this quick example this is what my table for profiles currently looks like, as the vales are filled in from select boxes. 从这个快速示例中可以看到,这是我的配置文件表当前的样子,因为从选择框中填充了值。

my question is. 我的问题是。 is the bes way to then map these vales to a meaningfull value like this. 然后是将这些价位映射到这样有意义的值的bes方法。

 id  |  gender
  1      male
  2      female

 id  | hairColor
  1      blonde
  2      brown
  3      black

and so on creating a new table for each attribute type? 等等,为每种属性类型创建一个新表? or is there a better way of doing this? 还是有更好的方法呢?

我将为这些使用一个ENUM

$table->enum('gender', ['male', 'female']);

Is hair color important as an entity? 头发的颜色重要吗? Do you insert colors by themselves and if nobody has red color, do you still need information about it's existence in the database? 您是否自己插入颜色,如果没有人红色,您是否仍然需要有关数据库中存在颜色的信息?

With this kind of data, it's usually not the case and prone to over-normalization of the database, which results in additional queries and no benefits. 使用这种数据,通常不是这种情况,并且容易对数据库进行过度规范化,从而导致额外的查询,并且没有任何好处。

Storing the color as a string value is perfectly fine in this case. 在这种情况下,将颜色存储为字符串值非常好。 Enum is also ok for the gender. 对于性别,枚举也可以。 That's unless you are Facebook and need dozens of "gender" options for people who feel like their rights are violated if you don't include their own specific sexual perspective. 除非您是Facebook,并且需要数十种“性别”选项供那些认为自己的权利受到侵犯的人使用,如果您不包括他们自己的特定性观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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