关系型数据库设计(MySQL)
我有一个表 User 来存储用户信息 - 例如姓名、出生日期、位置等。
我还创建了一个名为 User_Options 的链接表 - 目的是存储多值属性 - 这基本上存储了复选框选择。
我有一个前端表单供用户填写并创建他们的用户配置文件。以下是我创建的用于生成复选框选项的表格:
Table User_Attributes ===================== id attribute_name --------------------- 1 Hobbies 2 Music
Table User_Attribute_Options ====================================== id user_attribute_id option_name -------------------------------------- 1 1 Reading 2 1 Sports 3 1 Travelling 4 2 Rock 5 2 Pop 6 2 Dance
因此,在前端表单上有两组复选框选项 - 一组用于爱好,一组用于音乐。
这里是用户表:(
Table User ======================== id name age ------------------------ 1 John 25 2 Mark 32
Table User_Options ================================================== id user_id user_attribute_id value -------------------------------------------------- 1 1 1 1 2 1 1 2 3 1 2 4 4 1 2 5 5 2 1 2 6 2 2 4
上表中'user_attribute_id'是父属性的ID,'value'是属性选项的ID)。
所以我不确定我是否正确或有效地完成了这一切。我知道有一种在同一个表中存储分层数据的方法,但我更喜欢将数据分开。
我主要关心的是 User_Options 表 - 其背后的想法是只需要一个链接表来存储多值属性,而不是为每个多值属性都有一个表。
I have a table User that stores user information - such as name, date of birth, locations, etc.
I have also created a link table called User_Options - for the purpose of storing multi-value attributes - this basically stores the checkbox selections.
I have a front-end form for the user to fill in and create their user profile. Here are the tables I have created to generate the checkbox options:
Table User_Attributes ===================== id attribute_name --------------------- 1 Hobbies 2 Music
Table User_Attribute_Options ====================================== id user_attribute_id option_name -------------------------------------- 1 1 Reading 2 1 Sports 3 1 Travelling 4 2 Rock 5 2 Pop 6 2 Dance
So, on the front-end form there are two sets of checkbox options - one set for Hobbies and one set for Music.
And here are the User tables:
Table User ======================== id name age ------------------------ 1 John 25 2 Mark 32
Table User_Options ================================================== id user_id user_attribute_id value -------------------------------------------------- 1 1 1 1 2 1 1 2 3 1 2 4 4 1 2 5 5 2 1 2 6 2 2 4
(in the above table 'user_attribute_id' is the ID of the parent attribute and 'value' is the ID of the attribute option).
So I'm not sure that I've done all this correctly, or efficiently. I know there is a method of storing hierarchical data in the same table but I prefer to keep things separate.
My main concern is with the User_Options table - the idea behind this is that there only needs to be one link table that stores multi-value attributes, rather than have a table for each and every multi-value attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能看到的唯一要更改的是,在关联表 User_Options 中,您有一个似乎没有任何用途的 id。该表的主键将是所有三列,我认为您不会通过 id 引用用户拥有的选项 - 您将通过 user_id/user_attribute_id 获取它们。例如,给我所有用户选项,其中 user 为 1,用户属性 id 为 2。使用附加字段唯一键控这些记录似乎是无关紧要的。
我认为否则表格的总体形状及其关系对我来说是正确的。
The only thing I can see that I'd change is that in the association table, User_Options, you have an id that doesn't seem to serve a purpose. The primary key for that table would be all three columns, and I don't think you'd be referring to the options a user has by an id--you'd be getting them by user_id/user_attribute_id. For example, give me all the user options where user is 1 and user attribute id is 2. Having those records uniquely keyed with an additional field seems extraneous.
I think otherwise the general shape of the tables and their relationships looks right to me.
你的做法并没有什么问题。
以更多链接表引用(以及查询的组成)为代价,可以使事物更具可扩展性。也可以使事情变得更扁平,并且可扩展性和灵活性降低,但您的查询会更快。
但是,正如通常的情况一样,有不止一种方法可以做到这一点。
There's nothing wrong with how you've done it.
It's possible to make things more extensible at the price of more linked table references (and in the composition of your queries). It's also possible to make things flatter, and less extensible and flexible, but your queries will be faster.
But, as is usually the case, there's more than one way to do it.