简体   繁体   English

双向关系是否适合参考表?

[英]Is a bidirectional relationship appropriate for reference tables?

Please excuse me if this is a stupid question, but I'm new at relational databases and can't seem to find the answer (probably because everyone else already knows the answer). 如果这是一个愚蠢的问题,请原谅我,但我是关系数据库的新手,似乎无法找到答案(可能是因为其他人都知道答案)。

I'm storing test data that may include times in various formats that are slightly different (TAI, UTC, GPS, etc). 我正在存储测试数据,这些测试数据可能包含各种格式稍有不同的时间(TAI,UTC,GPS等)。 I'm not sure I want to force the times to be converted to an arbitrary consistent format before being input (or maybe that's the best way?), but assuming I can keep the flexibility, I plan to use a reference table (lookup table) of time types like so: 我不确定我想在输入之前强制将时间转换为任意一致的格式(或者这可能是最好的方式?),但假设我可以保持灵活性,我打算使用引用表(查找表) )时间类型如下:

ref_time_types
--------------
id (PK)
desc (UTC, GPS, TAI, etc)

and I'll also have a table to store the actual times: 我还会有一个表来存储实际时间:

tbl_time
-------------
id (PK)
ref_time_types.id (FK)
seconds
year
.
.
.

My question is I'm sure very basic. 我的问题是我非常基本。 Should I establish a bidirectional relationship between these, or a unidirectional many-to-one from tbl_time to ref_time_types? 我应该在这些之间建立双向关系,还是从tbl_time到ref_time_types的单向多对一关系? I can't think of any reason I would want to find all the UTC times, for example. 例如,我想不出任何我希望找到所有UTC时间的原因。 Is that the guiding principle on whether to create a bidirectional relationship? 这是否是建立双向关系的指导原则? Is there any best practice when it comes to relationships for reference tables? 在参考表的关系方面有没有最佳实践?

I'm using python, sqlalchemy, and sqlite if that makes any difference. 我使用的是python,sqlalchemy和sqlite,如果这有任何区别的话。

Do you actually need to preserve the format? 你真的需要保留格式吗?

  1. If no, then don't store it in the database. 如果不是,则不要将其存储在数据库中。 Database is for storing data, it shouldn't care about how the data is represented in the UI. 数据库用于存储数据,它不应该关心如何在UI中表示数据。 So, I'd recommend you handle the input however you want in the UI (if that means multiple formats, so be it), but convert it to consistent format before storing it to the database and get rid of the ref_time_types altogether. 所以,我建议你在UI中处理你想要的输入(如果这意味着多种格式,就这样吧),但是在将它存储到数据库之前将其转换为一致的格式并完全摆脱ref_time_types
  2. If yes, then go ahead and store the format as well, but I'd still use the consistent representation of the date/time itself since it would require less conversions when querying (you just need to convert the input criteria for the query, instead of all the rows that don't match the desired format). 如果是,那么继续存储格式,但我仍然使用日期/时间本身的一致表示,因为它在查询时需要较少的转换(您只需要转换查询的输入条件,而不是所有与所需格式不匹配的行)

Also, do use date/time type supported by your DBMS - I don't see a particular reason to split various date/time components to separate fields unless you actually want to query on these individual components (and want to index them). 另外,请使用DBMS支持的日期/时间类型 - 我没有看到将各种日期/时间组件拆分为单独字段的特定原因, 除非您确实要查询这些单独的组件(并希望对它们编制索引)。

Should I establish a bidirectional relationship between these, or a unidirectional many-to-one from tbl_time to ref_time_types? 我应该在这些之间建立双向关系,还是从tbl_time到ref_time_types的单向多对一关系?

As discussed above, you won't need any relationship in the first case (since there is only one table). 如上所述,在第一种情况下您不需要任何关系(因为只有一个表)。 A relationship would be many-to-1 the second case. 在第二种情况下,关系将是多对一。

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

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