database management system chapter5
DESCRIPTION
Database management system, Oracle SQL and PL/SQL - Chapter5TRANSCRIPT
Teaching Aid Material
(www.phindia.com/gupta)
Authors invite your valuable feedback and suggestions on the [email protected]
DATABASE MANAGEMENT SYSTEM ORACLE SQL AND PL/SQL
PART II : Application Using Oracle PL/SQL
CHAPTER 5
Introduction to PL/SQL Programming
DATABASE MANAGEMENT SYSTEM ORACLE SQL AND PL/SQL
PART II : Application Using Oracle PL/SQL
CHAPTER 5Introduction to PL/SQL Programming
1 of 9
Why PL/SQL?
Better Performance
Higher Productivity
Full Portability
Tight Integration with SQL
Security
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
2 of 9
Character Set
Type Characters
Uppercase, lowercase A-Z , a-z
Digits 0-9
Mathematical and punctuation symbols
~ ! @ # % & * ( )_ - + = | [ ] { }: ; ’’ ’ < > ? /
White space Space, tab, carriage return
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
3 of 9
PL/SQL Identifiers
Identifiers are used to name PL/SQL objects such as constants,variables, exceptions, procedures, cursors and reserved words.
Identifiers can be up to 30 characters in length, must start with aletter and can include dollar sign, an underscore, and a pound sign.
Identifiers are case sensitive and cannot include space, tab orcarriage return. An identifier can be enclosed within double quotes.
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
4 of 9
PL/SQL Literals
Literals are specific values and are not represented by identifiers
Can be character, number or Boolean value
To embed single quotes within a string literal, two single quotes next to each other may be placed
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
5 of 9
PL/SQL Delimiters Delimiters are symbols with special meaning to PL/SQL Used to separate identifiers from each other
Delimiter Characteristics
-- Single-line comment indicator
/* */ Multiline comment delimiter
|| Concatenation operator
.. Range operator
; Statement terminator
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
6 of 9
PL/SQL Variable
Type Purpose
Scalar Numeric NUMBER(p, s)The maximum precision is 38 digits
Character VARCHAR2, CHAR, LONGCHAR and VARCHAR2 are up to 32767 bytes
Date Range is between 01-Jan-4712BC and 31-Dec-9999AD
Boolean Can hold TRUE, FLASE or NULL only
RAW Similar to CHAR variablesLONG RAW is similar to LONGLONG RAW can be up to 2 gigabytes
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
7 of 9
PL/SQL Variable
Type Purpose
LOB BFILE File locaters pointing to read only large objects in operating system files
BLOB BLOB locaters that point to large binary objects inside the database
CLOB CLOB locaters point to large character objects inside the database
NCLOB NCLOB locaters point large national character set objects inside the database
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
8 of 9
NULL Represents unknown values as NULL values NULL is never equal to anything NVL, IS NULL or IS NOT NULL
CONSTANT Requires an initial value Value cannot be changed
Page reference in the book: 147-152
CHAPTER 5Introduction to PL/SQL Programming
9 of 9
Default Value
A variable is assigned a default value of NULL while declaration
Can be initialized by assignment operator (: =)
Page reference in the book: 147-152
CHAPTER 5PL/SQL Structure
1 of 3
PL/SQL Structure
DeclareVariables, Cursors, Constants
BeginPL/SQL statementsException
Statements for error handlingEnd;
Page reference in the book: 152-159
CHAPTER 5PL/SQL Structure
2 of 3
IF Statement
Allows actions based upon conditions
IF statement can also be nested
Three forms of IF statements:
IF … THEN …IF … THEN … ELSE … IF … THEN … ELSIF … ELSE ….
Page reference in the book: 152-159
CHAPTER 5PL/SQL Structure
3 of 3
Loops in PL/SQL
Simple LoopLoop
PL/SQL statementsExit Condition
End Loop
For LoopFor variable IN Start..End
PL/SQL StatementsEnd Loop;
While LoopWhile <Condition> Loop
PL/SQL StatementsEnd Loop
Page reference in the book: 152-159
CHAPTER 5PL/SQL and Oracle
1 of 10
Scope of a Variable in Nested Block
PL/SQL statements can be nested
SQL Cursor
A cursor is a private SQL work area where all commandsdefined in the cursor are executed
There are two types of cursors: Implicit cursor Explicit cursor
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
2 of 10
Implicit Cursor
It is automatically created and handled by Oracle Server
Support all types of DMLs [Insert/Update/Delete]
Supports SELECT statement that returns only
Explicit Cursor
Needs to be declared explicitly by the programmer
It is handled using cursor-related commands
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
3 of 10
INTO Clause Into clause is mandatory in a PL/SQL program It is placed between SELECT and FROM clauses Acts as a container with the associated variable Query must return only one row
%TYPE Attribute
Used to declare variables that refer to the column
% ROWTYPE Attribute Represents a row in a table
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
4 of 10
Example EX5_1
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
5 of 10
EX5_2
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
6 of 10
EX5_3
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
7 of 10EX5_5
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
8 of 10
EX5_10
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
9 of 10EX5_11
Page reference in the book: 159-168
CHAPTER 5PL/SQL and Oracle
10 of 10
EX5_12
Page reference in the book: 159-168
CHAPTER 5Short/ Objective Type Question
Q1. Define identifiers, literals and delimiters in Oracle PL/SQL.
Q2. Explain %TYPE and %ROWTYPE attributes with the help of suitable examples.
Q3. What do you understand by SQL cursors?
Q4. Write a PL/SQL program which will accept three numbers and print the smallest among them.SQL>@ C:/TTP/Q4Enter value for a: 12Enter value for b: 34Enter value for c: 12Smallest Number is 12
Q5. Write PL/SQL program using Loop … End Loop, For Loop and While Loop, which will accept integer from 1 to 10 and print factorial. For example, factorial of 5 is 5*4*3*2*1=120.
SQL>@ C:/TTP/Q5Enter value for i_num: 6FACTORIAL OF 6 IS 720
CHAPTER 5Short/Objective Type Question
Q6. Write a PL/SQL program which will accept a number from 1 to 20 and display following the the figure.SQL>@ C:/TTP/Q8Enter value for i_num: 10
-- *---- *------ *-------- *---------- *------------ *-------------- *---------------- *------------------ *-------------------- *
Q7. The basic programming unit of a PL/SQL code is a (a) Procedure(b) Subprogram(c) Module(d) Block
Q8. Which of the following sections of a PL/SQL program are optional?(a) Declarative Section(b) Exception Handler Section(c) Executable Section(d) All of these
CHAPTER 5Home Assignment
WORKOUT
Periods Proposed – 18Chapter 5Introduction to PL/SQL Programming
1 Period [Theory]1 Period [Practical]
Chapter 6Oracle Function, Procedure And Package
1 Period [Theory]1 Period [Practical]
Chapter 7Oracle Exception Handler and Database Triggers
1 Period [Theory]1 Period [Practical]
Chapter 8Implicit and Explicit Cursors
1 Period [Theory]2 Periods [Practical]
Chapter 9Advance Cursors
1 Period [Theory]1 Period [Practical]
Chapter 10PL/SQL Collection
1 Period [Theory]1 Period [Practical]
Chapter 11Oracle Objects and Dynamic SQL
1 Period [Theory]1 Period [Practical]
Chapter 12Performance Tuning
2 Period [Theory]1 Period [Practical]
DATABASE MANAGEMENT SYSTEM ORACLE SQL AND PL/SQL
PART II : Application using Oracle PL/SQL
THE BOOKThe book is organized into three parts to introduce thetheoretical and programming concepts of DBMS. Part I(Basic Concepts and Oracle SQL) deals with DBMS basic,software analysis and design, data flow diagram, ER model,relational algebra, normal forms, SQL queries, functions,sub‐queries, different types of joins, DCL, DDL, DML, objectconstraints and security in Oracle. Part II (Application UsingOracle PL/SQL) explains PL/SQL basics, functions,procedures, packages, exception handling, triggers, implicit,explicit and advanced cursors using suitable examples. Thispart also covers advanced concepts related to PL/SQL, suchas collection, records, objects, dynamic SQL andperformance tuning. Part III (Advanced Concepts andTechnologies) elaborates on advanced database conceptssuch as query processing, file organization, distributedarchitecture, backup, recovery, data warehousing, onlineanalytical processing and data mining concepts and theirtechniques.
KEY FEATURES Includes about 300 examples to illustrate the concepts. Offers about 400 objective type questions. Provides about 100 challenging workouts.
TEACHING AID MATERIALTeaching Aid Material for all the chapters is provided on thewebsite of PHI Learning. Visit www.phindia.com/gupta toexplore the contents.
Visit PHI Learning Center www.phindia.com/gupta and Click on Instructor Resources toaccess Teaching Aid Material for all the 18 chapters of the book.