在Netezza中,未强制执行主键。线程How to overcome Netezza's lack of unique constraint/referential integrity enforcement?回答了如何写入存储过程来强制执行它。
在我们的案例中,我们不想使用存储过程强制执行它,因为它会破坏我们的加载。相反,我们每月检查一次数据库完整性以找到可能的重复项。我们需要一个脚本来帮助我们为所有表生成以下查询:
SELECT {PK1}, {PK2}, COUNT(*)
FROM {TABLENAME}
GROUP BY {PK1}, {PK2} HAVING COUNT(*)>1
LIMIT 100;
答案 0 :(得分:0)
以下答案使用了我的另一篇文章https://stackoverflow.com/a/36989929/4266330
的知识以下脚本将返回您想要的结果
SELECT 'SELECT '||GROUP_BY_COLS||', COUNT(*)'||CHR(13)||
' FROM '||SCHEMA||'.'||TABLENAME||CHR(13)||
' GROUP BY '||GROUP_BY_COLS||' HAVING COUNT(*)>1'||CHR(13)||
' LIMIT 100;'
FROM (
SELECT
R.SCHEMA AS SCHEMA
,R.NAME AS TABLENAME
,TOOLKIT.SQLEXT.REGEXP_REPLACE(TOOLKIT.SQLEXT.GROUP_CONCAT('@'||LPAD(K.CONSEQ, 4, '0')||'@'||K.ATTNAME, ', '),
'@[0-9]{4}@', '') AS GROUP_BY_COLS
FROM _V_RELATION_COLUMN R
JOIN _V_RELATION_KEYDATA K
ON (R.SCHEMA=K.SCHEMA
AND R.NAME=K.RELATION
AND R.ATTNAME=K.ATTNAME)
WHERE K.CONTYPE='p'
GROUP BY 1, 2
) S;
示例结果:
SELECT IDP_EFFECTIVE_DATE, ACCOUNT_ID, COUNT(*) FROM AAA.L1_ACCOUNTS GROUP BY IDP_EFFECTIVE_DATE, ACCOUNT_ID HAVING COUNT(*)>1 LIMIT 100; SELECT DEAL_NUM, COUNT(*) FROM ABUHARI.L1_BMO16_BOOST_EXTRACT_TOR GROUP BY DEAL_NUM HAVING COUNT(*)>1 LIMIT 100; SELECT IDP_WAREHOUSE_ID, IDP_AUDIT_ID, ACCOUNT_KEY, COUNT(*) FROM ACTIMIZE.L1_ACTIMIZE_US12_ACCOUNT GROUP BY IDP_WAREHOUSE_ID, IDP_AUDIT_ID, ACCOUNT_KEY HAVING COUNT(*)>1 LIMIT 100;