Saturday, January 8, 2011

How to make INVALID OLAP Catalog VALID

Problem Description
The status of OLAP Catalog in dba_registry is invalid. You can check the status of OLAP components in dba_registry using the following query:
SQL> col comp_name for a20
SQL> select comp_name, status, version
  2  from DBA_REGISTRY
  3  where comp_name= 'OLAP Catalog';

COMP_NAME            STATUS        VERSION
-------------------- -----------   -----------------
OLAP Catalog         INVALID       10.2.0.3.0

Cause of the Problem
The problem happened due to either Oracle database upgrade bug or OLAP Option has not been installed properly.

Solution of the Problem
1. Try re-compiling the Invalid Objects by running the following SQL script:
SQL> @$ORACLE_HOME/rdbms/admin/utlrp.sql
Then, check the status of OLAP Catalog in dba_registry by running following script:
SQL> select COMP_ID,COMP_NAME,VERSION,STATUS from dba_registry where COMP_NAME LIKE '%OLAP%';

2. If Step 1 does not make OLAP Catalog Valid, then run the following scripts:
SQL> SPOOL olap_validate.log
SQL> @$ORACLE_HOME/olap/admin/amdrelod.sql 
SQL> @$ORACLE_HOME/rdbms/admin/utlrp.sql 
SQL> SPOOL OFF
After you run these scripts review the spool output olap_validate.log file to check whether there are any errors while executing the script.

3. Check for the invalid OLAP objects under SYS schema which are valid under OLAPSYS schema by running the following query and delete these invalid OLAP objects from SYS schema.
SQL> select a.object_name,a.object_type,a.status "SYS",b.status "OLAPSYS" 
from dba_objects a, dba_objects b 
where a.owner = 'SYS' 
and b.owner = 'OLAPSYS' 
and a.object_name = b.object_name 
and a.object_type = b.object_type 
and a.status <> 'VALID' 
and b.status <> 'INVALID' 
order by a.status;
4. Verify the status of OLAP Catalog in dba_registry as follows:
SQL> select COMP_ID,COMP_NAME,VERSION,STATUS from dba_registry where COMP_NAME LIKE '%OLAP%;

Friday, January 7, 2011

Implementing Object-Relational and Nested Relational Data Bases

Scenario 1:  Employees, Departments and Projects

In a certain Company, Employees work on Projects within Departments.  We could set up a single, relational table to store information on who does what and where; or on the other hand it might be more useful to nest the information about Projects and Departments as an attribute of Employees, or to nest the information about Employees and Departments as an attribute of Projects.

The script file Epd.sql, available to download, contains SQL statements to set up the “EPD” data base in each of these formats.  Further statements reconstruct the relational table from the nested-relational ones.  Download this file and read it using Notepad, and try to understand what the various statements mean and how they relate to each other.

Epd.sql
SET VERIFY ON
SET ECHO ON
DROP TABLE EPD
/
DROP TABLE EMP_PD
/
DROP TYPE PD
/
DROP TABLE Proj_ED
/
DROP TABLE ED_Table
/
DROP TYPE ED
/

CREATE TABLE EPD
    (EMP_NO         CHAR(2),
     PROJ_NO        CHAR(2),
     DEPT_NO        CHAR(2),
     CONSTRAINT     PKEY_EPD PRIMARY KEY (EMP_NO, PROJ_NO, DEPT_NO)
)
/
INSERT INTO EPD
    VALUES ('E1', 'P1', 'D1')
/
INSERT INTO EPD
    VALUES ('E1', 'P2', 'D1')
/
INSERT INTO EPD
    VALUES ('E2', 'P1', 'D1')
/
INSERT INTO EPD
    VALUES ('E2', 'P5', 'D3')
/
INSERT INTO EPD
    VALUES ('E4', 'P4', 'D6')
/
INSERT INTO EPD
    VALUES ('E5', 'P4', 'D6')
/
INSERT INTO EPD
   VALUES ('E6', 'P4', 'D7')
/
INSERT INTO EPD
    VALUES ('E9', 'P4', 'D7')
/
INSERT INTO EPD
    VALUES ('E5', 'P3', 'D9')
/
INSERT INTO EPD
    VALUES ('E7', 'P3', 'D9')
/

COMMIT;

SELECT * FROM EPD;


PAUSE 'Press Enter to Continue .... '

REM  Perhaps some of these values should be nested.
REM  Consider the projects and departments per employee.


CREATE TYPE PD AS OBJECT (
    PROJ_NO CHAR(2),
    DEPT_NO CHAR(2)  );
/

CREATE TYPE PD_Table AS TABLE OF PD ;
/

