简体   繁体   English

确保存储在 MS Access 链接/关系表中的关系不允许在时间上重叠

[英]Ensuring relationships stored in an MS Access linking/relationship table are not permitted to overlap with respect to time

I have an MS Access 2016 database with Person and Organization tables (PERS and ORG respectively) and a relationship table (PERS-ORG_RLTNP), which stores data about relationships between people and organizations, for example, relationships that show when particular people were members of an organization.我有一个 MS Access 2016 数据库,其中包含人员和组织表(分别为 PERS 和 ORG)和关系表(PERS-ORG_RLTNP),它存储有关人员和组织之间关系的数据,例如,显示特定人员何时成为成员的关系一个组织。 The PERS and ORG tables have autonumber primary keys (Pers_ID and Org_ID) that are migrated as foreign keys into PERS-ORG_RLTNP. PERS 和 ORG 表具有作为外键迁移到 PERS-ORG_RLTNP 的自动编号主键(Pers_ID 和 Org_ID)。 PERS-ORG_RLTNP also has a relationship type field (Pers-Org_Rltnp_Typ) as well as Effective_From_Date and Effective_To_Date fields that show when each relationship was in effect. PERS-ORG_RLTNP 也有一个关系类型字段 (Pers-Org_Rltnp_Typ) 以及 Effective_From_Date 和 Effective_To_Date 字段显示每个关系何时生效。

I want the database to permit multiple relationships of the same type between the same person and organization, but I don't want those relationships to overlap.我希望数据库允许同一个人和组织之间存在多个相同类型的关系,但我不希望这些关系重叠。 That is, I don't want the database to permit the Effective_From_Date of the second relationship (the one that starts later) to be during the time period between the Effective_From_Date and Effective_To_Date of the first relationship.也就是说,我不希望数据库允许第二个关系(稍后开始的那个)的 Effective_From_Date 在第一个关系的 Effective_From_Date 和 Effective_To_Date 之间的时间段内。 For example, if Joe Smith is a member of organization XYZ beginning on 1 Jan 1019 and ending on 15 June 2019 and then becomes a member again, I don't want the database to allow the insertion of a record for that second relationship that begins (has an Effective_From_Date) during the time period between 1 Jan 1019 and 15 June 2019.例如,如果 Joe Smith 是 XYZ 组织的成员,从 1019 年 1 月 1 日开始,到 2019 年 6 月 15 日结束,然后再次成为成员,我不希望数据库允许为开始的第二个关系插入记录(具有 Effective_From_Date)在 1019 年 1 月 1 日至 2019 年 6 月 15 日期间。

If there is a way using indexes and validation rules in MS Access that I can enforce this constraint, how do I do it?如果有一种在 MS Access 中使用索引和验证规则的方法可以强制执行此约束,我该怎么做?

Unfortantly, such business rules can't be defined, or managed at the table level by use of indexing, rules, or relationships.不幸的是,此类业务规则无法通过使用索引、规则或关系在表级别定义或管理。 You could start to adopt store procedures, but at the end of the day?您可以开始采用存储过程,但最终呢? Say if database could do this?说如果数据库可以做到这一点? Well, your user interface would really lack the ability to give feed back to the user.好吧,您的用户界面确实缺乏向用户提供反馈的能力。

As a result, it is rather simple matter to code this right into your data entry form(s).因此,将其直接编码到您的数据输入表单中是相当简单的事情。 That way the user gets user friendly prompts, and messages.这样,用户就会得到用户友好的提示和消息。

To prevent collisions, the logic here is quite simple:为了防止碰撞,这里的逻辑很简单:

A collision occurs when:在以下情况下发生碰撞:

RequestStartDate <= EndDate and RequestEndDate >= StartDate RequestStartDate <= EndDate 和 RequestEndDate >= StartDate

The above is thus a rather simply query, but if any collision occurs, the above will return records..and you simply don't allow the booking.因此,上面是一个相当简单的查询,但如果发生任何冲突,上面将返回记录..您根本不允许预订。 In other words, since we NEVER allow booking with a collision, then the above simply statement will work for us.换句话说,由于我们绝不允许发生碰撞的预订,那么上述简单的陈述将适用于我们。

dim strWhere                 as string 
dim dtRequeestStartDate      as date 
dim dtRequestEndDate         as date 

dtRequestStartDate = inputbox("Enter start Date") 
dtRequestEndDate = inputbox("Enter end date") 


strWhere="#" & format(dtRequestStartDate,"mm/­dd/yyyy") & "# <= EndDate" & _ 
 " and #" & format(dtRequestEndDate,"mm/dd­/yyyy") & "#  >= StartDate" 


if dcount("*","tableBooking",strW­here) > 0 then 
   msgbox "sorry, you cant book 
...bla bla bla.... 

The above is just an example, and I am sure you would build a nice form that prompts the user for the booking dates or catch this during data entry.上面只是一个例子,我相信你会构建一个很好的表单来提示用户预订日期或在数据输入过程中捕捉到这一点。

However, what is nice here is that the simple conditions and logic outlined above does return ANY collisions or any overlap in that date range.但是,这里的好处是上面概述的简单条件和逻辑确实会返回该日期范围内的任何冲突或任何重叠。 This includes one inside, or both ends starting outside, or either of one end between the start/end gap.这包括一个内部,或从外部开始的两端,或在开始/结束间隙之间的一端。

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

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