PL/SQL:
A PROGRAMMING LANGUAGE & CONTROL
STRUCTURES
PL/SQL:
A PROGRAMMING LANGUAGE. The standard
query language for relationship database is SQL
(Structured query language ).structured query language is a
fourth –generation , high level, nonprocedural language.
The PL/SQL also possess features of object-oriented
languages, such as:
Data encapsulation.
Error handling.
Information hiding.
Object-oriented programming(OOP).
HISTORY OF PL/SQL
 Built-in package.
 Error handling.
 The transaction control statements
SAVEPOINT,ROLLBACK,AND COMMIT.
 The DML statements INSERT,DELETE, AND UPDATE.
 Built-in functions –character, numeric, conversion, and
date functions.
 Modular programming with procedures and functions.
 Programmer- defined subtypes.
 Stored procedures, functions, and packages.
 DDL support through the DBMS –SQL package.
 Database access through work areas called cursors.
FUNDAMENTALS
A PL/SQL program constants of statements . You may
use upper or lowercase letters in your program.
RESERVED WORDS
IDENTIFIERS
LITERALS
COMMENTS
RESERVED WORDS
The reserved words or by words are words provided but
the language that have a specific use in the language.
Example –end , if while, package.
IDENTIFIERS
User –defined identifiers used to name variables,
constants, procedures, functions cursors, tables, records and
exception.
RULES
The name can be from 1to 30 characters in length.
The name must start with a letter .
Spaces are not allowed.
Other special character are not allowed.
LITERALS
Literals are values that are not represented by user-defined
identifiers.
THREE TYPES of literals:
Numeric
Character
Boolean
PL/SQL BLOCK STRUTURE
PL/SQL is a block structured language. A program can be
dividend into logical blocks.
TWO TYPES
Anonymous block
Named block
1.ANONYMOUS BLOCK
An anonymous block is a block of code without a name.
2.NAMED BLOCK
A subprogram is a named block that can be called and
can take arguments.
COMMENTS
Comments are used to document program. They are
written as part of a program but they are executed . In fact
comments are ignored by the PL/SQL engine.
1.To write a single line comments two dashes(--)are
entered at the beginning of a new line.
EXAMPLE:
--This is a single line comment
2.To write a multiline comment text is placed between
/*and*/. A multiline comment can be written on a separate
line .
EXAMPLE:
/*This is a multiline comment that ends here */
Scalar
Composite
Reference
LOB
DATA TYPES:
SCALAR
DATA
TYPE
CHARACTER
NUMBER
BOOLEAN
DATE
1.Character:
Variables with a character data type can store text. The
text may include letters , number & special characters.
Character data type include ;
CHAR.
VARCHAR.
2.NUMBER:
Whole numbers or integer values can be handled by
following data types.
BINARY INTEGER.
INTEGER.
INT.
SMALLINT.
POSITIVE.
NATURAL.
2.BOOLEaN :
It is used for Boolean data TRUE,FALSE or NULL only.
3.DATE:
A user can enter a date in many different formats with
the TO_DATE function, but a date is always stored in
standard 7 byte format.
Century.
Year.
Month.
Day.
Hour.
Minute.
Second.
VARIABLE DECLARATION:
The declarations are done in the DECLARE section
of the program block.
Syntax:
DECLARE
identifier name[CONSTRAINT] datatype
[NOT NULL][:=DEFAULT expression]
ANCHORED DECLARATION:
SQL uses %TYPE attribute to anchor a variable’s data
type.
Syntax:
variablename typeattribute %TYPE [value assignment]
NESTED ANCHORING:
The %TYPE attribute’s use can be nested.
Syntax :
DECLARE
--source variable v _ commission
v _ commission NUMBER(7,2)
NOT NULL CONSTRAINT FOR %TYPE
DECLARATIONS:
If a source variable is declared with a NOT NULL
constraint , the %TYPE declaration inherits the NOT NULL
constraint from the source.
ASSIGNMENT OPERATION:
The assignment operation is one of the ways to assign
value to a variable.
Syntax:
Variablename := literal  variable name  expression;
BIND VARIABLES:
Bind variables are also known as host variables.
Syntax:
VARIABLE variablename datatype
ARITHMETIC OPERATORS:
Five standard arithmetic operators are available in SQL.
Arithmetic operator use
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
Sequential structure.
Selection structure.
Looping structure.
CONTROL STRUCTURES:
Sequential structure:
A series of instructions are performed from the beginning
to the end in a linear order.
Selection structure:
There are three selection or conditional statements in
SQL.
 Relational operators.
 Logical operators.
 Special operators.