CREATE TABLE Emp_PD (
    EMP_NO CHAR(2),
    Set_of_PD PD_Table )
NESTED TABLE Set_of_PD  STORE AS  PDTable
/

INSERT INTO Emp_PD VALUES (
 'E1', PD_Table( PD( 'P1', 'D1' ),
                 PD( 'P2', 'D1' )  )  )
/
INSERT INTO Emp_PD VALUES (
 'E2', PD_Table( PD( 'P1', 'D1' ),
                 PD( 'P5', 'D3' )  )  ) 
/

INSERT INTO Emp_PD VALUES (
 'E4', PD_Table( PD( 'P4', 'D6' )  )  )
/
INSERT INTO Emp_PD VALUES (
 'E5', PD_Table( PD( 'P4', 'D6' ),
   PD( 'P3', 'D9' )  )  )
/

INSERT INTO Emp_PD VALUES (
 'E6', PD_Table( PD( 'P4', 'D7' )  )  )
/
INSERT INTO Emp_PD VALUES (
 'E9', PD_Table( PD( 'P4', 'D7' )  )  ) 
/
INSERT INTO Emp_PD VALUES (
 'E7', PD_Table( PD( 'P3', 'D9' )  )  )
/

COMMIT;

COLUMN SET_OF_PD FORMAT A50

SELECT * FROM Emp_PD;

PAUSE 'PRESS ENTER TO CONTINUE ... '

REM Reconstructing the orignal EPD Relation using Unnesting

SELECT  E.Emp_no, PD.Proj_no, PD.Dept_no
FROM Emp_PD E, Table (E.Set_of_PD) PD
/

PAUSE 'PRESS ENTER TO CONTINUE ... '

SELECT Set_of_PD FROM Emp_PD
WHERE EMP_NO =  'E2' ;

PAUSE 'PRESS ENTER TO CONTINUE ... '

REM  On the other hand !
REM  Consider the employees and departments per project.

CREATE TYPE ED AS OBJECT (
    EMP_NO CHAR(2),
    DEPT_NO CHAR(2)  );
/

CREATE TYPE ED_Table AS TABLE OF ED ;
/

CREATE TABLE Proj_ED (
    PROJ_NO CHAR(2),
    Set_of_ED ED_Table )
NESTED TABLE  Set_of_ED  STORE AS  EDTable
/

INSERT INTO Proj_ED VALUES (
 'P1', ED_Table( ED( 'E1', 'D1' ),
                 ED( 'E2', 'D1' )  )  )
/

INSERT INTO Proj_ED VALUES (
 'P2', ED_Table( ED( 'E1', 'D1' )  )  )
/

INSERT INTO Proj_ED VALUES (
 'P5', ED_Table( ED( 'E2', 'D3' )  )  )
/

INSERT INTO Proj_ED VALUES (
 'P4', ED_Table( ED( 'E4', 'D6' ),
                 ED( 'E5', 'D6' ),
   ED( 'E6', 'D7' ),
                 ED( 'E9', 'D7' )  )  )
/

INSERT INTO Proj_ED VALUES (
 'P3', ED_Table( ED( 'E5', 'D9' ),
                 ED( 'E7', 'D9' )  )  ) 
/

COMMIT;

COLUMN SET_OF_ED FORMAT A50

SELECT * FROM Proj_ED;

PAUSE 'PRESS ENTER TO CONTINUE ... '

REM Reconstructing the orignal EPD Relation using Unnesting

SELECT ED.emp_no, P.proj_no, ED.dept_no
FROM Proj_ED P, TABLE(P.Set_of_ED) ED
/

PAUSE 'PRESS ENTER TO CONTINUE ... '

SELECT Set_of_ED FROM Proj_ED
WHERE PROJ_NO = 'P4'
/



Then, run the script.  Note that the SQL is interspersed with SQL*Plus PAUSE commands, so that you can examine the output from various SELECT statements before pressing ENTER to continue running the script.

Scenario 2:  Points and Lines

In co-ordinate geometry, any point is defined in terms of its distance from a fixed point (the origin) along two lines or axes (denoted as X and Y) which are orthogonal (at right angles) to each other.  Thus we could define a data type to represent Points, having two numeric attributes to represent the X and Y values.

Any finite straight line connects the points at its two ends, leading to a further type definition.  Moreover, we can apply Pythagoras’ Theorem to calculate the length of such a line from the co-ordinates of its end-points.

The script file NestedLines.sql contains type definitions of PointType and LineType to implement these ideas, with length being defined as a Member Function of LineType.  It goes on to create a table of Lines (which has Points nested within it because of the way the data types have been set up) and provides further SQL statements to demonstrate how such a table can be used.

