Monday, 22 April 2013

Write a PL/SQL block to display following series

1 1 2 3 5 8 ..... N

DECLARE
term1 number;
term2 number;
term3 number;
n number;
BEGIN
  n:=&n;
  term1:=1;
  term2:=0;
  term3:=1;
  FOR i in 1..n
  LOOP
    dbms_output.put_line(term3||' ');
    term1:=term2;
    term2:=term3;
    term3:=term1+term2;
  END LOOP;
END;

Saturday, 20 April 2013

Example of trigger:

There are three tables: studinfo, result and update_studinfo. If you insert data into studinfo then that data will be inserted into result with calculation automatically. If you update any data then new data will be inserted into result and old data will be inserted into update_studinfo. If you delete any data then old data will be inserted into update_studinfo table.

You can do that in following way:

create table studinfo
(studid varchar2(3),
name varchar2(15),
php number(3),
oracle number(3),
vb number(3));


create table result
(studid varchar2(3),
name varchar2(15),
php number(3),
oracle number(3),
vb number(3),
total number(3),
per number(6,2),
operation varchar2(7),
op_date date);


create table update_studinfo
(studid varchar2(3),
name varchar2(15),
php number(3),
oracle number(3),
vb number(3),
operation varchar2(7),
op_date date);


create or replace trigger trgstudinfo
before insert or update or delete
on studinfo
for each row
declare
total number;
per number(6,2);
begin
  if inserting then
    total:=:new.php+:new.oracle+:new.vb;
    per:=total/3;
    insert into result values(:new.studid,:new.name,:new.php,:new.oracle,:new.vb,total,per,'INSERT',sysdate);
  end if;

  if updating then
    total:=:new.php+:new.oracle+:new.vb;
    per:=total/3;
    insert into result values(:new.studid,:new.name,:new.php,:new.oracle,:new.vb,total,per,'UPDATE',sysdate);
    insert into update_studinfo values(:old.studid,:old.name,:old.php,:old.oracle,:old.vb,'UPDATE',sysdate);
  end if;

  if deleting then
     insert into update_studinfo values(:old.studid,:old.name,:old.php,:old.oracle,:old.vb,'DELETE',sysdate);   
  end if;

end;
/


Join

A join is a query that combines rows from two or more tables, views, or materialized views. Oracle Database performs a join whenever multiple tables appear in the FROM clause of the query.
Most join queries contain at least one join condition, either in the FROM clause or in the WHERE clause. The join condition compares two columns, each from a different table. To execute a join of three or more tables, Oracle first joins two of the tables based on the join condition comparing their columns and then joins the result to another table based on join conditions containing columns of the joined tables and the new table.
Equijoins:
An equijoin is a join with a join condition containing an equality operator(=). An equijoin combines rows that have equivalent values for the specified columns.
Ex. SELECT empno, ename, job, dname, loc FROM emp e, dept d WHERE e.deptno=d.deptno;

Non-Equijoins:
A join which contains an operator other than equal to ‘=’ in the joins condition.
Ex. SELECT empno, ename, job, dname, loc FROM emp e, dept d WHERE e.deptno>d.deptno;

Self Joins:
A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by the table aliases that qualify column names in the join condition.
Ex. SELECT e1.empno, e2.ename, e1.job, e2.deptno FROM emp e1, emp e2 WHERE e1.empno=e2.mgr;

Inner Joins:
An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition.
Ex. SELECT empno, ename, job, dname, loc FROM emp INNER JOIN dept USING(deptno);

Outer Joins:
An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition.
To write a query that performs an outer join of tables A and B and returns all rows from A (a left outer join), use the LEFT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of B in the join condition in the WHERE clause. For all rows in A that have no matching rows in B, Oracle Database returns null for any select list expressions containing columns of B.
Ex. SELECT empno, ename, job,dname,loc FROM emp e LEFT OUTER JOIN dept d
on(e.deptno=d.deptno);                  OR
SELECT empno, ename, job, dname, loc FROM emp e, dept d WHERE e.deptno=d.deptno(+);

To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the RIGHT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of A in the join condition in the WHERE clause. For all rows in B that have no matching rows in A, Oracle returns null for any select list expressions containing columns of A.
Ex. SELECT empno, ename, job, dname, loc FROM emp e RIGHT OUTER JOIN dept d
ON(e.deptno=d.deptno);                                   OR
SELECT empno, ename, job, dname, loc FROM emp e, dept d WHERE e.deptno(+)=d.deptno;

