SQL Server 2008数据库中字段的复杂约束
我有以下(简化的)结构,允许我跟踪已分配给单元的设备。由于设备只能在表中的单元格中出现一次,因此我创建了一个约束,规定 idEquipment 和 idCell 在表中必须是唯一的。
CREATE TABLE [dbo].[CellEquipment](
[idEquipment] [int] NOT NULL,
[idCell] [int] NULL,
CONSTRAINT [PK_CellEquipment] UNIQUE NONCLUSTERED
(
[idEquipment] ASC,
[idCell] ASC
)
这个限制确保我永远不会将同一件设备添加到一个单元中两次。
所以现在我的任务是保存历史信息。我需要能够提取工作订单,查看其日期,然后找到该工作订单使用的设备。解决方案是将日期信息添加到表中,如下所示:
CREATE TABLE [dbo].[CellEquipment](
[idEquipment] [int] NOT NULL,
[idCell] [int] NULL,
[DateAdded] [datetime] NULL,
[DateRemoved] [datetime] NULL,
)
现在我上面的约束被打破了。 idCell/idEquipment
不再是唯一的,因为设备可以移除并重新添加到单元中。现在我有一些棘手的约会问题需要解决。为了确保数据完整性,对于数据库的更改必须满足以下条件:
idCell/idEquipment are unique (like before) OR
idCell/idEquipment's new DateAdded doesn't fall between a DateAdded/Removed OR
idCell/idEquipment's new DateRemoved doesn't fall between a DateAdd/Removed or
idCell/idEquipment's doesn't have a record with DateRemoved=NULL
检查约束没有能力实现这一点,唯一索引约束也无法做到这一点。顺便说一句,可以创建检查约束来确保 DateAdded DateAdded
DateAdded
DateAdded
DateAdded
DateAdded
DateAdded
DateRemoved
(以及其他 NULL/NOT NULL 约束关系)
我是否需要从代码、事务、不同的模型中强制执行这些关系?
也许有一种我不知道的设计模式可以帮助存储此类历史数据?
I have the following (simplified) structure that allows me to track equipment that has been assigned to a cell. Since equipment can only be in the cell once in the table, I've created a constraint that says that idEquipment and idCell must be unique in the table.
CREATE TABLE [dbo].[CellEquipment](
[idEquipment] [int] NOT NULL,
[idCell] [int] NULL,
CONSTRAINT [PK_CellEquipment] UNIQUE NONCLUSTERED
(
[idEquipment] ASC,
[idCell] ASC
)
This constraint ensures that I never add the same piece of equipment to a cell twice.
So now I've been tasked with keeping history information. I need to be able to pull up a workorder, see its date, and then find what equipment was used on that work order. A solution is to add date information to the table like this:
CREATE TABLE [dbo].[CellEquipment](
[idEquipment] [int] NOT NULL,
[idCell] [int] NULL,
[DateAdded] [datetime] NULL,
[DateRemoved] [datetime] NULL,
)
Now my constraint from above is broken. idCell/idEquipment
are no longer unique since equipment can be removed and re-added to the cell. Now I have some tricky date issues to contend with. To ensure data integrity the following must be true for changes to the database:
idCell/idEquipment are unique (like before) OR
idCell/idEquipment's new DateAdded doesn't fall between a DateAdded/Removed OR
idCell/idEquipment's new DateRemoved doesn't fall between a DateAdd/Removed or
idCell/idEquipment's doesn't have a record with DateRemoved=NULL
The Check constraints don't have the power to pull this off and the unique index constraints can't do it either. BTW a Check Constraint can be created to ensure that DateAdded < DateRemoved
(along with other NULL/NOT NULL constraint relationships)
Do I need to do enforce these relationships from code, a transaction, a different model?
Perhaps there is a design pattern that I don't know about that can help with storing such historical data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为我不知道设备和细胞对你来说意味着什么,所以我在这里可能会大错特错。
但在我看来,重要的信息在于“工单编号中使用了哪些设备?”这个问题。日期并不能真正给你这些信息。但这个(空中代码)结构可能会。
这使得在给定的工作订单上使用设备变得非常简单。要获取给定日期使用的设备,请连接 CellEquipment、work_order_equipment 和工作订单,然后查看工作订单表中的日期。
Since I don't know what equipment and cell mean to you, I could be way off base here.
But it seems to me that the important information is in the question "What equipment was used in work order number ?" Dates don't really give you that information. But this (air code) structure might.
That makes it dead simple to get the equipment used on a given work order. To get the equipment used on a given date, join CellEquipment, work_order_equipment, and workorders, and look at the dates in the workorders table.