简体   繁体   English

数据建模:具有亲子关系的种族?

[英]Data Modeling: ethnicities with parent-child relationship?

I have a site with users that I want users to be able to identify their ethnicities. 我有一个用户站点,希望用户能够识别他们的种族。 What's the best way to model this if there is only 1 level of hierarchy? 如果只有1个层次结构,最好的建模方法是什么?

Solution 1 (single table): 解决方案1(单表):

Ethnicity
- Id
- Parent Id
- Name

Solution 2 (two tables): 解决方案2(两个表):

Ethnicity Group
- Id
- Name

Ethnicity
- Id
- Ethnicity Group Id
- Name

I will be using this so that users can search for other users based on ethnicity. 我将使用它,以便用户可以根据种族搜索其他用户。 Which of the 2 approaches will work better for me? 这两种方法中的哪一种对我更有效? Is there another approach I have not considered? 我还有没有考虑过的另一种方法? I'm using MySQL. 我正在使用MySQL。

Well there is such a thing as an Ethnicity Group in the real world, so you do need two tables, not one. 好吧,在现实世界中有一个“族裔群体”之类的东西,因此您确实需要两个表,而不是一个。 The real world has three levels (the top-most would be Race), but I understand that may not be necessary here. 现实世界具有三个级别(最高的级别是“种族”),但我知道这里可能没有必要。 If you squash the three levels into two, you have to be careful, and lay them all out properly at the beginning. 如果将三个级别压缩成两个级别,则必须小心,并在开始时将它们全部正确布置。 However, they will be vulnerable to people saying they want the real thing, and you may have to change it, or change the structure to fit more in ... much more work later). 但是,他们会很容易受到人们说他们想要真实事物的攻击,您可能必须更改它,或更改结构以使其更适合……以后需要更多工作。

If you do it correctly, as per real world, that problem is eliminated. 如果按照现实世界正确执行此操作,则可以消除该问题。 Let me know if you want Race, and I will change the model. 让我知道您是否要参加比赛,我将更改其型号。

The tables are far too small, and the keys are too meaningful, to add Id-iot columns to them; 这些表太小了,键也太有意义了,以至于无法向它们添加Id-iot列。 leave them as pure Relational keys, otherwise you will lose the power of the Relational engine. 将它们保留为纯关系密钥,否则您将失去关系引擎的功能。 If you really want narrow keys, use a CHAR(2) EthnicityCode, rather than a NUMERIC(10,0) or a meaningless number. 如果您确实想要窄键,请使用CHAR(2)种族代码,而不要使用NUMERIC(10,0)或无意义的数字。

Link to Ethnicity Data Model (plus the answer to your other question) 链接到种族数据模型 (以及您其他问题的答案)

Link to IDEF1X Notation for those who are unfamiliar with the Relational Modelling Standard. 对于不熟悉关系建模标准的人,请链接到IDEF1X符号

If there is nothing like an "ethnicity group" in the real world, I'd suggest you don't introduce one in your data model. 如果在现实世界中没有像“民族团体”这样的人,我建议您不要在数据模型中引入一个人。

All the queries you can do with the second one you can also do with the first one, because you can just select FROM ethnicity AS e1 JOIN ethnicity AS es ON (e2.ethnicity_id = e1.parent_id) . 您可以对第二个查询进行的所有查询也可以对第一个查询进行查询,因为您可以选择FROM ethnicity AS e1 JOIN ethnicity AS es ON (e2.ethnicity_id = e1.parent_id)

I don't want to be awkward, but what are you going to do with people of mixed descent? 我不想尴尬,但是您将如何处理混血儿? I think that the best that you can hope for is a simple single-level enumeration like the kind of thing you get on census forms (eg 'Black', 'White', 'Asian', 'Hispanic' etc). 我认为,您所希望的最好的办法是进行简单的单级枚举,例如以普查形式获得的东西(例如“黑人”,“白人”,“亚洲人”,“西班牙裔”等)。 It's not ideal, but it allows people to fairly easily self-identify. 这不是理想的方法,但是它使人们可以轻松地自我识别。 Concepts like race and ethnicity are wooly enough without trying to create additional (largely meaningless) hierarchies on top of them, so my gut feeling is to keep it simple. 种族和种族之类的概念已经足够粗略,而无需尝试在它们之上创建其他(基本上没有意义的)层次结构,因此,我的直觉是保持简单。

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

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