To write a query that performs an outer join and returns all rows from A and B, extended with nulls if they do not satisfy the join condition (a full outer join), use the FULL [OUTER] JOIN syntax in the FROM clause.
Ex. SELECT empno, ename, job, dname, loc FROM emp e FULL OUTER JOIN dept d
ON(e.deptno=d.deptno);
Download or view topics : Initialization Parameters & Control files


Friday, 19 April 2013

You can download a word file for redo log and change vector but it is just short description



Exception Handling
What is an exception?
In PL/SQL, errors and warnings are called as exceptions. Whenever a predefined error occurs in a program, Pl/SQL rises an exception.
There are two types of exception handlers:
1.       Predefined internal exceptions
2.       Userdefined exceptions.

PL/SQL has a collection of predefined exceptions. Each has a name. These exceptions are automatically raised by PL/SQL whenever the corresponding error occurs.
When PL/SQL rises a predefined exception, program is aborted by displaying error message. But if program is to handle exception raised by PL/SQL then we have to use Exception Handler part of the block.


When Exception Handler is given, control is transferred to exception handler whenever there is an exception. After the exception handler is executed, control is transferred to next statement in the enclosing block.

Example of Exception handling:
DECLARE
                X_studid student.studid%type;
BEGIN
                SELECT studid, name FROM students
                WHERE name=’BHARAT PARMAR’;

EXCEPTION
                WHEN NO_DATA_FOUND Then
                INSERT INTO errortable VALUES(‘BHARAT PARMAR Not Found’);
END;

In above example, if no row found with the name ‘BHARAT PARMAR’ then PL/SQL raises exception NO_DATA_FOUND. Then this exception is handled by exception handler and one row is inserted into ERRORTABLE table.

PREDFINED EXCEPTIONS
PL/SQL has defined certain common errors and given names to these errors, which are called as predefined exceptions.

The following is the list of predefined exceptions and the corresponding Oracle Errors:
EXCEPTION NAME                                               ORACLE ERROR
CURSOR_ALREADY_OPEN                                      ORA-06511
DUP_VAL_ON_INDEX                                               ORA-00001
INVALID_CURSOR                                                     ORA-01001
LOGIN_DENIED                                                          ORA-01722
NO_DATA_FOUND                                                     ORA-01017
NOT_LOGGED_ON                                                    ORA-01403
PROGRAM_ERROR                                                    ORA-01012
ROW_TYPE_MISMATCH                                           ORA-06501
STORAGE_ERROR                                                     ORA-06504
TIMEOUT_ON_RESOURCE                                      ORA-06500
TOO_MANY_ROWS                                                  ORA-01422
VALUE_ERROR                                                           ORA-06502
ZERO_DIVIDE                                                             ORA-01476

USER DEFINED EXCEPTION:
PL/SQL allows you to create exceptions of your own. Unlike predefined exceptions, user defined exceptions are to be raised explicitly using RAISE command.
Syntax:
RAISE exception;

Once a user defined exception is raised using RAISE command, it is to be handled just like a predefined exception. The following are the steps of userdefined exceptions:
·         Declare an excpetion in DECLARE section using keyword EXCEPTION.
·         Use RAISE command to raise the exception in the executable part.
·         Handle userdefined exception in exception handler.
Important questions :


1. Explain Oracle instance architecture.
2. What is table space? Explain its types.
3. What is backup? List out types of backup. Explain any two.
4. Explain SQL Loader.
5. Explain Control files, Redo log file.
6. What is Net 8? Explain in details.

Thursday, 18 April 2013

What is Tablespace? Explain types of tablespace.

A databse consists of one or morelogical storage units is called a tablespace. Each tablespace in an OracleDatabase consists of one or more data files. A database typically contains atleast one tablespace. Within the database, the table is same as a folder onhard disk of a computer. Every oracle database has a tablespace named SYSTEM,which is created automatically when the Oracle is installed. The SYSTEMtablespace is the default location of all objects when a database is firstcreated. Tablespaces can be created, dropped(deleted) and merged, just as canthe folders on the hard disk of a computer.

Syntax to create a tablespace:

CREATE TABLESPACE<tablespace_name> DATAFILE <path> SIZE <int>

Syntax to delete a tablespace:
DROP TABLESPACE<tablespace_name>

Types of tablespace:

SYSTEM tablespace

TEMP tablespace

Tools tablespace

USER tablespace

DATA and INDEX tablespace

ROLLBACK tablespace