As in Scenario 1, download the script file, then read and run it to try to understand what is going on.

An alternative, object-relational approach involves giving each Point object a unique identifying number (Object ID).  Then a Lines2 table can be set up containing, not nested points, but references to points (which may thus be shared between different Line tuples).  In order to obtain meaningful information about the points of a line, it is then necessary to dereference the Object IDs.  All of this is implemented in the script ObjRelLines.sql.  Again, you should download, read and then run this script.
ObjRelLines.sql
SET VERIFY ON
SET ECHO ON

REM  REMOVING EXISTING TYPES AND TABLES

DROP TABLE Points CASCADE CONSTRAINTS PURGE
/
DROP  TABLE Lines2 CASCADE CONSTRAINTS PURGE
/
DROP TYPE PointTP FORCE
/
REM CREATING NEW TYPES AND TABLES

CREATE TYPE PointTP AS OBJECT ( 
 id integer,
 x NUMBER, 
 y NUMBER ); 
/ 

CREATE TABLE Points OF PointTP (Primary Key (id))
/

CREATE TABLE Lines2 ( 
 end1 REF PointTP,
        end2 REF PointTP
 )
/ 

REM Creating Points and Lines

Insert Into Points values (100, 0.0, 0.0);
Insert Into Points values (101, 2.3, 1.2);
Insert Into Points values (102, 1.3, 1.4);
Insert Into Points values (103, 4.0, 3.0);

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 100 and p2.id = 101;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 100 and p2.id = 102;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 100 and p2.id = 103;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 101 and p2.id = 100;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 101 and p2.id = 102;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 101 and p2.id = 103;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 102 and p2.id = 100;

INSERT INTO Lines2 
 SELECT REF(p1), REF(p2)
 FROM Points p1, Points P2
 WHERE p1.id = 103 and p2.id = 102;

REM QUERIES INVOLVING DEREFRENCING OBJECTS and getting attribute values

SELECT ll.end1.x, ll.end1.y, ll.end2.x, ll.end2.y
FROM Lines2 ll;

PAUSE 'PRESS ENTER TO CONTINUE ... '

REM YOU WILL SEE THAT USING OBJECT REFS, ENABLES OBJECT SHARING
REM NESTED TABLES DOES NOT ALLOW OBJECT SHARING

column deref(end1) format a30
column deref(end2) format a30

REM QUERIES INVOLVING DEREFRENCING OBJECTS using DEREF

select deref(end1), deref(end2)
from lines2;




For your convenience, copies of the output from all of the SELECT queries are contained in the file Example_Queries_Output.txt.


Database Modelling - Exercises PL/SQL Procedures using Cursors

Aims:

To demonstrate the use Cursors in PL/SQL Procedures.

Tables and Data for Employee Table:

Run the following scripts in an Oracle 10g session using SQL Plus before you do any of the tasks:

EMP.SQL: Copy the contents of the tables and data from below. It creates EMPLOYEE table and inserts some sample data into it.
EMP.SQL
SET VERIFY ON
SET FEEDBACK ON

Drop table EMPLOYEE cascade constraints purge;

SET ECHO ON

create table EMPLOYEE
 (Empid     number,
 Empname    varchar2(30),
 Empgrade   number,
 Empsalary  number,
 primary key (empid));

 REM POPULATING THE TABLE

 insert into  EMPLOYEE values (1, 'Paul', 1, 10000);
 insert into  EMPLOYEE values (2, 'Linda', 1, 11000);
 insert into  EMPLOYEE values (3, 'Natasha', 2, 14000);
 insert into  EMPLOYEE values (4, 'Julia', 2, 15000);
 insert into  EMPLOYEE values (5, 'Emanuel', 3, 20000);
 insert into  EMPLOYEE values (6, 'Vector', 3, 21000);
 insert into  EMPLOYEE values (7, 'Peter', 4, 215000);
 insert into  EMPLOYEE values (8, 'Sandra', 1, 11500);
 insert into  EMPLOYEE values (9, 'Pedro', 2, 16000);
 insert into  EMPLOYEE values (10, 'Norman', 4, 22000);
 insert into  EMPLOYEE values (11, 'Alvaro', 5, 25000);
 insert into  EMPLOYEE values (12, 'Jemma', 5, 26000);
 insert into  EMPLOYEE values (13, 'Bushy', 4, 23000);
 insert into  EMPLOYEE values (14, 'Amir', 1, 11500);


 SELECT * FROM EMPLOYEE;

 COMMIT;



Scenario:


