0% found this document useful (0 votes)
45 views

PLSQL

PL/SQL is a procedural language extension of SQL. It allows developers to write procedures, functions and triggers in Oracle databases. PL/SQL code can be used to manipulate data in Oracle databases. Views are virtual tables that contain queries and present a different perspective of the underlying data. Normalization is the process of splitting tables to reduce redundancy and ensure data dependencies make sense.

Uploaded by

Aravind
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

PLSQL

PL/SQL is a procedural language extension of SQL. It allows developers to write procedures, functions and triggers in Oracle databases. PL/SQL code can be used to manipulate data in Oracle databases. Views are virtual tables that contain queries and present a different perspective of the underlying data. Normalization is the process of splitting tables to reduce redundancy and ensure data dependencies make sense.

Uploaded by

Aravind
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

PL/SQL

 Procedural language/structured query language


 PL/SQL is procedural language that can be understood by human.
 PL using structured query language (i.e., PL is extension of SQL)
 It can be achieved by using a programming language
 PL with data manipulation and query statements of languages (SQL) included
in PL
 For oracle platform make use of a language called as PL/SQL
PL/SQL ENVIRONMENT

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

 Stored procedure can be stored in a database with procedure name


 To create a stored procedure make use of statement
 Create Procedure procedure_name
To execute a procedure use keyword
Execute procedure_name
Create procedure SP_HELLO IS
BEGIN
DBMS_OUTPUT.PUT_LINE(‘ ENJOY YOUR LIFE ‘);
END;
/
Procedure created
SQL> exec SP_HELLO;
ENJOY YOUR LIFE
PL/SQL procedure successfully completed
DELETE A PROCEDURE
SQL> drop Procedure SP_HELLO;
Procedure dropped.
Benefit of PL/SQL:
1. PL/SQL is portable
2. We can declare variable
3. We can program with procedural language control structure
4. Execute a sequence of statements conditionally
5. Execute a sequence of statement iteratively in a loop
6. Process individually the rows returned by a multiple multiple row query with
explicit cursor
DIFF B/W Function and Stored Procedure

Function Stored procedure


1. Should return atleast one output 1. Does not need to return values but
can retain values
2. Compiled at run time 2. Stored as pseudocode in database
(i.e.,) complied form
3. Cannot affect the statement of 3. Can affect the statement of
database database using commit
4.Can be invoked from SQL statements 4. Can’t be invoked from SQL
statements
5. Functions are mainly used to compile 5. Procedure is mainly used to process
values table
TRIGGERS
 Triggers can be executed whenever an DML takes place
 Triggers provides a way of executing PL/SQL code on the occurance of specific
Database events
 Oracle enables triggers to be invoked by many events other than table insert,update and
delete operation
Benefits of TRIGGERS:
1. Events can be logged
2. Collection of information on users accessing the table
3. Table data’s consistency can be maintained while using DML operation
4. Transactions which are not Liable/invalid can be prevented
CURSOR:
 It is a work area allocated for processing request by oracle sql engine.

TYPES OF CURSOR:
 Implicit cursor
 Explicit cursor

WHAT IS IMPLICIT CURSOR?


 Automatically created by the oracle server.
 User cannot control the behaviour of the cursor.
 Oracle server creates an implicit cursor for any PL/SQL block which executes
an sql statement
What is explicit cursor?
 Explicit cursor are user defined cursor.
 User has full control for explicit cursor.

Steps for creating an explicit cursor:


 Declare
 Open
 Fetch
 Close
DECLARE:
 Declaring a cursor means initializing a cursor into memory.
 You define explicit cursor in declaration section of your PL/SQL block.

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)

How do I restore a view?


We cant restore view table because view table is imaginary table so they cant
save in recycle bin. If there is no in recyclebin mean how can we retrieve
Original table are stored in recyclebin
NORMALIZATION
 Is the process of splitting the bigger table into many small table without changing its
functionality.
 It is generally carried out during design phase of SDLC
There are two goals:
1. Eliminating Redundant data
2. To ensure data dependencies make sence(only storing related data in a table.
These goal reduces the amount of space a database consumes and ensure that data is
logically stored.
STEPS IN NORMALIZATION
1NF – 1st Normal form
2NF – 2nd Normal form
3NF – 3rf Normal form
1NF(ELIMINATE REPEATING GROUPS
 Make a seprate table for each set of related attributes and give each table a
primarykey.
 Each field contain at most one value from its attribute domain
COLLEGE
RegNO – PK
Sname
Semester
DOB
MailID
Phone
BOOKNO- PK
Bname
Author
DOI
DOR
FINE
2 NF (ELIMINATE REDUNDANT DATA)
 If an attribute depends on only part of a multi-valued key,remove it to a
seperate table
 To perform 2NF table should be in 1NF

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

STUDENTS BOOKS Library


RegNo – PK BOOKNO – PK BOOKno – FK
Sname Bname RegNO – FK
Semester Author DOI
DOB DOR
MailID Fine
Phone
Disadvantage

 The only minor disadvantage is we may have to write complex queries as we


have more number of table to be accessed.
Denormalization:

 Denormalization is the process of combining more than 1 smaller table to


form 1 bigger table is called as denormalization
INDEX

Index is used by oracle to speed up the retrieval of rows.


It will be used as a pointer for retrieval of rows.
Is used and maintained automatically by oracle server.
Index can be created in two ways:
Automatically
Manually
Types of index
Unique index
Nonunique index
Automatically
 Index which will created automatically will be unique index.
 It will be created when we use primary key or unique key constraint in the
table.
Manual
 Manual index will be nonunique index created by the user.
 We can create on one or more than one column in table.
Syntax
Create index index_name
On table (column1,column2……)

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

 Drop index index_name;

You might also like