SlideShare a Scribd company logo
1 of 5
Download to read offline
Downloaded From: http://www.cbseportal.com




                                 Chapter – 1
                                    PL/SQL
                                 (Informatics Practices)


PL/SQL = Procedural Language extensions to SQL
An Oracle-specific language combining features of:

      modern, block-structured programming language
      database interaction via SQL

Designed to overcome declarative SQL's inability to specify control aspects of DB
interaction.

Used to add procedural capabilities to Oracle tools.

PL/SQL is implemented via a PL/SQL engine (cf. JVM)

      which can be embedded in clients (e.g. Forms, SQL*Plus)
      which is also usually available in the Oracle server

Why PL/SQL?

Consider trying to implement the following in SQL (SQL*Plus):

If a user attempts to withdraw more funds than they have from their account, then
indicate "Insufficient Funds", otherwise update the account

A possible implementation:

      ACCEPT person PROMPT 'Name of account holder: '
      ACCEPT amount PROMPT 'How much to withdraw: '

      UPDATE Accounts
      SET balance = balance - &amount
      WHERE holder = '&person' AND balance > &amount;
      SELECT 'Insufficient Funds'
      FROM Accounts
      WHERE holder = '&person' AND balance < = &amount;

Two problems:

      doesn't express the "business logic" nicely
      performs both actions when (balance-amount < amount)

We could fix the second problem by reversing the order (SELECT then UPDATE).




                         Downloaded From: http://www.cbseportal.com
Downloaded From: http://www.cbseportal.com




But in SQL there's no way to avoid executing both the SELECT and the UPDATE

PL/SQL allows us to specify the control more naturally:
-- A sample PL/SQL procedure

PROCEDURE withdrawal(person IN varchar(20), amount IN REAL ) IS
  current REAL;
BEGIN
  SELECT balance INTO current
  FROM Accounts
  WHERE holder = person;
  IF (amount > current)
     dbms_output.put_line('Insufficient Funds');
  ELSE
     UPDATE Accounts
     SET balance = balance - amount
     WHERE holder = person AND balance > amount;
     COMMIT;
  END IF;
END;
And package it up into a useful function, which could be used as:
SQL> EXECUTE withdrawal('John Shepherd', 100.00);

PL/SQL Syntax

PL/SQL is block-structured, where a block consists of:

DECLARE
  declarations for
     constants, variables and local procedures
BEGIN
  procedural and SQL statements
EXCEPTION
  exception handlers
END;

Data Types

PL/SQL constants and variables can be defined using:

      standard SQL data types (CHAR, DATE, NUMBER, ...)
      built-in PL/SQL types (BOOLEAN, BINARY_INTEGER)
      PL/SQL structured types (RECORD, TABLE)

Users can also define new data types in terms of these.
There is also a CURSOR type for interacting with SQL.

Record Types

Corresponding to Modula RECORDs or Constructs, and also closely related to SQL table
row type.



                         Downloaded From: http://www.cbseportal.com
Downloaded From: http://www.cbseportal.com




New record types can be defined via:
  TYPE TypeName IS RECORD
      (Field1 Type1, Field2 Type2, ...);
Example:
  TYPE Student IS RECORD (
      id# NUMBER(6),
      name VARCHAR(20),
      course NUMBER(4)
  );
Record components are accessed via Var.Field notation.
  fred Student;
  ...
  fred.id# := 123456;
  fred.name := 'Fred';
  fred.course := 3978;
Record types can be nested.
  TYPE Day IS RECORD
      (day NUMBER(2), month NUMBER(2), year NUMBER(4));

   TYPE Person IS RECORD
     (name VARCHAR(20), phone VARCHAR(10), birthday Day);

Constants and Variables

Variables and constants are declared by specifying:
  Name [ CONSTANT ] Type [ := Expr ] ;

Examples:
  amount INTEGER;
  part_number NUMBER(4);
  in_stock BOOLEAN;
  owner_name VARCHAR(20);
  max_credit CONSTANT REAL := 5000.00;
  my_credit REAL := 2000.00;

