PLSQL
PLSQL
PL/SQL
PL/SQL PL/SQL BLOCK
PL/SQL BLOCK
BLOCK
Procedure
Statement
Executor
SQL
SQL Statement
Executor
Oracle server
PL/SQL BLOCK STRUCTURE
DECLARE (optional)
Variable,cursors,User-defined exceptions
BEGIN (MANDATORY)
SQL STATEMENTS
PL/SQL STATEMENTS
EXCEPTION ( OPTIONAL)
Actions to perform when error occurs
END; (MANDATORY)
Example
SQL> Set serveroutput on;
DECLARE
employee varchar2(10);
Deptnum varchar(10);
BEGIN
select ename INTO employee from emp
where empno = 7369;
select deptno INTO deptnum from emp
where empno = 7369;
DBMS_OUTPUT.PUT_LINE(Employee ||’ belong to ‘ || deptno);
END;
/
O/P
SMITH belong to 20
PL/SQL procedure successfully completed
Stored Procedure
TYPES OF CURSOR:
Implicit cursor
Explicit cursor
SYNTAX OF DECLARE:
Cursor cursor_nama is select_statement;
OPEN:
In order to put that cursor to wprk we have to open it first.
When you open a cursor the memory will be alloted to it.
SYNTAX OF OPEN:
open cursor_name;
FETCH:
The process of retrieving the data from the cursor is called fetching.
SYNTAX OF FETCH:
Fetch cursor_name into PL/SQL variable.
CLOSE:
When the server comes across the closing statement of a cursor it will
relinguish all the resources associated with it.
SYNTAX OF CLOSE:
Close cursor_name;
BASIC PROGRAMMING STRUCTURE:
Declare
cursor cursor_name is select_statement;
BEGIN
open cursor_name;
fetch cursor_name into PL/SQL variable;
close cursor_name;
End;
Declare
V_name varchar2(30);
Cursor cur_qspiders is
Select ename from emp
Where job in (‘SALESMAN’,’MANAGER’);
Begin
Open cur_qspiders;
Loop
Fetch cur_qspiders into v_name;
Dbms_output.put_line(v_name);
Exit when cur_qspiders % not found;
End loop;
Close cur_qspiders;
End;
/
VIEWS
View is a virtual table which contain Query
SYNTAX:
Create view viewname as SELECT Statement
EX:
Create view V1 as
Select ename,dname,grade
From emp e,dept d,salgrade s
Where e.deptno=d.deptno and
e.Sal between s.losal and s.hisal;
VIEW CREATED
Views
View is a pre-defined table /virtual table that can be created by user
A view is a composition of table that is stored in a database with an associated
view name
A view consist of all the column /all the row from table
A view can be created for single/multiple table which is totally depends on
requirements
Logically represent subset of data from one or more tables
Before creating view check for right and get right from administrator
NOTE:
Suppose we change any column or records on the view table that change will
cause in original table.
Advantages of views
To restrict data access
To make complex query easy
To provide data independence
To present different views of the same data
It reduces the time (complex query can be avoided if we create a view)
In views by using group function Alias name will be treated as column for
those functions(alias name is mandatory by using group function)
STUDENTS BOOKS
Regno –PK BOOKNO – PK
Sname Bname
Semester Author
DOB DOI
MailID DOR
Phone Fine
3 NF (ELIMINATE COLUMNS NOT
DEPENDENT ON KEY)
If Attribute do not contribute to a description of the key,remove them to a
seperate table.
All attribute must be directly dependent on the primary key
To perform 3NF table should be in 2NF
Example
Create index abcd
On employees(first_name);
When to go for index
The table is large.
Column contains a large range of values.
Column contains a large number of null values.
Some specific columns are frequently used in a where clause.
Checking Index
We can check index from USER_INDEXES.
Select * from user_indexes;
Dropping index