PL/SQL has five conditional or selection statement’s
available for decision making.
 IF…THEN…END IF.
 IF…THEN…ELSE…END IF.
 IF…THEN…ELSIF…END IF.
 CASE…END CASE.
 Searched CASE.
IF…THEN…END IF
It is also known as simple IF statement. A simple IF
statement performs action statements if the result of the
condition is TRUE. If the condition is FALSE, no action is
performed.
Syntax:
IF condition(s) THEN
Action statements
END IF;
IF…THEN…ELSE…END IF:
This statement is an extension of the simple IF statement.
It provides action statements for the TRUE outcome as well
as for the FALSE outcome.
Syntax:
IF condition(s) THEN
Action statement 1
ELSE
Action statement 2
END IF;
IF…THEN…ELSIF…END IF:
When you have many alternative /options, you can use
previously explained statements, but the ELSFIF alternative
is more efficient than the other two.
Syntax:
IF condition(s)1 THEN
Action statements 1
ELSIF condition(s)2 THEN
Action statements 2
…
ELSIF condition(s)N THEN
Action statement N
[ELSE
Else Action statements]
END IF;
CASE:
The CASE statement is an alternative to the IF…THEN…
ELSIF…END IF statement. The CASE statement begins
with keyword CASE and ends with the keywords END
CASE.
Syntax:
Case [variable_name]
WHEN value1condition1 THEN action_statement1;
WHEN value2condition2 THEN action_statement2;
WHEN valueNcondition N THEN action_statementN;
ELSE action_statement;
END CASE;
SEARCHED CASE:
A statement with a value is known as a CASE statement,
and a statement with conditions is known as a searched
CASE statement. A CASE statement uses variable_name as
a selector, but a searched CASE does not use variable_name
as a selector.
NESTED IF:
The nested IF statement contains an IF statement within
another IF statement. If the condition in the outer IF
statement is TRUE, the inner IF statement is performed.
Looping structure:
Looping means iterations. A loop repeats a statement or
series of statements a specific number of times as defined by
the programmer.
THREE TYPES
 Basic loop
 While loop
 For loop
BASIC LOOP
A basic loop is a loop that is performed repeatedly. once a
loop is entered all statements in the loop are performed.
Syntax
Loop
Looping statement 1;
Looping statement 2;
……….
Looping statement N;
Exit (when condition);
End loop
WHILE LOOP
The while loop is an alternative to be basic loop and it
performed as long as the condition is true terminates the
condition becomes false.
Syntax
While condition loop
Looping statement 1;
Looping statement 2;
…….
Looping structure n;
End loop;
FOR LOOP
The for loop is the simples loop you can write. There is a
no net and no use an exit statement, And the counter need
not be declared.
Syntax
For counter in (reverse)lower….upper loop
Looping statement1
Looping statement2
……
Looping statement N
End loops
NESTED BLOCKS
PL/SQL block may contain another PL/SQL block ; in
another words PL/SQL blocks can be nested.
DATA MANIPULATION
 Insert statement
 Delete statement
 Update statement
INSERT STATEMENT
We will use an insert statement to add a new employee in
the employee table.
DELETE STATEMENT
We will show the use of the delete statement in the
PL/SQL block to remove some rows.
UPDATE STATEMENT
The update statement can be used in a PL/SQL block for
modification off data.
TRANSACTION CONTRAL STATEMENT
The COMMIT statements to commit the current
transaction.
The SAVEPOINT statements to mark a point in
your transaction.
The ROLLBACK (TO SAVEPOINT N)
statements to discard all or part of the transaction.

