断言一组列可以在雪花中形成主要钥匙的最佳方法是什么?
臭名昭著的主要关键约束是 不强制a>在雪花sql中:
-- Generating a table with 4 rows that contain duplicates and NULLs:
CREATE OR REPLACE TEMP TABLE PRIMARY_KEY_TEST AS
SELECT
*
FROM (
SELECT 1 AS PK, 'TEST_TEXT' AS TEXT
UNION ALL SELECT 1 AS PK, 'TEST_TEXT' AS TEXT
UNION ALL SELECT NULL AS PK, NULL AS TEXT
UNION ALL SELECT NULL AS PK, NULL AS TEXT
)
;
SELECT *
FROM PRIMARY_KEY_TEST
;
pk | 文本 |
---|---|
1 | test_text |
1 | test_text |
null | null null null null |
null null null null null null null null null null null null null null null null null null null | null |
-- These constraints will NOT throw any errors in Snowflake
ALTER TABLE PRIMARY_KEY_TEST ADD PRIMARY KEY (PK);
ALTER TABLE PRIMARY_KEY_TEST ADD UNIQUE (TEXT);
null null null null null null null null null nuln nuln nuln nuln nuln n n均值很大> 在更新一组数据时检查至关重要。
因此,我正在寻找易于编写和读取的(理想情况下是1-2行)的代码(易于基于某些雪花功能),如果一组列不再形成Snowflake SQL中的可行主键,则会引发错误。
有什么建议吗?
Infamously primary key constraints are not enforced in snowflake sql:
-- Generating a table with 4 rows that contain duplicates and NULLs:
CREATE OR REPLACE TEMP TABLE PRIMARY_KEY_TEST AS
SELECT
*
FROM (
SELECT 1 AS PK, 'TEST_TEXT' AS TEXT
UNION ALL SELECT 1 AS PK, 'TEST_TEXT' AS TEXT
UNION ALL SELECT NULL AS PK, NULL AS TEXT
UNION ALL SELECT NULL AS PK, NULL AS TEXT
)
;
SELECT *
FROM PRIMARY_KEY_TEST
;
PK | TEXT |
---|---|
1 | TEST_TEXT |
1 | TEST_TEXT |
NULL | NULL |
NULL | NULL |
-- These constraints will NOT throw any errors in Snowflake
ALTER TABLE PRIMARY_KEY_TEST ADD PRIMARY KEY (PK);
ALTER TABLE PRIMARY_KEY_TEST ADD UNIQUE (TEXT);
However knowing that a set of colums has values that are uniuqe for every row and never NULL
is vital to check when updating a set of data.
So I'm looking for a easy to write and read (ideally 1-2 lines) piece of code (proably based on some Snowflake function) that throws an error if a set of columns no longer forms a viable primary key in Snowflake SQL.
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误测试查询很容易使用
合格
和窗口计数编写。该模式是将主密钥列列表按部分分配并搜索非唯一值,也可以添加对空的其他检查。如果列列表是主键的有效候选者,则不会返回任何行,如果有行违反规则将被返回:可以使用雪花脚本和提升例外:
Such test query is easy to write using
QUALIFY
and windowed COUNT. The pattern is to place primary key column list into PARTITION BY part and search for non-unique values, additional check for nulls could be added too. If the column list is a valid candidate for Primary key, it will not return any rows, if there are rows violating the rules they will be returned:It is possible using Snowflake Scripting and RAISE exception:
您可以通过在您希望不能够无效的列上添加非零件约束,从而在雪花上执行无效。
主要的关键限制仅是信息性;将数据插入表中时不会强制执行。对于主键,您必须删除 /删除数据或插入之前,您必须检查数据是否存在,然后仅更新。
根据您正在做的事情,您可以使用以下
you can enforce NOT NULL in Snowflake by adding a NOT NULL constraint on the columns which you want not to be null able.
The primary key constraint is informational only; It is not enforced when you insert the data into a table. For primary key you will have to either remove / delete the data or before inserting you will have to check if the data exists then you only update.
Depending upon what you are doing you may use the following