Quantcast
Channel: Thinking Out Loud
Viewing all articles
Browse latest Browse all 666

Using Exception Table

$
0
0

It has been quite a while since I have used Exception table and just a quick demo to share.

oracle@arrow1:HAWKA:/media/sf_working/sql
$ sqlplus demo/demo @exception_demo.sql

SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 7 16:36:33 2017

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning and Real Application Testing options

ARROW1:(DEMO@HAWKA):PRIMARY> col OWNER for a20
ARROW1:(DEMO@HAWKA):PRIMARY> col TABLE_NAME for a20
ARROW1:(DEMO@HAWKA):PRIMARY> col R_CONSTRAINT_NAME for a20
ARROW1:(DEMO@HAWKA):PRIMARY> drop table f purge;

Table dropped.

ARROW1:(DEMO@HAWKA):PRIMARY> drop table p purge;

Table dropped.

ARROW1:(DEMO@HAWKA):PRIMARY> drop table exceptions purge;

Table dropped.

ARROW1:(DEMO@HAWKA):PRIMARY> set echo off
Running @?/rdbms/admin/utlexcpt.sql

Table created.

ARROW1:(DEMO@HAWKA):PRIMARY> desc exceptions
 Name                    Null?    Type
 ----------------------- -------- ----------------
 ROW_ID                           ROWID
 OWNER                            VARCHAR2(30)
 TABLE_NAME                       VARCHAR2(30)
 CONSTRAINT                       VARCHAR2(30)

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> create table p
  2  ( id number not null,
  3    CONSTRAINT p_pk PRIMARY KEY (id)
  4  );

Table created.

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> create table f
  2  ( id number not null,
  3    CONSTRAINT  f_fk FOREIGN KEY (id) REFERENCES  p(id)
  4  );

Table created.

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> alter table f DISABLE CONSTRAINT f_fk
  2  ;

Table altered.

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> insert into p values (1);

1 row created.

ARROW1:(DEMO@HAWKA):PRIMARY> insert into f values (2);

1 row created.

ARROW1:(DEMO@HAWKA):PRIMARY> commit;

Commit complete.

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> alter table f ENABLE CONSTRAINT f_fk
  2  EXCEPTIONS INTO exceptions
  3  ;
alter table f ENABLE CONSTRAINT f_fk
                                *
ERROR at line 1:
ORA-02298: cannot validate (DEMO.F_FK) - parent keys not found


ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> select owner,table_name,constraint_type,R_CONSTRAINT_NAME
  2  from dba_constraints
  3  where owner in ('DEMO')
  4  and table_name in ('P','F')
  5  order by owner, table_name,R_CONSTRAINT_NAME asc,constraint_type desc
  6  ;

OWNER                TABLE_NAME           C R_CONSTRAINT_NAME
-------------------- -------------------- - --------------------
DEMO                 F                    R P_PK
DEMO                 F                    C
DEMO                 P                    P
DEMO                 P                    C

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> select owner,table_name,constraint_type,R_CONSTRAINT_NAME
  2  from dba_constraints
  3  where owner in ('DEMO')
  4  and constraint_name in ('P_PK')
  5  order by owner, table_name,R_CONSTRAINT_NAME asc,constraint_type desc
  6  ;

OWNER                TABLE_NAME           C R_CONSTRAINT_NAME
-------------------- -------------------- - --------------------
DEMO                 P                    P

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> select * from exceptions
  2  ;

ROW_ID             OWNER                TABLE_NAME           CONSTRAINT
------------------ -------------------- -------------------- ------------------------------
AAAEv1AAFAAAAerAAA DEMO                 F                    F_FK

ARROW1:(DEMO@HAWKA):PRIMARY>
ARROW1:(DEMO@HAWKA):PRIMARY> select f.*
  2  from f, exceptions
  3  where exceptions.constraint='F_FK'
  4  and f.rowid=exceptions.row_id
  5  ;

        ID
----------
         2

ARROW1:(DEMO@HAWKA):PRIMARY>


Viewing all articles
Browse latest Browse all 666

Trending Articles