营业时间数据库设计

编程入门 行业动态 更新时间:2024-10-26 02:24:55
本文介绍了营业时间数据库设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们当前正在开发一个应用程序,其中多个实体具有关联的营业时间。开放时间可能跨越数天,也可能包含在一天之内。

We are currently developing an application in which multiple entities have associated opening hours. Opening hours may span multiple days, or may be contained within a single day.

例如。

或者

周一开放于06: :00,周一关闭至15:00。

Opens Monday at 06:00 and closes Monday at 15:00.

此外,一个实体每天可能有多组开放时间。 到目前为止,我发现的最佳设计是将开放时间定义为以下内容:

Also, an entity may have multiple sets of opening hours per day. So far, the best design I have found, is to define an opening hour to consist of the following:

StartDay,StartTime,EndDay和EndTime。

StartDay, StartTime, EndDay and EndTime.

此设计可提供所有所需的灵活性。但是,数据完整性成为一个问题。我似乎找不到解决方案,不允许跨度重叠(在数据库中)。

This design allows for all the needed flexibility. However, data integrity becomes an issue. I cannot seem to find a solution that will disallow overlapping spans (in the database).

请分享您的想法。

编辑:数据库是Microsoft SQL Server 2008 R2

The database is Microsoft SQL Server 2008 R2

推荐答案

假定一个可靠的触发框架

Presuming a robust trigger framework

在插入/更新时,您将检查新的开始或结束日期是否在任何现有范围内。如果这样做了,那么您将回滚更改。

On insert/update you would check if the new start or end date falls inside of any existing range. If it does then you would roll back the change.

CREATE TRIGGER [dbo].[mytable_iutrig] on [mytable] FOR INSERT, UPDATE AS IF (SELECT COUNT(*) FROM inserted, mytable WHERE (inserted.startdate < mytable.enddate AND inserted.startdate > mytable.startdate) OR (inserted.enddate < mytable.enddate AND inserted.enddate > mytable.startdate)) > 0 BEGIN RAISERROR --error number ROLLBACK TRANSACTION END

更多推荐

营业时间数据库设计

本文发布于:2023-10-23 01:43:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:营业时间   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!