Your task is to create a procedure which, when executed, will increase the salary for an employee by £500 or £1000 depending on his or her grade (the higher the grade, the higher the employee’s ranking within the company).  The code given below provides a skeleton for the procedure, but you will need to fill in the missing bits indicated by bold letters.  You are recommended to use Notepad to do the editing, and you will need to give your file the extension .sql, e.g., calling it myprocedure.sql.  Once you have done this, you should attempt to compile the code by means of the @ command (using the full path-name of the file) in SQL*Plus.   If Oracle tells you that the procedure has been compiled with errors, entering the command show errors will list the problems with the code.   Once the procedure has been compiled successfully, you can run it by typing execute salary_increase at the SQL> prompt.

NOTES:
Remember that you need to have a variable to store each component of the return from the cursor. Where an IF statement is used, remember to END IF;

The salaries for the grades are listed overleaf:

Grade
Salary Range
1
10000-15000
2
14000-19500
3
19000-22000
4
21500-25500
5
25000-30000



Your Tasks:

Task 1: Complete the code below, including a cursor which will select the id, grade, and salary for each employee.   You should then use this information to check the grade and increase the salary accordingly.  The criteria for increase are given in the pseudo code below:


CREATE OR REPLACE PROCEDURE salary_increase AS
/* Declare the cursor and variables here without  the DECLARE keyword*/
BEGIN
           

            /* Open the cursor */

           

            LOOP

                FETCH                             /* complete the fetch command */
                EXIT WHEN cursor_name%NOTFOUND; /*this will exit when cursor empty*/

                /* Check using if statement:  if grade is < 4 increase salary by 500
                   Check using if statement:  if grade is > 3 increase salary by 1000 */
           
            END LOOP;
END;
/


Once this is completed, compile the code and correct any errors that are noted.

Execute the procedure and note the changes to the salary.

Task 2: Amend the code so that if the salary increase would take the salary above the maximum for that grade, the salary is altered to the maximum and not increased above it.

(NOTE: you may wish to create a Grade table for this task).

Database Modelling - Exercises on PL/SQL Procedures

Aims:
To create and execute a procedure.

Application Tables and Data for Patient Data:
Run the following scripts in an Oracle 10g session using SQL Plus.

Tables.sql: It is a script file containing table definitions in SQL for a patients database including patient, doctor, visits, vaccinations, valid for. Script is following.
set echo on

/* echoes input */

/* drop tables in case exist already */

drop table valid_for cascade constraints; 
drop table vaccinations cascade constraints;
drop table patient cascade constraints;
drop table visits cascade constraints;
drop table doctor cascade constraints;

/* create the base tables */

/* again order matters, foreign keys point at existing tables */

create table patient (
pid char(6) constraint pkp primary key,
pname char(20),
address varchar2(100),
dobirth date,
date_reg date);

create table doctor (
did char(1) constraint pkd primary key,
dname char(20),
date_start date);

create table valid_for (
vaccinated char(20) constraint pkvf primary key,
lasting_years number); 

create table visits (
pid char(6),
did char(1),
vdate date,
constraint pkvis primary key (pid,vdate),
foreign key(pid) REFERENCES patient(pid),
foreign key(did) REFERENCES doctor(did)
);

create table vaccinations (
pid char(6),
vdate date,
action number,
vaccinated char(20),
constraint pkvac primary key (pid,vdate,action),
foreign key(pid,vdate) REFERENCES visits(pid,vdate),
foreign key(pid) REFERENCES patient(pid),
foreign key(vaccinated) REFERENCES valid_for(vaccinated)
);

commit;

/* end of script */

Data.sql: It is a script file containing data to load into the tables. Following is the contents of Data.sql script.
insert into patient values('1','Fred','Newcastle','14-mar-1973','29-sep-2002');
insert into patient values('2','Mary','Heaton','28-oct-1986','06-aug-1991');
insert into patient values('3','Susan','Tynemouth','07-feb-1935','01-jan-1970');
insert into patient values('4','Bill','Gosforth','03-jan-1987','18-jul-1991');

insert into doctor values ('1','Peter Roberts','25-apr-1995');
insert into doctor values ('2','Brenda Townsend','06-sep-1998');

insert into visits values('1','1','04-dec-2002');
insert into visits values('2','2','06-aug-1996');
insert into visits values('2','2','04-mar-1999');
insert into visits values('2','2','27-jul-2002');
insert into visits values('2','2','16-dec-2004');
insert into visits values('4','1','22-jul-1996');
insert into visits values('4','1','26-jun-1998');
insert into visits values('4','2','30-jun-1998');
insert into visits values('4','2','09-jul-2002');
insert into visits values('4','1','30-jun-2004');

