PLSQL 01 - 10 - Sept - 2020
PLSQL 01 - 10 - Sept - 2020
PLSQL
Mahendra Pachpor
9/10/2020
Course Code • Day-1 / PLSQL
Version Number:1.0
2 9/10/2020
Session Objectives
3 9/10/2020
About SQL
• The purpose of SQL is to provide an interface to a relational database such as Oracle Database,
and all SQL statements are instructions to the database.
• The strengths of SQL provide benefits for all types of users, including application
programmers, database administrators, managers, and end users.
Why PLSQL????/
• Now you must be having few questions in mind
If SQL is so
powerful, why
we require to
use PL/SQL?
Let us think
about few
scenarios…
Why PL/SQL?
• By extending SQL, PL/SQL offers a unique combination of power and ease of use.
• We can write procedures and functions which can be invoked from different applications.
Advantages of PL/SQL
Tight
Integration
Access to with SQL
Better
Pre-defined
Performance
Packages
Supports both
Tight Security static and
Advantages dynamic SQL.
Of
PL/SQL
Support for
Higher
Object-Oriented
Productivity
Programming
Support for
Full Portability Developing Web
Applications
Tight Integration With SQL
• PL/SQL lets you use all the SQL commands as well as all the SQL functions and
operators.
• This extensive SQL support lets you manipulate Oracle data flexibly and safely.
• PL/SQL fully supports SQL data types, which reduces the need to convert the data
passed between your applications and the database.
• Running a SQL query and processing the result set is as easy in PL/SQL.
Better Performance
Database
Application
• Each SQL statement results in another call to Oracle and higher performance overhead.
• Every time a SQL statement is issued, it must be sent over the network, creating more traffic.
• With PL/SQL, an entire block of statements can be sent to Oracle at one time.
• This can drastically reduce communication between your application and Oracle.
Higher Productivity Full Portability
• PL/SQL lets you write very compact code for • Applications written in PL/SQL can run on
manipulating data any operating system and platform where
the Oracle database runs.
• PL/SQL can query, transform, and update
data in a database. • With PL/SQL, you can write portable
program libraries and reuse them in
• PL/SQL offers object-oriented features and different environments.
data types which in turn saves time on
design and debugging.
Access to Pre-defined Packages Support for Object-Oriented Programming
• PL/SQL lets you use all the SQL data manipulation, and transaction control commands in a pl/sql
block.
• Unlike static SQL, which remains the same in each execution, PL/SQL supports dynamic SQL which
enables you to build SQL statements as character strings at runtime.
PL/SQL Architecture
PL/SQL Architecture
PL/SQL Architecture
• As a console program
• As a pseudo-GUI
• (This form of SQL*Plus is available only on Microsoft Windows)
PLSQL Environment
SQL Developer
• Database administration and query tool that provides a single consistent interface for various databases
• It allows to visually navigate through your database structure, create and execute SQL queries and scripts the
easy way
TOAD
• Used for development and administration of various relational databases using SQL
PL/SQL Block
PL/SQL Block
• PL/SQL is a block-structured language.
• Each program written in PL/SQL is written as a block.
• Blocks can also be nested.
• Each block is meant for a particular task.
PL/SQL
Block types
Anonymous Named
Block Block
PL/SQL Block
• After compilation, it is not getting stored on • Once compiled, Named blocks are getting stored
oracle server. on oracle server.
• We can not call anonymous block in any other • We can call them in any other pl/sql block
pl/sql block
PL/SQL Anonymous Block
PL/SQL Named Block Structure
BEGIN
Execution Section
EXCEPTION
Exception Section
END;
PL/SQL Block Structure
Header Section
• Relevant for named blocks only
• Stored procedures (used to perform repetitive code.)
• Stored functions (used to return value to the calling block),
• Packages (allows related objects to be stored together),
• Triggers (pl/sql block executed implicitly whenever the triggering event
takes place).
• Determines the way that the named block or program must be called.
• Includes the name, parameter list, and RETURN clause (for a function
only).
PL/SQL Block Structure
DECLARE
v_name VARCHAR2(35);
v_id NUMBER := 0;
PL/SQL Block Structure
Executable Section
BEGIN
SELECT ename
INTO v_ename
FROM emp
WHERE empno = 7900 ;
DBMS_OUTPUT.PUT_LINE
(‘Employee name :’ || v_ename);
END;
PL/SQL Block Structure
Exception Section
• It contains statements that are executed when a runtime error occurs within a block.
• An optional section.
• Control is transferred to this section when an run time error is encountered and it is
handled
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE
(‘ There is no employee with Employee no 7900 ’);
END;
PL/SQL Datatypes
PL/SQL Data Types
• The PL/SQL data types are divided into four categories
• These are placeholders that store the values that can change through the PL/SQL Block
• PL/SQL lets you declare constants and variables, then use them in SQL and procedural
statements anywhere an expression can be used
• Invalid Declarations
v_cust_id NUMBER(2) NOT NULL;
v_name VARCHAR2 DEFAULT ‘Sachin’;
• Initialize identifiers by using the assignment operator (:=) or the reserved word “DEFAULT”
Questions
40 9/10/2020
10SSEP2013
Thank You
www.birlasoft.com
9/10/2020
41