Variables can also be defined in terms of:

      the type of an existing variable or table column
      the type of an existing table row (implict RECORD type)

Examples:

   employee Employees%ROWTYPE;
   name Employees.name%TYPE;

Assigning Values to Variables

A standard assignment operator is available:
  tax := price * tax_rate;
  amount := TO_NUMBER(SUBSTR('750 dollars',1,3));




                         Downloaded From: http://www.cbseportal.com
Downloaded From: http://www.cbseportal.com




Values can also be assigned via SELECT...INTO:
   SELECT price +10 INTO cost
  FROM StockList
  WHERE item = 'Cricket Bat';
  total := total + cost;

SELECT...INTO can assign a whole row at once:
    DECLARE
      emp Employees%ROWTYPE;
      my_name VARCHAR(20);
      pay NUMBER(8,2);
    BEGIN
      SELECT * INTO emp
      FROM Employees
      WHERE id# = 966543;
      my_name := emp.name;
...
      SELECT name,salary INTO my_name,pay
      FROM Employees
      WHERE id# = 966543;
    END;

Control Structures

PL/SQL has conventional set of control structures:

      for sequence (note:- ; is a terminator)
      IF for selection
      FOR, WHILE, LOOP for repetition

Along with exceptions to interrupt normal control flow.
And a NULL; statement to do nothing.

Selection

Selection is expressed via:

   1. IF Cond1 THEN Statements1;
   2. ELSIF Cond2 THEN Statements2;
   3. ELSIF Cond3 THEN Statements3;

Example:

 If A > B Then                                  If A > B Then
    Dbms_output.put_line (‘A is big’);             Dbms_output.put_line (‘A is big’);
 End if;                                        ELSIF
                                                   Dbms_output.put_line (‘B is big’);
                                                End if;




                          Downloaded From: http://www.cbseportal.com
Downloaded From: http://www.cbseportal.com




If A > B Then
   Dbms_output.put_line (‘A is big’);
ELSE
   Dbms_output.put_line (‘B is big’);
End if;




                        Downloaded From: http://www.cbseportal.com

More Related Content

Recently uploaded

Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioChristian Posta
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 

Recently uploaded (20)

Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Comparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and IstioComparing Sidecar-less Service Mesh from Cilium and Istio
Comparing Sidecar-less Service Mesh from Cilium and Istio
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 

