gpt4 book ai didi

sql - 通过 SQL 代码识别候选主键

转载 作者:行者123 更新时间:2023-12-03 03:27:48 24 4
gpt4 key购买 nike

我有一个包含数百万行且没有约束的原始数据,我想通过 SQL 代码识别主键的唯一列。

有什么方法可以通过 SQL 代码识别候选主键吗?

最佳答案

您可以尝试以下查询

select count(<potential_column>),count(distinct <potential column>) from <table>

当计数匹配时,您就有了一个可供考虑的候选键。

例如,如果我有一个名为 PEOPLE 的人员表,我可能会执行类似的操作。

select 'FullName' as FldName, count(fullname) as Tot,count(distinct fullName) as TotD 
from People
union
select 'SSN' as FldName, count(SSN) as Tot,count(distinct SSN) as TotD
from People

这将返回两行,显示字段名称和计数。任何 tottotd 匹配的行都将是候选行,尽管在本例中,我会根据数据知识依赖 SSN(社会安全号码)。

您可以使用连接字段运行相同的查询,例如

  select 'First/Last' as FldName, count(Firstname+lastName) as Tot,
count(distinct firstname+LastName) as TotD
from People

如果您正在寻找复合键,尽管尝试决定连接哪些字段需要更好地了解您的数据

关于sql - 通过 SQL 代码识别候选主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27619300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com