insert into valid_for values('smallpox',10);
insert into valid_for values('typhoid',3);
insert into valid_for values('cholera',0.5);
insert into valid_for values('polio',10);
insert into valid_for values('tetanus',7);
insert into valid_for values('hepatitis',0.5);

insert into vaccinations values('1','04-dec-2002',1,'smallpox');
insert into vaccinations values('1','04-dec-2002',2,'typhoid');
insert into vaccinations values('2','06-aug-1996',1,'typhoid');
insert into vaccinations values('2','06-aug-1996',2,'cholera');
insert into vaccinations values('2','06-aug-1996',3,'polio');
insert into vaccinations values('2','04-mar-1999',1,'typhoid');
insert into vaccinations values('2','27-jul-2002',1,'typhoid');
insert into vaccinations values('2','27-jul-2002',2,'tetanus');
insert into vaccinations values('2','16-dec-2004',1,'typhoid');
insert into vaccinations values('2','16-dec-2004',2,'hepatitis');
insert into vaccinations values('4','22-jul-1996',1,'typhoid');
insert into vaccinations values('4','22-jul-1996',2,'cholera');
insert into vaccinations values('4','26-jun-1998',1,'tetanus');
insert into vaccinations values('4','30-jun-1998',1,'typhoid');
insert into vaccinations values('4','09-jul-2002',1,'typhoid');
insert into vaccinations values('4','09-jul-2002',2,'hepatitis');
insert into vaccinations values('4','30-jun-2004',1,'cholera');
insert into vaccinations values('4','30-jun-2004',2,'typhoid');

commit;

/* end of script */

Your Tasks:

A. Create in a text file a procedure to add data to the vaccinations table
with all data values read as parameters and an exception section

B. Compile this procedure in SQL*Plus.

C. Execute the procedure in SQL*Plus with at least four different sets of
data.

(i) a set that is acceptable to the system

(ii) three further sets that generate different kinds of errors, e.g. primary key violations, foreign key violations, datatype errors etc.

D. Comment on the reasons for the errors, suggesting ways in which the
messages could be made more useful to the end-user.

E. Save the results of the four executions in a spool file.

Exercise 7: Relational Algebra and its Simulation using SQL

nPurpose
nTo understand how some of the relational operations are implemented in SQL.
nTo query a relational database using relational algebra operations / expressions.
nTo compare the actual output produced by an SQL interpreter from a database with the result of applying relational algebra operations to the relational database.
nScenario
nThe Company database contains data about the departments with their employees, as well as for the department projects with the employees working on them.
nThe set of relations in the Company relational schema is provided on the Figure 1.
nData for one particular instance of database Company is provided on Figure 2.
nThe SQL script for creating of relational database with the above test data preloaded is provided in the file Company.sql which you can get from later of this post.

Company Relational Schema
nDEPT: stores data about company departments, including their managers
nEMP: stores data about people working in the company
nPROJ: stores data about the projects on which the company currently works
nALLOC: stores data about the people allocated to different projects

Figure 1

Database Schema Instance


Your Job
1)Write the following queries against Company database using both relational algebra operators first and then using SQL
i)Get a list of department number (i.e., DEPT_NO) of those departments which have projects or employees.
ii)Get a list of department number of those departments which have both projects and employees.
iii)Get a list of department number of those departments that have projects but no employees are working on them.
iv)Get information on all possible combinations of employees and departments.
v)Find information on those employees and their departments who are working for them but are not their managers.
vi)Find information about employees who are currently working on projects.
vii)Find information about the projects of the departments
viii)Part (vii) including those projects which are not yet assigned to any department.
ix)Part (viii) including departments without registered projects.

2)Connect to the Oracle database using SQL*Plus, load the Company database contained in the file Company.sql (downloadable from Blackboard) and execute the SQL scripts you have written against it.

- Connect to the Oracle database using SQL*Plus

- Load the Company database, e.g.
@C:\CM0719\Company.sql

- Execute the queries

Execute your own queries in SQL which implement exactly the same algebra queries as the ones written in the algebra before.

- Compare the results

Why the results received as an output of SQL interpreter differ from the expected results in the case of applying algebraic operations on the relational database (if they really differ)?

Get the Company.sql schema from http://arjudba.blogspot.com/2011/01/company-schema-for-simulation-sql.html

A company schema for simulation SQL exercise

SET VERIFY ON

Rem Setup File for SQL double unit

Rem Running this file will empty the data base that is left over from other units 
Rem Invoke this file from the SQL window by typing @COMPANY

SET ECHO ON

Rem Everything must first be dropped from the data base.  If an object doesn't
Rem exist, there will be an error message, but this can be ignored.