Class Xii E Book Informatics Practices Chapter 1 Pl Sql

  • 1. Downloaded From: http://www.cbseportal.com Chapter – 1 PL/SQL (Informatics Practices) PL/SQL = Procedural Language extensions to SQL An Oracle-specific language combining features of: modern, block-structured programming language database interaction via SQL Designed to overcome declarative SQL's inability to specify control aspects of DB interaction. Used to add procedural capabilities to Oracle tools. PL/SQL is implemented via a PL/SQL engine (cf. JVM) which can be embedded in clients (e.g. Forms, SQL*Plus) which is also usually available in the Oracle server Why PL/SQL? Consider trying to implement the following in SQL (SQL*Plus): If a user attempts to withdraw more funds than they have from their account, then indicate "Insufficient Funds", otherwise update the account A possible implementation: ACCEPT person PROMPT 'Name of account holder: ' ACCEPT amount PROMPT 'How much to withdraw: ' UPDATE Accounts SET balance = balance - &amount WHERE holder = '&person' AND balance > &amount; SELECT 'Insufficient Funds' FROM Accounts WHERE holder = '&person' AND balance < = &amount; Two problems: doesn't express the "business logic" nicely performs both actions when (balance-amount < amount) We could fix the second problem by reversing the order (SELECT then UPDATE). Downloaded From: http://www.cbseportal.com
  • 2. Downloaded From: http://www.cbseportal.com But in SQL there's no way to avoid executing both the SELECT and the UPDATE PL/SQL allows us to specify the control more naturally: -- A sample PL/SQL procedure PROCEDURE withdrawal(person IN varchar(20), amount IN REAL ) IS current REAL; BEGIN SELECT balance INTO current FROM Accounts WHERE holder = person; IF (amount > current) dbms_output.put_line('Insufficient Funds'); ELSE UPDATE Accounts SET balance = balance - amount WHERE holder = person AND balance > amount; COMMIT; END IF; END; And package it up into a useful function, which could be used as: SQL> EXECUTE withdrawal('John Shepherd', 100.00); PL/SQL Syntax PL/SQL is block-structured, where a block consists of: DECLARE declarations for constants, variables and local procedures BEGIN procedural and SQL statements EXCEPTION exception handlers END; Data Types PL/SQL constants and variables can be defined using: standard SQL data types (CHAR, DATE, NUMBER, ...) built-in PL/SQL types (BOOLEAN, BINARY_INTEGER) PL/SQL structured types (RECORD, TABLE) Users can also define new data types in terms of these. There is also a CURSOR type for interacting with SQL. Record Types Corresponding to Modula RECORDs or Constructs, and also closely related to SQL table row type. Downloaded From: http://www.cbseportal.com
  • 3. Downloaded From: http://www.cbseportal.com New record types can be defined via: TYPE TypeName IS RECORD (Field1 Type1, Field2 Type2, ...); Example: TYPE Student IS RECORD ( id# NUMBER(6), name VARCHAR(20), course NUMBER(4) ); Record components are accessed via Var.Field notation. fred Student; ... fred.id# := 123456; fred.name := 'Fred'; fred.course := 3978; Record types can be nested. TYPE Day IS RECORD (day NUMBER(2), month NUMBER(2), year NUMBER(4)); TYPE Person IS RECORD (name VARCHAR(20), phone VARCHAR(10), birthday Day); Constants and Variables Variables and constants are declared by specifying: Name [ CONSTANT ] Type [ := Expr ] ; Examples: amount INTEGER; part_number NUMBER(4); in_stock BOOLEAN; owner_name VARCHAR(20); max_credit CONSTANT REAL := 5000.00; my_credit REAL := 2000.00; Variables can also be defined in terms of: the type of an existing variable or table column the type of an existing table row (implict RECORD type) Examples: employee Employees%ROWTYPE; name Employees.name%TYPE; Assigning Values to Variables A standard assignment operator is available: tax := price * tax_rate; amount := TO_NUMBER(SUBSTR('750 dollars',1,3)); Downloaded From: http://www.cbseportal.com
  • 4. Downloaded From: http://www.cbseportal.com Values can also be assigned via SELECT...INTO: SELECT price +10 INTO cost FROM StockList WHERE item = 'Cricket Bat'; total := total + cost; SELECT...INTO can assign a whole row at once: DECLARE emp Employees%ROWTYPE; my_name VARCHAR(20); pay NUMBER(8,2); BEGIN SELECT * INTO emp FROM Employees WHERE id# = 966543; my_name := emp.name; ... SELECT name,salary INTO my_name,pay FROM Employees WHERE id# = 966543; END; Control Structures PL/SQL has conventional set of control structures: for sequence (note:- ; is a terminator) IF for selection FOR, WHILE, LOOP for repetition Along with exceptions to interrupt normal control flow. And a NULL; statement to do nothing. Selection Selection is expressed via: 1. IF Cond1 THEN Statements1; 2. ELSIF Cond2 THEN Statements2; 3. ELSIF Cond3 THEN Statements3; Example: If A > B Then If A > B Then Dbms_output.put_line (‘A is big’); Dbms_output.put_line (‘A is big’); End if; ELSIF Dbms_output.put_line (‘B is big’); End if; Downloaded From: http://www.cbseportal.com
  • 5. Downloaded From: http://www.cbseportal.com If A > B Then Dbms_output.put_line (‘A is big’); ELSE Dbms_output.put_line (‘B is big’); End if; Downloaded From: http://www.cbseportal.com