SQL Server:“最独特”指数
在表中,我想确保五列键上仅存在唯一值:
Timestamp Account RatingDate TripHistoryKey EventAction
========= ======= ========== ============== ===========
2010511 1234 2010511 1 INSERT
2010511 1234 2010511 4 INSERT
2010511 1234 2010511 7 INSERT
2010511 1234 2010511 1 INSERT <---duplicate
但是我只希望当EventAction为时在行之间应用唯一约束插入:
Timestamp Account RatingDate TripHistoryKey EventAction
========= ======= ========== ============== ===========
2010511 1234 2010511 1 INSERT
2010511 1234 2010511 1 UPDATE
2010511 1234 2010511 1 UPDATE <---not duplicate
2010511 1234 2010511 1 UPDATE <---not duplicate
2010511 1234 2010511 1 DELETE <---not duplicate
2010511 1234 2010511 1 DELETE <---not duplicate
2010511 1234 2010511 1 INSERT <---DUPLICATE
可能吗?
In a table i want to ensure that only unique vales exist over the five-column key:
Timestamp Account RatingDate TripHistoryKey EventAction
========= ======= ========== ============== ===========
2010511 1234 2010511 1 INSERT
2010511 1234 2010511 4 INSERT
2010511 1234 2010511 7 INSERT
2010511 1234 2010511 1 INSERT <---duplicate
But i only want the unique constraint to apply between rows when EventAction is INSERT
:
Timestamp Account RatingDate TripHistoryKey EventAction
========= ======= ========== ============== ===========
2010511 1234 2010511 1 INSERT
2010511 1234 2010511 1 UPDATE
2010511 1234 2010511 1 UPDATE <---not duplicate
2010511 1234 2010511 1 UPDATE <---not duplicate
2010511 1234 2010511 1 DELETE <---not duplicate
2010511 1234 2010511 1 DELETE <---not duplicate
2010511 1234 2010511 1 INSERT <---DUPLICATE
Possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是
编辑:
Yes
Edit:
什么是事件动作?
我怀疑您在这里工作时遇到设计问题,并且您可能需要考虑为每种类型的 EventAction 创建一个表/关系。例如,这样做将使您能够在 InsertEventAction 表上创建唯一约束。
也许您可以提供您的问题的业务背景。
根据评论回复:鉴于数据源的性质和您希望实现的解析活动,我认为 gbn 已经建议了您的最佳选择。
遗憾的是源数据库不是 SQL Server,因为您可以使用触发器实现自己的审核机制。这样的解决方案可以在触发器中包含“过滤器”逻辑。
What is an EventAction?
I suspect that you have a design issue at work here and that you may want to consider creating a table/relation for each type of EventAction. Doing so would enable you to create a unique constraint on the InsertEventAction table for example.
Perhaps you can provide the business context to your question.
Following on from comments reply: Given the nature of the data source and the parsing activity you wish to implement I think gbn has suggested your best options.
It's a shame the source database is not also SQL Server as you could implement your own audit mechanism using Triggers. Such a solution could include your "filter" logic within the Trigger.
我会考虑通过检查约束来做到这一点。我认为传统的唯一约束不起作用。
I would consider doing this with a check constraint. I don't think a traditional unique constraint will work.
这是“INSTEAD OF INSERT”触发器的示例。 EXISTS 检查是否存在具有相同值的现有行,但仅当 EventAction 为 INSERT 时才有效。
This is an example of an "INSTEAD OF INSERT" trigger. The EXISTS checks if there are existing rows with the same values, but only when EventAction is INSERT.