简体   繁体   English

SQL 非常小的“多”表的一对多关系

[英]SQL one-to-many relationship for very small 'many' table

I have a 'store_location' table which has a one to many relationship with closing and opening times, which is just to keep track of service hours, and which weekdays those hours are active.我有一个“store_location”表,它与关闭和打开时间有一对多的关系,这只是为了跟踪服务时间,以及这些时间在哪些工作日是活跃的。 So a 'weekdays' column, and 'opening hour', and 'closing hour' if I were to create a new table for it.因此,如果我要为其创建一个新表,则需要一个“工作日”列、“营业时间”和“关闭时间”。

But it feels a little unnecessary to have to create a new table just for this.但是感觉有点没必要为此创建一个新表。 Coming from a NoSQL background, I feel tempted to just create a column "service_times" which is an array of arrays, or array of objects.来自 NoSQL 背景,我很想创建一个列“service_times”,它是 arrays 或对象数组的数组。 And what if the 'many' table is even simpler than that, eg I'd also like to do the same with marking public holidays, so i'd have another table just to keep track of closing/opening dates... a 2 column table.如果“多”表比这更简单,例如,我也想在标记公共假期时做同样的事情,所以我有另一个表只是为了跟踪关闭/打开日期...... a 2列表。

What is best practice here?这里的最佳做法是什么?

The best way to store the information depends on how it is going to be used.存储信息的最佳方式取决于将如何使用它。 And scheduling data can be quite hard to represent well.调度数据可能很难很好地表示。

If the store hours are simply going to be selected for display, then it doesn't matter how you store them.如果只是选择商店营业时间进行显示,那么您如何存储它们并不重要。 An array, blob, normalized format or whatever is fine.一个数组、blob、规范化格式或任何可以的东西。

More likely, the store hours are going to be used for filtering.更有可能的是,商店营业时间将用于过滤。 This then becomes troublesome.这会变得很麻烦。

In the simplest case, there is a direct mapping between days of the week and hours and the service hours never include midnight.在最简单的情况下,星期和小时之间存在直接映射,并且服务时间从不包括午夜。 In that case, you can have columns, arrays, or a separate table with a start and end time per day.在这种情况下,您可以有列、arrays,或具有每天开始和结束时间的单独表。

But then it gets harder if you want to include midnight and have separate hours for holidays.但是,如果您想包括午夜并有单独的假期时间,那就更难了。 Given the complexity.鉴于复杂性。 I would probably suggest having a location-calendar table with rows per location and date.我可能会建议有一个位置日历表,每个位置和日期都有行。 If something is open after midnight, then the table would have two rows in the table (say 00:00 - 02:00 and then 17:00-24:00).如果某些东西在午夜之后打开,那么表格中将有两行(例如 00:00 - 02:00,然后是 17:00-24:00)。 You would then prepopulate this table for years into the future.然后,您将在未来几年内预填充此表。 And it can take holidays into account.它可以考虑假期。

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

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