SQL 查找为什么 PK 候选者在无键表上有重复项
如果我的标题让你头疼……我和你在一起。我不想解释为什么这个表存在,除了它是遗留系统的一部分,而且该系统也执行“记录级访问”(RLA),我知道这对于许多表来说都是一个问题,无论如何,RLA 是提到这一点是因为添加列会更改表格式,然后许多非常旧的程序将不再工作...
显然添加 PK 已被证明不会更改表格式。所以我被告知,一组特定的密钥保证是唯一的,那么你知道什么......事实并非如此。现在我需要展示他们不存在的地方。
我能想到的是:
获取表与其主键匹配的叉积。
以某种方式在结果集中获取一个计数列,以获取 PK 与其自身匹配的条目数。
过滤该结果集以查找 count id 大于 2 的值。
我将看看是否充分扩展 PK,我实际上会发现一些独特的东西。
If my title hurts your head... I'm with you. I don't want to get into why this table exists except that it is part of a legacy system, also the system does "record level access"(RLA) and this I know will be an issue for many tables, anyways the RLA is mentioned because adding a column will change the table format and then many very old programs will no longer work...
Apparently adding a PK has been shown not to change the table format. So I've been told that a certain set of keys is guarantied to be unique, well what do you know... it isn't. And now I need to show where they aren't.
All I can think of is:
Get the cross product where the table matches on it's primary key.
Somehow get a count column onto the result set for the number of entries where the PK matches it self.
Filter that result set for values where count id greater than 2.
I'm going to see if I expand the PK sufficiently I'll actually find something unique.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除约束/唯一索引,插入数据,然后运行此查询:
其中
col1, col2, ..., coln
是键中的列列表(一列或多列)。结果将是出现多次的键的列表以及显示它们出现频率的计数。Remove the constraints / unique indexes, insert the data, and then run this query:
where
col1, col2, ..., coln
is the list of columns in your key (one or more columns). The result will be the list of keys that occur more than once together with a count showing how often they occur.