19 April 2007

Establishing Refential Integrity With Materialized Views In Oracle

RI(Refential Integrity )  is established with FK(foreign key) and PK(primary key) in Oracle. If you do not have a primary key on a column you are not allowed to create a FK. If your table  has a non-unique column, how can you create a FK? Example below shows how to establish RI with MVs: 
 
SQL> create table a(i number);
 
Table created
 
SQL> insert into a values(1);
 
1 row inserted
 
SQL> insert into a values(1);
 
1 row inserted
 
SQL> create table b(k number);
 
Table created
 
SQL> alter table b add constraint fk_ba foreign key(k) references a(i);
 
alter table b add constraint fk_ba foreign key(k) references a(i)
 
ORA-02270: no matching unique or primary key for this column-list
 
 
SQL> create materialized view mv_a
  2  parallel
  3  build immediate as
  4  select distinct i from a;
 
Materialized view created
 
SQL> alter materialized view mv_a add constraint pk_mv_a primary key(i);
 
Materialized view altered
 
SQL> alter table b add constraint fk_ba foreign key(k) references mv_a(i);
 
Table altered
 
SQL> insert into b values(1);
 
1 row inserted
 
SQL> insert into b values(2);
 
insert into b values(2)
 
ORA-02291: integrity constraint (HR.FK_BA) violated - parent key not found
 
SQL>
 

02 April 2007

Workaround for ORA-00283, ORA-01610, ORA-00279, ORA-00339 Errors When Duplicating Database

In my earlier posts i described how to clon an oracle database.Today when i clon database for development environment, i struggled some oracle errors. I want to share this experince by this entry:

After creatimg control files....


 
oracle@aur:/data/oradata> sqlplus / as sysdba

SQL*Plus: Release 10.1.0.5.0 - Production on Fri Dec 8 16:38:06 2006

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

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area  293601280 bytes
Fixed Size                  1302824 bytes
Variable Size              94117592 bytes
Database Buffers          197132288 bytes
Redo Buffers                1048576 bytes
SQL> alter database recover database until cancel;
alter database recover database until cancel
*
ERROR at line 1:
ORA-01507: database not mounted


SQL> alter database mount;

Database altered.

SQL> alter database recover database until cancel;
alter database recover database until cancel
*
ERROR at line 1:
ORA-00283: recovery session canceled due to errors
ORA-01610: recovery using the BACKUP CONTROLFILE option must be done


SQL> recover database using backup controlfile until cancel;
ORA-00279: change 9058471059996 generated at 12/08/2006 14:18:39 needed for
thread 1
ORA-00289: suggestion :
/data06/app/oracle/product/10.1/dbs/arch1_1_608653118.dbf
ORA-00280: change 9058471059996 for thread 1 is in sequence #1


Specify log: {=suggested | filename | AUTO | CANCEL}
/data/oradata/test/redo01.log
ORA-00339: archived log does not contain any redo
ORA-00334: archived log: '/data/oradata/test/redo01.log'


ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/data/oradata/test/system01.dbf'


SQL> recover database using backup controlfile until cancel;
ORA-00279: change 9058471059996 generated at 12/08/2006 14:18:39 needed for
thread 1
ORA-00289: suggestion :
/data06/app/oracle/product/10.1/dbs/arch1_1_608653118.dbf
ORA-00280: change 9058471059996 for thread 1 is in sequence #1


Specify log: {=suggested | filename | AUTO | CANCEL}
/data/oradata/test/redo02.log
ORA-00339: archived log does not contain any redo
ORA-00334: archived log: '/data/oradata/test/redo02.log'


ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/data/oradata/test/system01.dbf'


SQL> recover database using backup controlfile until cancel;
ORA-00279: change 9058471059996 generated at 12/08/2006 14:18:39 needed for
thread 1
ORA-00289: suggestion :
/data06/app/oracle/product/10.1/dbs/arch1_1_608653118.dbf
ORA-00280: change 9058471059996 for thread 1 is in sequence #1


Specify log: {RET=suggested | filename | AUTO | CANCEL}
/data/oradata/test/redo03.log
Log applied.
Media recovery complete.
SQL> RECOVER DATABASE
ORA-00283: recovery session canceled due to errors
ORA-01610: recovery using the BACKUP CONTROLFILE option must be done


SQL> ALTER DATABASE OPEN RESETLOGS;

Database altered.

SQL> ALTER TABLESPACE TEMP ADD TEMPFILE '/data/oradata/test/temp01.dbf'
  2       SIZE 1470M REUSE AUTOEXTEND ON NEXT 655360  MAXSIZE 32767M;

Tablespace altered.

SQL> select * from dual;

D
-
X


SQL>

Solution of ORA-06502

When you work on associative arrays in Oracle you must consider key lengths. When you create an associative arrays with 4 chars length, it means that you canuse key names up to 4 letters.If you convert keys expilicitly with to_char built-in function, you must trim to eliminate spaces; otherwise you are a potential person that get ORA-06502 error as i demonstrated below:
 
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr

SQL> set serverout on 5000
SQL>
SQL> DECLARE
  2    i NUMBER;
  3    k VARCHAR2(3);
  4    TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  5    vt_List t_StringHashTable;
  6  BEGIN
  7    i := 1;
  8    k := to_char(i, '000');
  9    vt_List(k) := 'Test item...';
 10    dbms_output.put_line(vt_List(k));
 11  END;
 12  /

DECLARE
  i NUMBER;
  k VARCHAR2(3);
  TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  vt_List t_StringHashTable;
BEGIN
  i := 1;
  k := to_char(i, '000');
  vt_List(k) := 'Test item...';
  dbms_output.put_line(vt_List(k));
END;

ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 8

SQL> DECLARE
  2    i NUMBER;
  3    k VARCHAR2(1000);
  4    TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  5    vt_List t_StringHashTable;
  6  BEGIN
  7    i := 1;
  8    k := to_char(i, '000');
  9    dbms_output.put_line(length(k));
 10    dbms_output.put_line('k is "' || k || '"');
 11    vt_List(k) := 'Test item...';
 12    dbms_output.put_line(vt_List(k));
 13  END;
 14  /

4
k is " 001"

DECLARE
  i NUMBER;
  k VARCHAR2(1000);
  TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  vt_List t_StringHashTable;
BEGIN
  i := 1;
  k := to_char(i, '000');
  dbms_output.put_line(length(k));
  dbms_output.put_line('k is "' || k || '"');
  vt_List(k) := 'Test item...';
  dbms_output.put_line(vt_List(k));
END;

ORA-06502: PL/SQL: numeric or value error: associative array key violates its type constraints
ORA-06512: at line 11

SQL> DECLARE
  2    i NUMBER;
  3    k VARCHAR2(3);
  4    TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  5    vt_List t_StringHashTable;
  6  BEGIN
  7    i := 1;
  8    k := TRIM(to_char(i, '000'));
  9    vt_List(k) := 'Test item...';
 10    dbms_output.put_line(vt_List(k));
 11  END;
 12  /

Test item...

PL/SQL procedure successfully completed

SQL> DECLARE
  2    i NUMBER;
  3    TYPE t_StringHashTable IS TABLE OF VARCHAR2(256) INDEX BY VARCHAR2(3);
  4    vt_List t_StringHashTable;
  5  BEGIN
  6    i := 1;
  7    vt_List(i) := 'Test item...';
  8    dbms_output.put_line(vt_List(i));
  9  END;
 10  /

Test item...

PL/SQL procedure successfully completed

SQL>