Pet*_*ter 7 sql database database-design duplicates vertica
我正在尝试在检查以确保数据不重复的加载期间创建验证过程.Vertica本身不支持此功能:
运行查询时,Vertica会检查约束违规,而不是在加载数据时检查.要在加载过程中检测约束违规,请使用带有NO COMMIT选项的COPY(第667页)语句.通过在不提交数据的情况下加载数据,您可以使用ANALYZE_CONSTRAINTS函数对数据进行后加载检查.如果函数发现约束违规,则可以回滚负载,因为您尚未提交它.
问题是我无法弄清楚如何以编程方式执行此操作.我怀疑我需要一个存储过程,但我不熟悉vertica的存储过程语法/限制.你能帮我吗?这就是我所拥有的:
-- Create a new table. "id" is auto-incremented and "name" must be unique
CREATE TABLE IF NOT EXISTS my_table (
id IDENTITY
, name varchar(50) UNIQUE NOT NULL
, type varchar(20)
, description varchar(200)
);
--Insert a record
begin;
copy my_table from stdin
abort on error
NO COMMIT; -- this begins the load
name1|type1|description1 --this is the load
\. -- this closes the load
commit;
-- insert the duplicate record
begin;
copy my_table from stdin
abort on error
NO COMMIT; -- this begins the load
name1|type1|description1 --this is the load
\. -- this closes the load
commit; -- Surprisingly, the load executes successfully! What's going on?!?!
-- Check constraints. We see that there is a failed constraints:
select analyze_constraints('my_table');
Run Code Online (Sandbox Code Playgroud)
我的想法是做一些条件逻辑.Psudo代码如下.你能帮我准备一下Vertica吗?
Begin
load data
if (select count(*) from (select analyze_constraints('my_table')) sub) == 0:
commit
else rollback
Run Code Online (Sandbox Code Playgroud)
小智 4
-- Start by Setting Vertica up to rollback and return an error code
-- if an error is encountered.
\set ON_ERROR_STOP on
-- Load Data here (code omitted since you already have this)
-- Raise an Error condition by selecting 1/0 if any rows were rejected
-- during the load
SELECT
GET_NUM_REJECTED_ROWS() AS NumRejectedRows
,GET_NUM_ACCEPTED_ROWS() AS NumAcceptedRows
;
SELECT 1 / (1-SIGN(GET_NUM_REJECTED_ROWS()));
-- Raise an Error condition if there are duplicates in my_table
SELECT 1 / ( 1 - SIGN( COUNT(*) ) )
FROM ( SELECT name1,type1,description1
FROM MY_TABLE
GROUP BY 1,2,3
HAVING COUNT(*) > 1 ) AS T1 ;
-- Raise an Error if primary key constraint is violated.
SELECT 1 / ( 1 - SIGN( COUNT(*) ) )
FROM (SELECT ANALYZE_CONSTRAINTS ('my_table')) AS T1;
COMMIT;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4503 次 |
| 最近记录: |