验证oracle中的约束

时间:2018-02-13 22:54:12

标签: oracle validation testing constraints etl

我是oracle的新手,我想知道如何在oracle表中验证约束作为ETL测试过程的一部分。 (这两个表可以是T1和T2)。请让我知道一个示例查询。

谢谢,Santosh

1 个答案:

答案 0 :(得分:1)

以下是您拥有的选项的示例

SQL> create table t1 as select distinct owner from dba_objects;

Table created.

SQL> alter table t1 add primary key ( owner );

Table altered.

SQL> create table t2 as select * from dba_objects;

Table created.

SQL>
SQL> update t2 set owner = 'BAD_DATA' where rownum <= 10;

10 rows updated.

SQL> commit;

Commit complete.

所以我们想要在T1和T2之间链接OWNER,而T2有一些不好的数据。添加约束会产生错误

SQL>
SQL> alter table t2 add constraint fk foreign key ( owner ) references t1 ( owner );
alter table t2 add constraint fk foreign key ( owner ) references t1 ( owner )
                              *
ERROR at line 1:
ORA-02298: cannot validate (MCDONAC.FK) - parent keys not found

所以我要做的第一件事就是在NOVALIDATE模式下启用它,这意味着不会有更多错误数据进入

SQL>
SQL>
SQL>
SQL> alter table t2 add constraint fk foreign key ( owner ) references t1 ( owner ) enable novalidate;

Table altered.

现在,一旦完成,您就可以尝试验证约束。 (显然我们知道这会失败,但在一般情况下,你会启用novalidate,然后尝试启用验证。这意味着你的系统锁定更少)

SQL>
SQL> alter table t2 modify constraint fk enable validate;
alter table t2 modify constraint fk enable validate
                                 *
ERROR at line 1:
ORA-02298: cannot validate (MCDONAC.FK) - parent keys not found

如果我们想查看实际的错误数据,那么我们可以在$ ORACLE_HOME / rdbms / admin中创建一个名为EXCEPTIONS的表,其中包含一个预先发送的脚本。

SQL> @?/rdbms/admin/utlexcpt.sql

Table created.

现在我们可以扩展validate命令来捕获行。我们仍然收到错误

SQL>
SQL> alter table t2 modify constraint fk enable validate exceptions into exceptions;
alter table t2 modify constraint fk enable validate exceptions into exceptions
                                 *
ERROR at line 1:
ORA-02298: cannot validate (MCDONAC.FK) - parent keys not found

但现在坏行已被捕获到表格中

SQL>
SQL> select * from exceptions;

ROW_ID             OWNER                TABLE_NAME CONSTRAINT
------------------ -------------------- ---------- --------------------
AAAySAAAHAAABhjAAA MCDONAC              T2         FK
AAAySAAAHAAABhjAAB MCDONAC              T2         FK
AAAySAAAHAAABhjAAC MCDONAC              T2         FK
AAAySAAAHAAABhjAAD MCDONAC              T2         FK
AAAySAAAHAAABhjAAE MCDONAC              T2         FK
AAAySAAAHAAABhjAAF MCDONAC              T2         FK
AAAySAAAHAAABhjAAG MCDONAC              T2         FK
AAAySAAAHAAABhjAAH MCDONAC              T2         FK
AAAySAAAHAAABhjAAI MCDONAC              T2         FK
AAAySAAAHAAABhjAAJ MCDONAC              T2         FK

10 rows selected.

如果您想运行一个简单的查询来检查哪些行是坏的,那么您想要获取未设置为加入的行的外部联接也会起作用。

SQL>
SQL> select t2.owner, t2.rowid
  2  from t1,
  3       t2
  4  where t2.owner = t1.owner(+)
  5  and t1.owner is null;

OWNER                ROWID
-------------------- ------------------
BAD_DATA             AAAySAAAHAAABhjAAA
BAD_DATA             AAAySAAAHAAABhjAAB
BAD_DATA             AAAySAAAHAAABhjAAC
BAD_DATA             AAAySAAAHAAABhjAAD
BAD_DATA             AAAySAAAHAAABhjAAE
BAD_DATA             AAAySAAAHAAABhjAAF
BAD_DATA             AAAySAAAHAAABhjAAG
BAD_DATA             AAAySAAAHAAABhjAAH
BAD_DATA             AAAySAAAHAAABhjAAI
BAD_DATA             AAAySAAAHAAABhjAAJ

10 rows selected.

SQL>
SQL>
SQL>