DROP TABLE EMP CASCADE CONSTRAINTS;
DROP TABLE DEPT CASCADE CONSTRAINTS;
DROP TABLE PROJ CASCADE CONSTRAINTS;
DROP TABLE ALLOC CASCADE CONSTRAINTS;

Rem CASCADE CONSTRAINTS is necessary to avoid violating any formal integrity
Rem constraints on the tables.

Rem Now we can re-create the data base:

CREATE TABLE EMP 
    (EMP_NO         CHAR(2)
         CONSTRAINT PKEY_EMP PRIMARY KEY ,
     EMP_NAME       CHAR(10),
     DEPT_NO     CHAR(2),
     SALARY         INTEGER
         CONSTRAINT SAL_RANGE CHECK (SALARY BETWEEN 6000 AND 30000) ,
     MARITAL_STATUS CHAR(1)
         CONSTRAINT MAR_STATS CHECK (MARITAL_STATUS IN ('S', 'M', 'W', 'D')) 
     );

CREATE TABLE DEPT
    (DEPT_NO        CHAR(2)
         CONSTRAINT PKEY_DEPT PRIMARY KEY ,
     DEPT_NAME      CHAR(10)
         CONSTRAINT UNIQ_NAME UNIQUE ,
     MANAGER_NO     CHAR(2)
         CONSTRAINT UNIQ_MANAGER UNIQUE,
     BUDGET         INTEGER
         CONSTRAINT MAX_BUDG CHECK (BUDGET <= 400000) );

CREATE TABLE PROJ
    (PROJ_NO        CHAR(2)
         CONSTRAINT PKEY_PROJ PRIMARY KEY ,
     DEPT_NO        CHAR(2),
     START_DATE     DATE
         CONSTRAINT UNIQ_START UNIQUE , 
     DEADLINE       DATE,
     CONSTRAINT DURATION CHECK (TO_DATE (START_DATE, 'DD-MON-YYYY') < 
                                TO_DATE (DEADLINE, 'DD-MON-YYYY')) ); 

CREATE TABLE ALLOC
    (EMP_NO         CHAR(2),
     PROJ_NO        CHAR(2),
     CONSTRAINT     PKEY_ALLOC PRIMARY KEY (EMP_NO, PROJ_NO) );

INSERT INTO EMP
    VALUES ('E1', 'Smith', 'D1', 9900, 'W');
INSERT INTO EMP
    VALUES ('E2', 'Jones', 'D2', 13200, 'M');
INSERT INTO EMP
    VALUES ('E3', 'Roberts', 'D2', 11000, 'M');
INSERT INTO EMP
    VALUES ('E4', 'Evans', 'D3', 16500, 'S');
INSERT INTO EMP
    VALUES ('E5', 'Brown', 'D3', 27500, 'S');
INSERT INTO EMP
    VALUES ('E6', 'Green', 'D3', 13200, 'M');
INSERT INTO EMP
    VALUES ('E7', 'McDougal', 'D4', 17600, 'D');
INSERT INTO EMP
    VALUES ('E8', 'McNally', 'D5', 12100, 'M');
INSERT INTO EMP
    VALUES ('E9', 'Fletcher', 'D5', 13200, 'S');

INSERT INTO DEPT
    VALUES ('D1', 'Production', NULL, 100000);
INSERT INTO DEPT
    VALUES ('D2', 'Sales', 'E5', 250000);
INSERT INTO DEPT
    VALUES ('D3', 'Accounts', 'E9', 95000);
INSERT INTO DEPT
    VALUES ('D4', 'Admin', 'E8', 156000);
INSERT INTO DEPT
    VALUES ('D5', 'Personnel', 'E7', 196000);

INSERT INTO PROJ
    VALUES ('P1', 'D1', '20-APR-1994', '23-FEB-1998');
INSERT INTO PROJ
    VALUES ('P2', 'D1', '21-JAN-1995', '14-MAY-1997');
INSERT INTO PROJ
    VALUES ('P3', 'D2', '02-FEB-1996', '03-MAR-1999');
INSERT INTO PROJ
    VALUES ('P4', 'D3', '11-DEC-1995', '01-JAN-1999');
INSERT INTO PROJ
    VALUES ('P5', 'D4', '08-OCT-1995', '31-DEC-1999');
INSERT INTO PROJ
    VALUES ('P6', NULL, '09-OCT-1995', '30-DEC-1999');


INSERT INTO ALLOC
    VALUES ('E1', 'P1');
INSERT INTO ALLOC
    VALUES ('E1', 'P2');
INSERT INTO ALLOC
    VALUES ('E2', 'P1');
