SQL 查找为什么 PK 候选者在无键表上有重复项

发布于 2024-10-07 18:58:46 字数 381 浏览 2 评论 0原文

如果我的标题让你头疼……我和你在一起。我不想解释为什么这个表存在,除了它是遗留系统的一部分,而且该系统也执行“记录级访问”(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女 2024-10-14 18:58:46

删除约束/唯一索引,插入数据,然后运行此查询:

SELECT col1, col2, ..., coln, COUNT(*)
FROM your_table
GROUP BY col1, col2, ..., coln
HAVING COUNT(*) > 1

其中 col1, col2, ..., coln 是键中的列列表(一列或多列)。结果将是出现多次的键的列表以及显示它们出现频率的计数。

Remove the constraints / unique indexes, insert the data, and then run this query:

SELECT col1, col2, ..., coln, COUNT(*)
FROM your_table
GROUP BY col1, col2, ..., coln
HAVING COUNT(*) > 1

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.

故事还在继续 2024-10-14 18:58:46
select col1, ... from tab group by col1, ... having count(*)>1;
select col1, ... from tab group by col1, ... having count(*)>1;
白云悠悠 2024-10-14 18:58:46
SELECT * FROM (SELECT ID, COUNT(*) CNT FROM MY_TABLE GROUP BY ID) WHERE CNT > 1
SELECT * FROM (SELECT ID, COUNT(*) CNT FROM MY_TABLE GROUP BY ID) WHERE CNT > 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文