ORACLE PL/SQL

  • 1.
    PL/SQL: A PROGRAMMING LANGUAGE& CONTROL STRUCTURES
  • 2.
    PL/SQL: A PROGRAMMING LANGUAGE.The standard query language for relationship database is SQL (Structured query language ).structured query language is a fourth –generation , high level, nonprocedural language. The PL/SQL also possess features of object-oriented languages, such as: Data encapsulation. Error handling. Information hiding. Object-oriented programming(OOP).
  • 3.
    HISTORY OF PL/SQL Built-in package.  Error handling.  The transaction control statements SAVEPOINT,ROLLBACK,AND COMMIT.  The DML statements INSERT,DELETE, AND UPDATE.  Built-in functions –character, numeric, conversion, and date functions.  Modular programming with procedures and functions.  Programmer- defined subtypes.  Stored procedures, functions, and packages.  DDL support through the DBMS –SQL package.  Database access through work areas called cursors.
  • 4.
    FUNDAMENTALS A PL/SQL programconstants of statements . You may use upper or lowercase letters in your program. RESERVED WORDS IDENTIFIERS LITERALS COMMENTS RESERVED WORDS The reserved words or by words are words provided but the language that have a specific use in the language. Example –end , if while, package.
  • 5.
    IDENTIFIERS User –defined identifiersused to name variables, constants, procedures, functions cursors, tables, records and exception. RULES The name can be from 1to 30 characters in length. The name must start with a letter . Spaces are not allowed. Other special character are not allowed. LITERALS Literals are values that are not represented by user-defined identifiers.
  • 6.
    THREE TYPES ofliterals: Numeric Character Boolean PL/SQL BLOCK STRUTURE PL/SQL is a block structured language. A program can be dividend into logical blocks. TWO TYPES Anonymous block Named block
  • 7.
    1.ANONYMOUS BLOCK An anonymousblock is a block of code without a name. 2.NAMED BLOCK A subprogram is a named block that can be called and can take arguments. COMMENTS Comments are used to document program. They are written as part of a program but they are executed . In fact comments are ignored by the PL/SQL engine. 1.To write a single line comments two dashes(--)are entered at the beginning of a new line.
  • 8.
    EXAMPLE: --This is asingle line comment 2.To write a multiline comment text is placed between /*and*/. A multiline comment can be written on a separate line . EXAMPLE: /*This is a multiline comment that ends here */
  • 9.
  • 10.
  • 11.
    1.Character: Variables with acharacter data type can store text. The text may include letters , number & special characters. Character data type include ; CHAR. VARCHAR. 2.NUMBER: Whole numbers or integer values can be handled by following data types. BINARY INTEGER. INTEGER. INT. SMALLINT. POSITIVE. NATURAL.
  • 12.
    2.BOOLEaN : It isused for Boolean data TRUE,FALSE or NULL only. 3.DATE: A user can enter a date in many different formats with the TO_DATE function, but a date is always stored in standard 7 byte format. Century. Year. Month. Day. Hour. Minute. Second.
  • 13.
    VARIABLE DECLARATION: The declarationsare done in the DECLARE section of the program block. Syntax: DECLARE identifier name[CONSTRAINT] datatype [NOT NULL][:=DEFAULT expression]
  • 14.
    ANCHORED DECLARATION: SQL uses%TYPE attribute to anchor a variable’s data type. Syntax: variablename typeattribute %TYPE [value assignment] NESTED ANCHORING: The %TYPE attribute’s use can be nested. Syntax : DECLARE --source variable v _ commission v _ commission NUMBER(7,2)
  • 15.
    NOT NULL CONSTRAINTFOR %TYPE DECLARATIONS: If a source variable is declared with a NOT NULL constraint , the %TYPE declaration inherits the NOT NULL constraint from the source. ASSIGNMENT OPERATION: The assignment operation is one of the ways to assign value to a variable. Syntax: Variablename := literal variable name expression;
  • 16.
    BIND VARIABLES: Bind variablesare also known as host variables. Syntax: VARIABLE variablename datatype ARITHMETIC OPERATORS: Five standard arithmetic operators are available in SQL. Arithmetic operator use + Addition - Subtraction * Multiplication / Division ** Exponentiation
  • 17.
  • 18.
    Sequential structure: A seriesof instructions are performed from the beginning to the end in a linear order. Selection structure: There are three selection or conditional statements in SQL.  Relational operators.  Logical operators.  Special operators. PL/SQL has five conditional or selection statement’s available for decision making.
  • 19.
     IF…THEN…END IF. IF…THEN…ELSE…END IF.  IF…THEN…ELSIF…END IF.  CASE…END CASE.  Searched CASE. IF…THEN…END IF It is also known as simple IF statement. A simple IF statement performs action statements if the result of the condition is TRUE. If the condition is FALSE, no action is performed.
  • 20.
    Syntax: IF condition(s) THEN Actionstatements END IF; IF…THEN…ELSE…END IF: This statement is an extension of the simple IF statement. It provides action statements for the TRUE outcome as well as for the FALSE outcome.
  • 21.
    Syntax: IF condition(s) THEN Actionstatement 1 ELSE Action statement 2 END IF; IF…THEN…ELSIF…END IF: When you have many alternative /options, you can use previously explained statements, but the ELSFIF alternative is more efficient than the other two.
  • 22.
    Syntax: IF condition(s)1 THEN Actionstatements 1 ELSIF condition(s)2 THEN Action statements 2 … ELSIF condition(s)N THEN Action statement N [ELSE Else Action statements] END IF;
  • 23.
    CASE: The CASE statementis an alternative to the IF…THEN… ELSIF…END IF statement. The CASE statement begins with keyword CASE and ends with the keywords END CASE. Syntax: Case [variable_name] WHEN value1condition1 THEN action_statement1; WHEN value2condition2 THEN action_statement2; WHEN valueNcondition N THEN action_statementN; ELSE action_statement; END CASE;
  • 24.
    SEARCHED CASE: A statementwith a value is known as a CASE statement, and a statement with conditions is known as a searched CASE statement. A CASE statement uses variable_name as a selector, but a searched CASE does not use variable_name as a selector. NESTED IF: The nested IF statement contains an IF statement within another IF statement. If the condition in the outer IF statement is TRUE, the inner IF statement is performed.
  • 25.
    Looping structure: Looping meansiterations. A loop repeats a statement or series of statements a specific number of times as defined by the programmer. THREE TYPES  Basic loop  While loop  For loop BASIC LOOP A basic loop is a loop that is performed repeatedly. once a loop is entered all statements in the loop are performed.
  • 26.
    Syntax Loop Looping statement 1; Loopingstatement 2; ………. Looping statement N; Exit (when condition); End loop WHILE LOOP The while loop is an alternative to be basic loop and it performed as long as the condition is true terminates the condition becomes false.
  • 27.
    Syntax While condition loop Loopingstatement 1; Looping statement 2; ……. Looping structure n; End loop; FOR LOOP The for loop is the simples loop you can write. There is a no net and no use an exit statement, And the counter need not be declared.
  • 28.
    Syntax For counter in(reverse)lower….upper loop Looping statement1 Looping statement2 …… Looping statement N End loops NESTED BLOCKS PL/SQL block may contain another PL/SQL block ; in another words PL/SQL blocks can be nested.
  • 29.
    DATA MANIPULATION  Insertstatement  Delete statement  Update statement INSERT STATEMENT We will use an insert statement to add a new employee in the employee table. DELETE STATEMENT We will show the use of the delete statement in the PL/SQL block to remove some rows.
  • 30.
    UPDATE STATEMENT The updatestatement can be used in a PL/SQL block for modification off data. TRANSACTION CONTRAL STATEMENT The COMMIT statements to commit the current transaction. The SAVEPOINT statements to mark a point in your transaction. The ROLLBACK (TO SAVEPOINT N) statements to discard all or part of the transaction.