INSERT INTO ALLOC
    VALUES ('E2', 'P5');
INSERT INTO ALLOC
    VALUES ('E4', 'P4');
INSERT INTO ALLOC
    VALUES ('E5', 'P4');
INSERT INTO ALLOC
    VALUES ('E6', 'P4');
INSERT INTO ALLOC
    VALUES ('E9', 'P4');
INSERT INTO ALLOC
    VALUES ('E5', 'P3');
INSERT INTO ALLOC
    VALUES ('E7', 'P3');

ALTER TABLE EMP   ADD CONSTRAINT FKEY_DEPT  FOREIGN KEY (DEPT_NO) REFERENCES DEPT (DEPT_NO) DEFERRABLE;
ALTER TABLE DEPT  ADD CONSTRAINT FKEY_EMP   FOREIGN KEY (MANAGER_NO) REFERENCES EMP (EMP_NO) DEFERRABLE; 
ALTER TABLE PROJ  ADD CONSTRAINT FKEY_PROJ  FOREIGN KEY (DEPT_NO) REFERENCES DEPT (DEPT_NO) DEFERRABLE;
ALTER TABLE ALLOC ADD CONSTRAINT ALLOC_EMP  FOREIGN KEY (EMP_NO)  REFERENCES EMP (EMP_NO) DEFERRABLE;
ALTER TABLE ALLOC ADD CONSTRAINT ALLOC_PROJ FOREIGN KEY (PROJ_NO) REFERENCES PROJ (PROJ_NO) DEFERRABLE;

COMMIT;

SET ECHO OFF

PROMPT
PROMPT You are now ready to proceed with the SQL exercises.
PROMPT

During 9.2 to 10.2g database upgrade ORA-00600 [qmtGetColumnInfo1] found

Problem Description
While upgrading a database from 9.2 to 10.2g during the XML Datababase (XDB) Upgrade, the following errors are reported in the database alert.log file or upgrade logfile.
declare 
* 
ERROR at line 1: 
ORA-31159: XML DB is in an invalid state 
ORA-00600: internal error code, arguments: [qmtGetColumnInfo1], [], [], [], [], [], [], []
ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 3 
ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 12 
ORA-06512: at line 143 

Cause of the Problem
The environment variable LD_LIBRARY_PATH (SHLIB_PATH for HP-UX or LIBPATH for AIX) is set to a wrong value while running the upgrade scripts, that is pointing to the old 9.2 ORACLE_HOME.

Solution of the Problem
Make sure you have set LD_LIBRARY_PATH, SHLIB_PATH or LIBPATH to the correct values before performing the database upgrade.
Set LD_LIBRARY_PATH so the first directory referenced is $ORACLE_HOME/lib.
For AIX, set LIBPATH so the first directory referenced is $ORACLE_HOME/lib Example (replace $ORACLE_HOME with the full path of the Oracle home directory):

csh:
setenv LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (LINUX)
setenv LIBPATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (AIX)
setenv SHLIB_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (HPUX)

ksh:

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (LINUX)
export LIBPATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (AIX)
export SHLIB_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH (HPUX)

Wednesday, January 5, 2011

ORA-31159: XML DB is in an invalid state ORA-00600 [qmx: no ref]

Problem Description
There is a complain from the user end that they are hitting this error, "ORA-31159: XML DB is in an invalid state" and I saw by querying,
SQL> select comp_name, status, version from DBA_REGISTRY where comp_name= 'Oracle XML Database';

COMP_NAME
--------------------------------------------------------------------------------
STATUS VERSION
-------------------------------------------- ------------------------------
Oracle XML Database
VALID 11.1.0.7.0

And,
SQL> select count(*) from dba_objects where owner='XDB' and status='INVALID';

COUNT(*)
----------
0
there does not exist any invalid objects under XDB schema.
Again I told user to try access database and now they reponded whenever they try to access tables with XMLTYPE data type columns we get : ORA-00600: internal error code, arguments: [qmx: no ref], [], [], [], [], [], [], [], [], [], [], []

I also see from alert log there is an entry,
Errors in file /u01/diag/rdbms/bddipdc/bddipdc/trace/bddipdc_ora_950348.trc  (incident=36368): 
ORA-00600: internal error code, arguments: [qmx: no ref], [], [], [], [], [], [], [], [], [], [], [] 
Errors in file /u01/diag/rdbms/bddipdc/bddipdc/trace/bddipdc_ora_950348.trc: 
ORA-31159: XML DB is in an invalid state

