16 December 2006

Creating(NOFORCE, WITH CHECK OPTION parameters) And Inserting Views In Oracle

Views are some kind of storing queries in database. If you have a big query and want use it elsewhere, you can create a view. Normally views do not permit to inserts via themselves. In Oracle it can be possible. Additional features of views in Oracle described below:

Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr


SQL> drop table t;

Table dropped
SQL> create table t(a number, z number);
Table created
SQL> create or replace view t_v as select a as aa,z as zz from t;
View created
SQL> select * from t_v;
        AA         ZZ
---------- ----------

SQL> insert into t_v values(1,2);
1 row inserted
SQL> insert into t_v values(2,3);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from t;
         A          Z
---------- ----------
         1          2
         2          3

SQL> select * from t_v;
        AA         ZZ
---------- ----------
         1          2
         2          3


SQL> create or replace  noforce view  no_existed_table_v  as select a as aa,z as zz from no_existed_table;

create or replace  noforce view  no_existed_table_v  as select a as aa,z as zz from no_existed_table
ORA-00942: table or view does not exist
SQL> create or replace  force view  no_existed_table_v  as select a as aa,z as zz from no_existed_table;
Warning: View created with compilation errors
SQL> select * from no_existed_table_v;
select * from no_existed_table_v
ORA-04063: view "HR.NO_EXISTED_TABLE_V" has errors

SQL> drop view t_v;

View dropped
SQL> create view t_v as select a as aa,z as zz from t with read only;
View created
SQL> insert into t_v values(1,2);
insert into t_v values(1,2)
ORA-01733: virtual column not allowed here

SQL> create or replace view t_v as select a as aa,z as zz from t where a = 1;

View created
SQL> SELECT * FROM t_v;
        AA         ZZ
---------- ----------
         1          2

SQL> insert into t_v values(3,4);
1 row inserted
SQL> create or replace view t_v as select a as aa,z as zz from t where a = 1 with check option constraint ck_v;
View created
SQL> insert into t_v values(3,4);
insert into t_v values(3,4)
ORA-01402: view WITH CHECK OPTION where-clause violation

03 December 2006

On Associative Arrays, Nested Tables and Varrays In Oracle

Oracle has three different types of collections. I mentioned them in one of my previus posts. You can take more information there.
Now, i want to give examples how to use them:


