简体   繁体   English

数据库设计中的关系与数据约束

[英]Relations vs data constraints in database design

To simplify the description let's assume we've got a DB model with TimeScopes (ID, start, end) and Processes with FK (TimeScopes.ID), and this is one-to-many relation where all processes holding a reference to the TimeScopes.ID are running within the given time scope.为了简化描述,假设我们有一个 DB model 和 TimeScopes (ID, start, end) 和带有 FK (TimeScopes.ID) 的进程,这是一对多的关系,所有进程都持有对 TimeScopes 的引用.ID 在给定时间内运行 scope。

So first, you define a single time scope and pick multiple processes running at that time scope.因此,首先,您定义一个时间 scope 并选择当时运行的多个进程 scope。

TimeScopes
  |__
     Processes

There are some additional constraints in the application (no DB triggers in the DB so far), like assumptions that TimeScopes are not overlapping - only one TimeScope could be active at the time.应用程序中有一些额外的限制(到目前为止,数据库中没有数据库触发器),例如假设 TimeScopes 不重叠 - 当时只有一个 TimeScope 可能处于活动状态。

Now I've got a new requirement to add some new Processes that could be initiated at any moment in time without any constraint - so effectively they do not have to, but they could be running simultaneously at the same time (in parallel).现在我有了一个新的要求,即添加一些可以在任何时候不受任何限制地启动的新进程——因此它们不必这样做,但它们可以同时(并行)同时运行。

In this case, someone would define the process first and then assign it to such a new, single Process multiple time scopes without a constraint mentioned earlier.在这种情况下,有人会先定义流程,然后将其分配给这样一个新的、单一的流程多个时间范围,而没有前面提到的约束。 So this would be quite the opposite relation one Process to many TimeScopes.所以这将是一个进程与许多 TimeScopes 的完全相反的关系。

Processes
  |__
     TimeScopes

I'm looking for help and advice on how to solve it?我正在寻找有关如何解决它的帮助和建议?

From the relations perspective, I can adjust the relation between TimeScopes and Processes to many-to-many by adding a table to define relations in both directions.从关系的角度来看,我可以通过添加一个表来定义两个方向的关系,将 TimeScopes 和 Processes 之间的关系调整为多对多。

TimeScopes
  |__
   __ TP-Relations
  |
Processes

What makes me uncertain about that is the additional time constraint from the existing model.让我不确定的是现有 model 的额外时间限制。 That drives me to think about an alternative solution and define maybe a separate tables for NewProcesses and NewTimeScopes这促使我考虑另一种解决方案,并为 NewProcesses 和 NewTimeScopes 定义一个单独的表

NewProcesses
  |__
     NewTimeScopes

and later combine results between two models in queries by use of UNION statement.然后使用 UNION 语句在查询中组合两个模型之间的结果。

Which seems to be better?哪个似乎更好? Any other or better ideas?还有其他或更好的想法吗?

Update/Rephrase the question更新/改写问题

Let's say we've got Entities "A" and "B" in many-to-many relation made with additional "AB" (junction) table.假设我们有实体“A”和“B”与附加的“AB”(联结)表建立多对多关系。

For some records representing A->B in 1-to-many relation, there are some additional constraints about the A records.对于以一对多关系表示 A->B 的一些记录,关于 A 记录还有一些额外的限制。 Let's say we've got some triggers to watch each record in A doesn't cross the boundaries of another A record.假设我们有一些触发器来观察 A 中的每条记录不会跨越另一个 A 记录的边界。

For other records representing B->A in 1-to-many relation these boundaries are not always acceptable so shouldn't be forced/checked.对于以一对多关系表示 B->A 的其他记录,这些边界并不总是可以接受的,因此不应强制/检查。

Question: How to deal with such a problem?问题:如何处理这样的问题?

  • Modify the model/relations, how?修改模型/关系,如何?
  • Modify the triggers?修改触发器? but is it even possible on the DB level to recognise that some records would be entered for A->B vs B->A relations?但是在数据库级别上是否甚至可以识别为 A->B 与 B->A 关系输入一些记录?

Based on the comments, it appears the following is true:根据评论,似乎以下情况属实:

  • A timescope can have many processes一个时间范围可以有很多进程
  • A process can belong to many timescopes一个进程可以属于多个时间范围
  • A timescope should not have an overlapping start or end with another timescope if the timescope has more than one process.如果时间范围有多个进程,则时间范围不应与另一个时间范围有重叠的开始或结束。

I would separate this out into two questions:我会将其分为两个问题:

How should I model the relationship between process and timescope?我应该如何 model 进程和时间范围的关系?

It seems that this is a straightforward many-to-many relationship, so I would include a joining table as you suggest.这似乎是一个简单的多对多关系,所以我会按照你的建议包括一个连接表。

How should I ensure the business rule about overlapping timescopes?我应该如何确保有关重叠时间范围的业务规则?

I don't think you can enforce this through the entity relationships in your model.我认为您不能通过 model 中的实体关系来强制执行此操作。 You then have a few options - if you must implement it at the database level, a trigger on the relation table should do the job - your rule applies only if there is more than 1 process for a given timescope, which will cause an insert or update event on your join table.然后您有几个选项 - 如果您必须在数据库级别实现它,关系表上的触发器应该可以完成这项工作 - 您的规则仅适用于给定时间范围内有超过 1 个进程的情况,这将导致插入或更新连接表上的事件。

I am not a huge fan of triggers - they are hard to test, hard to debug, and can lead to performance problems.我不是触发器的忠实拥护者——它们难以测试、难以调试,并且可能导致性能问题。 If at all possible, I'd implement this in the application layer.如果可能的话,我会在应用层实现它。

Should you store different types of timescopes/processes in different tables?您是否应该将不同类型的时间范围/流程存储在不同的表中?

This is partly philosophical, partly practical.这部分是哲学的,部分是实用的。 In general, you should store similar types of entity in the same table.通常,您应该将相似类型的实体存储在同一个表中。 Especially if they vary in behaviour, but not attributes;特别是如果它们的行为不同,但属性不同; don't repeat yourself and all that.不要重复自己和所有这些。

If you have different validation rules, but identical attributes, I'd keep them in a single table.如果您有不同的验证规则,但属性相同,我会将它们保存在一个表中。

This is all subject to "understandability" - if your system describes mammals, keeping "humans" and "cats" in the same table with a type discriminator is probably fine.这一切都取决于“可理解性” - 如果您的系统描述哺乳动物,则将“人类”和“猫”与类型鉴别器放在同一张表中可能没问题。 If your system describes "humans" and "tables", the fact that both have "leg_count" attributes is not a reason to keep them in the same table.如果您的系统描述了“humans”和“tables”,那么两者都具有“leg_count”属性的事实并不是将它们保留在同一个表中的理由。

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

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