The trace file shows that the failing sql (Current SQL statement) involves XDB, examples
BEGIN 
   xdb.dbms_xdbz.enable_hierarchy ('XDB', 'XDB$SCHEMA'); 
   xdb.dbms_xdbz.disable_hierarchy('XDB', 'XDB$SCHEMA'); 
END;
declare 
  XMLNSXSD BFILE := dbms_metadata_hack.get_bfile('xmlabc.xsd.11.0'); 
  XMLNSURL VARCHAR2(2000) := 'http://www.w3.org/2001/abcxml.xsd'; 
begin 
  xdb.dbms_xmlschema.registerSchema(XMLNSURL, XMLNSXSD, FALSE, FALSE, FALSE, 
                    TRUE, FALSE, 'XDB', 
                                   options=>DBMS_XMLSCHEMA.REGISTER_BINARYXML); 
end;
Stack trace is similar to
skdstdst <- ksedst1 <- ksedst <- dbkedDefDump <- ksedmp 
<- qm_init_uga_helper <- qm_run_as_ksys <- qm_init_uga 
Cause of the Problem
ORA-00600 is oracle bug and this bug fires due to the incorrect setting of the LD_LIBRARY_PATH environment variable. (For AIX, the environment variable involved is LIBPATH, while in HP-UX, it is SHLIB_PATH and LD_LIBRARY_PATH.) This variable needs to be set such that the first directory referenced is the database's $ORACLE_HOME/lib. Most importatntly, this variable needs to be correctly set prior to each database startup, i.e. the current setting may be correct, but this needs to be correct when the database was started up as it determines which library files are loaded. Secondly, if a client connects to an 11g instance via a 10g listener, the LD_LIBRARY_PATH need to be added to the listener.ora.

Solution of the Problem
Step 01: Shutdown the database and listener.
SQL> conn / as sysdba
SQL> shutdown immediate
SQL> lsnrctl stop
Step 02: Set LD_LIBRARY_PATH so the first directory referenced is $ORACLE_HOME/lib. For AIX, set LIBPATH so the first directory referenced is $ORACLE_HOME/lib Example (replace $ORACLE_HOME with the full path of the Oracle home directory): csh:
setenv LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (LINUX)
setenv LIBPATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (AIX)
setenv SHLIB_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (HPUX)

ksh: 

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (LINUX)
export LIBPATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (AIX)
export SHLIB_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32 (HPUX)
Step 03: (Only applicable for AIX)
You need to remove any currently unused modules in the kernel and library memory by, running /usr/sbin/slibclean as root user. # /usr/sbin/slibclean

Step 04: (Only applicable if database uses 10g listener)
If a client connects to an 11g instance via a 10g listener, modify and add the ENVS= "LD_LIBRARY_PATH" specificaton to the listener.ora:
SID_LIST_LISTENER = 
 (SID_LIST = 
   (SID_DESC = 
     (SID_NAME = PLSExtProc) 
... 
 ) 
   (SID_DESC = 
     (SID_NAME = 11gSID ) 
     (ORACLE_HOME =/opt/oracle/product/11.1.0) 
     (ENVS= "LD_LIBRARY_PATH=/opt/oracle/product/11.1.0/lib") 
   ) 
 )
Step 05: Start the database and the listener.
SQL> conn / as sysdba
SQL> startup
SQL> host lsnrcrl start

Sunday, January 2, 2011

$_GET request does not show expected result but forwarded to homepage

Problem Description
Any $_GET request does not show expected result rather it forwarded to homepage of the site. For example I am in a subdirectory of a website and then I submit a form after filling the information. After I submit the form it is forwarded to homepage. More specifically, please open http://blastyourblog.com/seo-tool-04/alexacomparison.php and now I type two urls like http://arjudba.blogspot.com/ and http://arjudba.blogspot.com/ then it will display url as http://blastyourblog.com/seo-tool-04/alexacomparison.php?domains=arjudba.blogspot.com%0D%0Abanscam.blogspot.com&width=500&height=400>ype=t&tspan=3m and immediately it goes to homepage instead of showing result in that window. (Note that it is corrected now.) This is really pain problem as I see there is no code bug and also whenever I put the same script into home directory it works as usual.

Solution of the Problem
The problem happened due to my hosting server which is Hostgator in this case. According to hostgator http://blastyourblog.com/seo-tool-04/alexacomparison.php scenario was caused by a mod_security rule being flagged by the script. They said now your domain has been white listed for the mod_security rule and so it is functioning properly. So, if you face such type of problem in your server immediately contact to your hosting server instead of spending time in your end. They will edit the mod_security rule and then you will have no issue.

Happy New Year to Everyone

Happy new year to everyone. May this new year brings peace and happiness in your life.