Vertica:重复/主键的数据验证

时间:2012-09-28 23:10:08

标签: 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');

我的想法是做一些条件逻辑。 Psudo代码如下。你能帮我准备一下Vertica吗?

Begin
load data
if (select count(*) from (select analyze_constraints('my_table')) sub) == 0:
commit
else rollback

3 个答案:

答案 0 :(得分: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;    

答案 1 :(得分:1)

Vertica没有存储过程。您需要在Vertica之外以某种方式以编程方式执行此操作。

你拥有的伪逻辑是好的;只需在某些东西中实现它(JAVA,C ++等)。您不需要执行'回滚'。加载数据时(使用NO COMMIT),在执行COMMIT语句之前不会提交。

答案 2 :(得分:0)

使用ansi merge怎么样?

- 使用快速批量加载程序将数据上传到临时表 -Merge从临时表到基表

相关问题