[英]Should you create separate tables for fixed options?
As shown below, in multiple places in my DB Design I am creating tables for "options" if you will. 如下所示,在我的数据库设计中的多个位置,如果您愿意,我正在为“选项”创建表。 Example, RFP Stages.
例如,RFP阶段。 Which would hold things like "Complete", "Bidding", etc.
哪个会包含“完成”,“竞标”等内容。
I'm doing this because it was recommended to me at one point in a question that was deleted. 我这样做是因为在一个被删除的问题中我被推荐给了我。 Is this the proper way to do this?
这是正确的方法吗? Or should I just store these options as text, given there are only 5 or so possibilities?
或者我应该将这些选项存储为文本,因为只有5种左右的可能性?
If your list of possible values such as rfp_stage
involves more than just a name, then you definitely want a look-up table added to your design. 如果您的可能值列表(例如
rfp_stage
涉及名称,那么您肯定需要在设计中添加一个查找表。 For example, to track a color used for "Complete" and another color for “Bidding”, then you need a second field color
along with name
on the rfp_stage
look-up table. 例如,要跟踪用于“完成”的颜色和“出价”的另一种颜色,则需要第二个字段
color
以及rfp_stage
查找表上的name
。
If the name of each RFP stage is the only value, no such additional items such as color discussed above, then you may want to use a string column without any lookup table. 如果每个RFP阶段的名称是唯一的值,没有上面讨论的颜色等附加项,那么您可能希望使用不带任何查找表的字符串列。
You could enforce a list of possible values in your app(s). 您可以在应用程序中强制执行可能值的列表。
As a backup to the app's enforcement, if you are using a powerful database system such as Postgres , you can define a domain of possible values and require the field to always have a value in that domain. 作为应用程序执行的备份,如果您使用强大的数据库系统(如Postgres) ,则可以定义可能值的域 ,并要求该字段始终在该域中具有值。 Trying to add or update a row with an unexpected value will fail, with an error thrown by the database engine.
尝试添加或更新具有意外值的行将失败,并且数据库引擎会抛出错误。
Then again, if there is any possibility of the names of any stage changing, such as “Bidding” changing to “Out for bid” (same meaning, different wording), then a look-up table is useful as a single place to update the wording. 然后,如果任何阶段的名称有可能发生变化,例如“出价”更改为“出价”(相同的含义,不同的措辞),则查找表可用作更新的单个位置措辞。 No need to perform a mass update of values across many rows.
无需跨多行执行大量的值更新。
Similarly, if you need to localize the display of each RFP stage, say to show “Complete” and “Bidding” in French or Japanese, then you need to add the look-up table. 同样,如果您需要本地化每个RFP阶段的显示,例如以法语或日语显示“完成”和“出价”,则需要添加查找表。 You'll likely have yet another look-up table to hold localization strings, but that's outside the scope of this Answer.
您可能还有另一个查找表来保存本地化字符串,但这超出了本答案的范围。 Search Stack Overflow and perhaps DBA Stack Exchange for more info.
搜索Stack Overflow以及DBA Stack Exchange以获取更多信息。
Lastly, some people might use an enum in the database as a stand-in value to represent each possible value. 最后,有些人可能会使用数据库中的枚举作为替代值来表示每个可能的值。 For example, “1” for “Complete”, “2” for “Bidding”, and so on.
例如,“完成”为“1”,“出价”为“2”,依此类推。 I generally avoid this approach myself as it makes reading the data from the rows awkward and inconvenient.
我自己通常会避免使用这种方法,因为它会使行中的数据读取不方便且不方便。
As with many design decisions, there is no clear-cut golden rule. 与许多设计决策一样,没有明确的黄金法则。 Context matters.
环境很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.