SQL> DECLARE
  2    TYPE occupation_table IS TABLE OF VARCHAR2(64) INDEX BY VARCHAR2(16);
  3    occupations occupation_table;
  4    k           VARCHAR2(16);
  5  BEGIN
  6    occupations('One') := 'Architecture';
  7    occupations('Two') := 'Engineer';
  8    k := occupations.FIRST;
  9    WHILE k IS NOT NULL LOOP
 10      dbms_output.put_line('occupations(''' || k || ''') is ' || occupations(k));
 11      k := occupations.NEXT(k);
 12    END LOOP;
 13  END;
 14  /

occupations('One') is Architecture
occupations('Two') is Engineer

PL/SQL procedure successfully completed
SQL>

2. Varrays
SQL> create type t_telephone_numbers is varray(2) of varchar2(10);
Type created
SQL> create table workers( name varchar2(16), telephone_list t_telephone_numbers );
Table created
SQL> INSERT INTO workers VALUES('Mennan',t_telephone_numbers('2122122122', '2122221133') );
1 row inserted
SQL> SELECT * FROM workers;
NAME             TELEPHONE_LIST
---------------- --------------
Mennan          
[object]
SQL> SELECT * FROM table(SELECT telephone_list FROM workers WHERE name = 'Mennan'  );
COLUMN_VALUE
------------
2122122122
2122221133

SQL>

3. Nested Tables
SQL> create type t_hobbies is table of varchar2(16);
  2  /

Type created
SQL> create table workers( name varchar2(16), hobbies t_hobbies )
  2    nested table hobbies store as workers_hobbies;

Table created
SQL> SELECT table_name, nested FROM user_tables WHERE table_name like 'WORKERS%';
TABLE_NAME                     NESTED
------------------------------ ------
WORKERS                        NO
WORKERS_HOBBIES                YES

SQL> INSERT INTO workers VALUES('Mennan',t_hobbies('Reading Book', 'Listening') );
1 row inserted
SQL> INSERT INTO workers VALUES('Ali',t_hobbies('Swimming', 'Coding', 'Football') );
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT * FROM workers;
NAME             HOBBIES
---------------- -------
Mennan           [object]
Ali             
[object]
SQL> SELECT * FROM workers_hobbies;
SELECT * FROM workers_hobbies
ORA-22812: cannot reference nested table column's storage table
SQL> SELECT * FROM table(SELECT hobbies FROM workers WHERE name = 'Mennan'  );
COLUMN_VALUE
----------------
Reading Book
Listening

SQL> INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') );
INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') )
ORA-12899: value too large for column "HR"."WORKERS_HOBBIES"."COLUMN_VALUE" (actual: 20, maximum: 16)
SQL> alter type t_hobbies modify element type varchar2(8) cascade;
alter type t_hobbies modify element type varchar2(8) cascade
ORA-22324: altered type has compilation errors
ORA-22328: object "HR"."T_HOBBIES" has errors.
PLS-00729: only widening of the collection element type is allowed
ORA-06550: line 0, column 0:
PL/SQL: Compilation unit analysis terminated

SQL> alter type t_hobbies modify element type varchar2(64) cascade;
Type altered
SQL> INSERT INTO workers VALUES('Ayse',t_hobbies('Reading Horror Books') );
1 row inserted
SQL> commit;
Commit complete
SQL> SELECT * FROM table(SELECT hobbies FROM workers WHERE name = 'Ayse'  );
COLUMN_VALUE
----------------------------------------------------------------
Reading Horror Books

SQL>

02 December 2006

Handling Exceptions With Bulk Operations In Oracle

When working large amount of data, you must consider exceptions. Suppose that a bulk operation that takes 100.000 of records and inserts it to a table. There may have some exceptions during insertion such as foreign key or not null constraints. If you did not consider exceptions, you can lose your time and do more work. One other assumption that can be, you can get an error on the last record. So all of your correct data can get waste.
Oracle solves this problems with some techniques. One of them is save bulk_exceptions in forall statement. Other is dbms_errlog package. I have described dbms_errlog package one of my older posts. (For performance analysis click here)
Demonstration below simple shows the cases above:

Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as SYS

SQL>
SQL> drop table t;

Table dropped

Create a table and add some constraints.
SQL> create table t(i number not null);
Table created
SQL> alter table t add constraint ck_i check( i < 2 );
Table altered

Make insertion with normal way. You will get an exception and all work will be rolled back.
SQL> DECLARE
  2    TYPE t_t IS TABLE OF NUMBER;
  3    n_t t_t;
  4  BEGIN
  5    SELECT decode(rownum, 2, NULL, rownum) BULK COLLECT
  6      INTO n_t
  7      FROM user_tables
  8     WHERE rownum < 4;
  9    FORALL i IN n_t.FIRST .. n_t.LAST
 10      INSERT INTO t VALUES( n_t (i) );
 11  END;
 12  /

DECLARE
  TYPE t_t IS TABLE OF NUMBER;
  n_t t_t;
BEGIN
  SELECT decode(rownum, 2, NULL, rownum) BULK COLLECT
    INTO n_t
    FROM user_tables
   WHERE rownum < 4;
  FORALL i IN n_t.FIRST .. n_t.LAST
    INSERT INTO t VALUES( n_t (i) );
END;

ORA-01400: cannot insert NULL into ("SYS"."T"."I")
ORA-06512: at line 9

SQL> SELECT * FROM t;
         I
----------

SQL> rollback;
Rollback complete

First technique is handling in forall statement. You will get correct records in table.
SQL> DECLARE
  2    TYPE t_t IS TABLE OF NUMBER;
  3    n_t t_t;
  4  BEGIN
  5    SELECT decode(rownum, 2, NULL, rownum) BULK COLLECT
  6      INTO n_t
  7      FROM user_tables
  8     WHERE rownum < 4;
  9    FORALL i IN n_t.FIRST .. n_t.LAST SAVE EXCEPTIONS
 10      INSERT INTO t VALUES( n_t (i) );
 11 
 12  EXCEPTION
 13    WHEN OTHERS THEN
 14      dbms_output.put_line( sqlerrm );
 15      FOR i IN 1 .. SQL%BULK_EXCEPTIONS.COUNT LOOP
 16        dbms_output.put_line(SQL%BULK_EXCEPTIONS(i)
 17                             .ERROR_INDEX || ' : ' ||
 18                             SQLERRM(-1 * SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
 19      END LOOP;
 20 
 21  END;
 22  /

ORA-24381: error(s) in array DML
2 : ORA-01400: cannot insert NULL into ()
3 : ORA-02290: check constraint (.) violated

PL/SQL procedure successfully completed
SQL> SELECT * FROM t;
         I
----------
         1

SQL> rollback;
Rollback complete

Second method is using supplied package dbms_errlog.
SQL> drop table err$_t;
Table dropped
SQL> BEGIN
  2    dbms_errlog.create_error_log('t');
  3  END;
  4  /

PL/SQL procedure successfully completed
SQL> DECLARE
  2    TYPE t_t IS TABLE OF NUMBER;
  3    n_t t_t;
  4  BEGIN
  5    SELECT decode(rownum, 2, NULL, rownum) BULK COLLECT
  6      INTO n_t
  7      FROM user_tables
  8     WHERE rownum < 4;
  9    FORALL i IN n_t.FIRST .. n_t.LAST
 10      INSERT INTO t VALUES( n_t (i) ) log errors reject LIMIT unlimited;
 11 
 12  END;
 13  /

PL/SQL procedure successfully completed
SQL> SELECT * FROM t;
         I
----------
         1


Errors are logged.
SQL> SELECT t.ora_err_number$ errno, t.ora_err_mesg$ errmess FROM err$_t t;
     ERRNO ERRMESS
---------- --------------------------------------------------------------------------------
      1400 ORA-01400: cannot insert NULL into ("SYS"."T"."I")
      2290 ORA-02290: check constraint (SYS.CK_I) violated