Dbms Unit-5
Dbms Unit-5
UNIT-5
STRUCTURE OF PL / SQL
1) Explain about the structure of PL / SQL program?
PL / SQL:
PL /SQL stands for procedural language extension to structured query language.
PL/ SQL is a combination of sql along with the procedural features of programming
language.
It was developed by oracle corporation in early 1990’s to enhance the capabilities of
SQL.
PL/SQL incorporates many of the advanced features of programming languages that
was designed during 1970 and 1980’s.
It is not a case sensitive language.
PL/SQL is the super set of SQL.
It fully supports SQL commands and datatypes.
It allows the data manipulation and query statements of SQL to be included in
PL/SQL block structure.
Oracle uses PL/SQL engine to processes the pl/sql statements.
K.UMADEVI-AWDCKKD 1
BSC-DBMS
DECLARE(optional)
Declaration statements;
BEGIN(Mandatory)
Execution statements;
EXCEPTION(optional)
Exception handling statements;
END;
1) DECLARATION SECTION;-
K.UMADEVI-AWDCKKD 2
BSC-DBMS
RULES:
ADVANTAGES OF PL/SQL:
K.UMADEVI-AWDCKKD 3
BSC-DBMS
K.UMADEVI-AWDCKKD 4
BSC-DBMS
a) NO CODING NEEDED:
It is very easy to manage the database system without any need to write the large
amount of code by using the standard SQL.
b) WELL DEFINED STANDARD:
It has well defined standard like ISO & ANSI.
c) PORTABILITY:
SQL can be used in the program like pcs, servers laptops and even some of the
mobile phones.
d) INTERACTIVE LANGUAGE:
The domain languages can be used for communicating with the database and
receive answer to the complex question in seconds.
e) MULTIPLE DATA VIEWS:
With the help of SQL language the users can make different views of database
structure and database for the different users.
a) DIFFICULT INTERFACES:
SQL has a complex interface that makes it difficult for some users to access it.
b) PARTIAL CONTROL: The programmer who use SQL does not have a full
control over the database because of hidden business rules.
c) IMPLEMENTATION:
Some of the database go to the popritery extension to standard SQL for
ensuring the vendor location.
d) COST:
The operating cost of some SQL versions makes it difficult for some
programmers to access it.
Different symbols in pl/sql
K.UMADEVI-AWDCKKD 5
BSC-DBMS
Symbol Meaning
+ addition operator
% attribute indicator
. component selector
/ division operator
, item separator
* multiplication operator
= relational operator
; statement terminator
- subtraction/negation operator
:= assignment operator
|| concatenation operator
** exponentiation operator
K.UMADEVI-AWDCKKD 6
BSC-DBMS
Symbol Meaning
.. range operator
!= relational operator
~= relational operator
^= relational operator
K.UMADEVI-AWDCKKD 7
BSC-DBMS
ii) IF-THEN-ELSE-ENDIF;
The general syntax is;
If condition then
Statements;
else
Statements;
End if;
iii) IF-THEN-ELSEIF-ENDIF:
The general syntax is
If condition then
Statements;
elseif condition then
Statements;
else
Statements;
end if;
declare
A number;
B number;
C number;
Begin
A:=10;
K.UMADEVI-AWDCKKD 8
BSC-DBMS
B:=20;
C:=30;
if(a>=b) and (a>=c) then
dbms_output.put_line(A||‘is big’);
elsif(b>a) and (b>=c) then
dbms_output.put_line(B|| ‘is big’);
else
dbms_output.put_line(C|| ‘is big’);
end if;
end;
OUTPUT: 30 is big
B) CASE EXPRESSION:
else
K.UMADEVI-AWDCKKD 9
BSC-DBMS
Statements;
end case;
K.UMADEVI-AWDCKKD 10
BSC-DBMS
EXIT STATEMENTS:
Example: declare
i number;
begin
K.UMADEVI-AWDCKKD 11
BSC-DBMS
i :=100;
loop
dbms_output.put_line(i);
i:=i+1;
exit when i>110;
end loop;
end;
b) WHILE LOOP:
This loop is used to repeat a sequence of statements until the condition is
false.
The condition is evaluated at the start of each iteration.
The loop terminates when the condition is false
Statements;
…………………..
End loop;
declare
i number;
begin
i:=20;
while i<=30 loop
dbms_output.put_line(i);
i:=i+1;
end loop;
Output: 20 21 22 23 24 25 26 27 28 29 30
PL/SQL procedure successful completed
C) FOR LOOP:
This loop is used to repeat a sequence of statement until the condition is false.
The condition is evaluated at the start of each iteration.
K.UMADEVI-AWDCKKD 12
BSC-DBMS
statements;
……………….
end loop;
Counter: It is an implicitly declared integer variable whose value automatically increase
(or)decreases.
REVERSE: It is used to decrement the iteration in for loop.
LOWER BOUND: it specifies a minimum value in a given range.
UPPER BOUND: It specific a maximum value in a given range.
Example: /*for loop*/
Declare
i number;
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
Output: 1 2 3 4 5 6 7 8 9 10
PL/SQL procedure successfully completed.
6.Write about sequential control statements in PL/SQL?
Sequential control statements constructors:
These are also called as unconditional control statements. By default all PL/SQL blocks are
executed in a top down sequential process.
The process begins with a begin statement and ends with a END statements so to
change the sequence conditional statements.
1) Go to
2) Null
1) GOTO STATEMENT:
The GOTO statements immediately transfers program control to the label name
unconditionally.
The statements (or) label name must be unique in the block.
K.UMADEVI-AWDCKKD 13
BSC-DBMS
Syntax:
Goto<<label name>>;
Example: /*goto*/
begin
dbms_output.put_line(‘HI’);
goto abc;
dbms_output.put_line(‘HELLO’);
<<abc>>
dbms_output.put_line(‘BYE’);
end;
2)NULL STATEMENTS:
The NULL statements does nothing other than pass control to the next
statements.
In some cases, if you want to tell PL/SQL to don nothing then in such cases the null
statements to be used.
SYNTAX: null;
Example: /*program for null statements*/
Declare
a number;
begin
a:=&a;
if a mod 2!=0 then
dbms_output.put_line(‘number is odd’);
else
null;
end if;
end;
output: Enter value for a: 1
old 4: a:=&a;
new 4: a:=1;
number is odd PL/SQL procedure successfully completed.
K.UMADEVI-AWDCKKD 14
BSC-DBMS
Operator Operation
+, - identity, negation
*, / multiplication, division
+, -, || addition, subtraction,
concatenation
=, !=, <, >, <=, >=, <>, ^=, IS NULL, LIKE, comparison
BETWEEN, IN
AND conjunction
OR disjunction
** Exponentiation
K.UMADEVI-AWDCKKD 15
BSC-DBMS
K.UMADEVI-AWDCKKD 16
BSC-DBMS
dbms_output.put_line(‘employee no’||eno);
dbms_output.put_line(‘employee name’ ||name);
dbms_output.put_line(‘employee salary’ ||salary);
end;
Output: employee no7369
employee nameSMITH
employee salary800
PL/SQL procedure successfully completed.
% ROW TYPE:
The row type attribute provides a record type that represents a row in a
database table.
The record can store an entire row of data selected from the table (or) fetched
from a cursor (or) cursor variable.
Example: x emp % row type;
Here the variable ‘x’ can store all the columns data of emp table.
declare
e emp%rowtype;
begin
select * into e from emp where empno=7369;
dbms_output.put_line(‘employee no’ ||e.empno);
dbms_output.put_line(‘employee name’ ||e.ename);
dbms_output.put_line(‘employee deptno’ ||e.deptno);
dbms_output.put_line(‘employee salary’ ||e.sal);
end;
output: employee no7369
employee nameSMITH
employee deptno20
employee salary800
K.UMADEVI-AWDCKKD 17
BSC-DBMS
EXCEPTION HANDLING:
Exception block in PL/SQL program takes an appropriate action against the error
condition.
when an error occurs, an exception is raised i.e the normal execution stops and the
control transfers to the exception handling path of the PL/SQL block.
ADVANTAGES:
1) pre-defined exception
2)user-defined exceptions
1) pre-defined exceptions:
These are internally defined by runtime system.
There are many pre-defined exceptions in PL/SQL which are executed when
any database rule is violated by the programers.
The exception handling part is used when the exception occurs.
Syntax: exception
Statements;
Statements;
K.UMADEVI-AWDCKKD 18
BSC-DBMS
Declare
n number;
begin
n:=10/0;
exception
when ZERO_DIVIDE then
dbms_output.put_line(‘division by zero error’);
end;
Output: division by zero error
PL/SQL procedure successfully completed.
K.UMADEVI-AWDCKKD 19
BSC-DBMS
2.USER-DEFINED EXCEPTION:
We can also define our own exceptions. These are declared and defined by user.
These are declared in the declaration selection of PL/SQL blocks with their type as
exception.
They must be raised explicitly using the raise statement.
RAISING AN EXCEPTION:
A user defined exception is raised explicitly by issuing the raise statement within the begin block.
Syntax: declare
begin
………………………………………
exception
statements;
ends;
K.UMADEVI-AWDCKKD 20
BSC-DBMS
HEADER: The header contains the name of the procedure and the parameter (or) the variable
passed to the procedure.
BODY: The body contains the declaration section, execution section, and exception section to
a general PL/SQL blocks.
CREATING A PROCEDURE:
We can create a new procedure with the create procedure statement, which may
declare a list of parameter.
The procedure must define the actions to be performed by the standard PL/SQL blocks.
PASSING PARAMETERS: we can pass parameters to procedure in three ways these are called as
modes .They are
i. IN PARAMETERS
ii. OUT PARAMETERS
iii. IN OUT PARAMETES
1) IN PARAMETER:
These types of parameters are used to pass values to stored procedures.
It is read only parameter.
2) OUT PARAMETER:
These types of parameters are used to send output to stored procedure.
It is similar to a return type in functions
It is write only parameter.
K.UMADEVI-AWDCKKD 21
BSC-DBMS
3) IN OUT PARAMETER:
It is Read-Write parameter.
The syntax of creating procedure is
Is/as
[declaration section]
begin
<procedure body>
exception
exception section
end[procedure name];
K.UMADEVI-AWDCKKD 22
BSC-DBMS
Z:=x+y;
dbms_output.put_line(‘sum is’ ||Z);
end P;
Output: Procedure created
1) EXECUTE:
The procedure can be executed by passing execute keyword.
Ex: sql> execute p(10,20);
2) CALLING A PROCEDURE:
A procedure can be called from any PL/SQL program by giving it names followed by
parameters.
Example:
To call the procedure ‘p’ the following statement will be written in a PL/SQL program.
/*calling procedure*/
Begin
p(10,20);
end;
K.UMADEVI-AWDCKKD 23
BSC-DBMS
1) HEADER:
The header contains name of the functions and parameters passed to the function
and return clause is used to specify the return datatype.
2) BODY:
The body contains declaration, execution and exception section similar to general
PL/SQL block.
CREATING A FUNCTION:
We can create a new function with the create function statements which
may declare a list of parameters.
The function must contains a return statement. The function must define
the action to be performed by Standard PL/SQL block.
1) IN PARAMETER
2) OUT PARAMETER
3) IN/OUT PARAMETER
1)IN PARAMETER:
K.UMADEVI-AWDCKKD 24
BSC-DBMS
3) OUT PARAMETER:
These type of parameter are used to send output from stored functions.
It is a write only parameters.
4) IN/OUT PARAMETERS:
These type of parameter are used to pass values and send output to from function
Syntax:
The syntax of creating functions
Create or replace function function_name (parameter [IN/OUT/IN OUT] datatype)
return type
Is/as
<declaration section>
Begin
<function section>
Exception
<exception section>
End function_name;
K.UMADEVI-AWDCKKD 25
BSC-DBMS
/*function program*/
A function can be called from any PL/SQL program by giving its name followed by
parameters.
A function may accepts one (or) more parameters but returns a single value.
/*calling function*/
Declare
S number;
begin
S:=F(11,12);
dbms_output.put_line(‘sum is’ ||S);
end;
Output: sum is23
PL/SQL procedure successfully completed.
DROPPING A FUNCTION: To drop a function drop function statement is used.
K.UMADEVI-AWDCKKD 26
BSC-DBMS
Example: SQL>@addition;
10/
Sum is 10
Pl/sql procedure successfully completed
After execution it displays errors if any (or) the below statement appears.
PL/SQL procedure successfully completed
K.UMADEVI-AWDCKKD 27
BSC-DBMS
RULES:
1) The programname should not have space.
2) Any message should be tybe between ‘________’ (single quotes)
3) Every line in the program ends with semicolon (;).
4) The output statement is Dbms_output.put_line
1.IMPLICT CURSORS
The implicit cursors are automatically generated by oracle while an SQL
statement is executed.
These are created by default when DML statements like insert, update, delete
etc………. are executed.
IMPLICIT CURSOR ATTRIBUTES: Oracle provides some attributes know as implicit cursor attributes
to check the status of DML operations.These are used with SQL keyword. Some of them are
a) % FOUND
b) %NOTFOUND
K.UMADEVI-AWDCKKD 28
BSC-DBMS
c) %ROWCOUNT
d) %ISOPEN
2.EXPLICIT CURSORS:
These are defined by the programmers to gain more control over the context
area.
These cursors should be defined in the declaration section of PL/SQL.
It is created on a select statements which returns more than one row.
Steps to create explicit cursor:The following steps used to create explicit cursors.
Cursor is a keyword.
Select statement is used for select query which returns multiple rows.
K.UMADEVI-AWDCKKD 29
BSC-DBMS
Syntax: fetch<cursor_name>into<var_list>;
Syntax: close<cursor_name>;
These attributes are used to check the status of the explicit cursor.
ATTRIBUTE EXAMPLE
1. %FOUND Cursor_name %found
2. %NOTFOUND Cursor_name %notfound
3. %ROWCOUNT Cursor_name %rowtype
4. %ISOPEN Cursor_name %isopen
/*explicit cursor*/
Declare
no emp.empno% type;
name emp.ename%type;
salary emp.sal%type;
cursor c1 is select empno, ename, sal from emp where sal>2000;
begin
open c1;
loop
fetch c1 into no, name, salary;
exit when c1%notfound;
dbms_output.put_line(no||’ ‘||name||’ ‘|| salary);
end loop;
close c1;
end;
ouput:7839 KING 5000
7698 BLAKE 2850
7782 CLARK 2450
K.UMADEVI-AWDCKKD 30
BSC-DBMS
Syntax:
Create [or replace] package package_name
{is/as}
<procedure_specification>;
K.UMADEVI-AWDCKKD 31
BSC-DBMS
<function_specification>;
<variable_declaration>;
<cursor_declaration>;
<exception_declaration>;
end [package_name];
Here
package is keyword.
package_name is defined by the user.
The elements which are all declare in the specification can be accessed form
outside of the package. Such elements are known as public elements.
Example:
/*package specification*/
create or replace package pack as procedure pro (no emp.empno%type);
function fun (x number, y number) return number;
End pack;
Output: Package created
2) PACKAGE BODY:
It consists of definition of all the elements that are present in the package
specification.
It fully defines cursors, procedures and functions declared in the package
specification.
It can also have private elements that are not declared in package specification.
To create package body, use the ‘create package body statement.
Syntax:
Create [or replace] package body package_name
{is/as}
<global declaration part>;
K.UMADEVI-AWDCKKD 32
BSC-DBMS
Advantages:
K.UMADEVI-AWDCKKD 33
BSC-DBMS
Creating a trigger:
Create (or) replace: It creates or replaces an existing trigger with the trigger_name.
Before/ater/instead of:This specifies when the trigger would be executed trigger on a
view.
Insert (or) /update(or)/delete:it specifies the DML operations.
Of col_name:This specifies the column name that would be updated
On table_name:This specifies the name of the table associated with the trigger.
Referencing old as a new as n:It refers to the old and new values for various DML
statements.
: old and :new are used to refer the columns.
Example: :old.col_name
:new.col_name
K.UMADEVI-AWDCKKD 34
BSC-DBMS
For each row:it specifies a row level trigger i.e the trigger would be executed for each row
being effected
when <condition>:it refers to the condition for rows for which the trigger would fire.
Types of triggers:depending upon the execution of a trigger it may be classified as
statement_level trigger(table)
Row_level trigger
Before trigger
After trigger
Instead of trigger
1) Statement_level trigger:
The trigger would be executed when delete (or) insert (or) update are applied on the
table.
2) Row_level trigger:
This trigger is fired when for each row statement is executed
3) Before trigger:
This trigger is fired before the certain action is performed
4) After trigger:
This trigger is fired after the certain action is performed
5) Instead of trigger:
This trigger is fired instead of performing the particular action
/*before trigger*/
Create or replace trigger t1
Before insert on emp
For each row
Begin
:new.ename:=upper(:new.ename);
End;
Output: Trigger created
K.UMADEVI-AWDCKKD 35
BSC-DBMS
The record type has to be defined before its record can be declared i.e to declare a
record you must first define the record type (a composite type ) then declare a record for
that type.
IN THE SYNTAX:
K.UMADEVI-AWDCKKD 36
BSC-DBMS
Declaring a record:
Syntax to declare a record of a user defined record type is
Record_namerecord_type_name;
Declare
Type pks is record
{
Snoemp.eno%tyhpe;
Name emp.ename%tyhpe;
Salary emp.sal%type;
};
p, pks
begin
select eno, ename, sal into p, form emp where eno=1;
dbms_output.put_line(‘employe_no’||’ ‘||name:’||p.name||’ ‘||sal: ||p.salary);
end;
17.Explain about fundamental |(or) language element of PL/SQL?
FUNDAMENTAL OF PL/SQL:
Like every other programming language PL/SQL has a set of elements. These elements are
used to represent real world objects and operation.
They are
1) Character set
2) Reserved words
3) Delimiters
4) Identifiers
5) Literals
6) Lexical units
1) Character set:
PL/SQL programs are written as lines of text using a specific set of characters.
The PL/SQL character set includes
A-Z (or) a-x letters/ alphabets
K.UMADEVI-AWDCKKD 37
BSC-DBMS
0-9 numbers
Specific symbuls( ), +, -, /, <>……….
3) DELIMETERS:
Delimiters are simple (or) compound symbols that have special meaning to PL/SQL.
SYMBOL MEANING
< > Not equal to
+, -, /, * Arithmetic operation
@ Remote access indicator
-- Single line comment
|*………….*| Multi line comment
:= Assignment operator
4) Identifiers:
Identifiers refers to the programs items and units, which include constants, variable,
exception, cursors, subprograms, packages etc……
Identifiers can contain upto 30 characters, but they must start with an alphabetic
characters.
5) Literals:
Literals are the explicit numeric character string (or) Boolean values which are not
represented by an identifiers. TRUE, NULL etc… are all the literals of the type
Boolean.
The different types of literals are:
K.UMADEVI-AWDCKKD 38
BSC-DBMS
6) Lexical units:
A line of PL/SQL contains group of characters known as lexical unit, which can be
classified as follows
Delimiters
Identifiers
Literals
Comments
Syntax:
Example: declare
Salary number(6);
K.UMADEVI-AWDCKKD 39
BSC-DBMS
Example:salnumber(3):=10000;
(or)
Sal:=&sal;
Example: write any PL/SQL program for essay
CONSTANT:
1) A constant is a value used I PL/SQL block that remains unchanged throughout
the program.
2) It is a user-defined literal value
3) It is also used with constant keyword
Syntax:var_name constant datatype:=value:
Example: p: constant float:=3.14;
EXAMPLE: WRITE RECORD PROGRAM FOR ESSAY
K.UMADEVI-AWDCKKD 40
BSC-DBMS
DBMS-V(A) IMPORTANT
QUESTIONS ESSAYS
1. Explain about File based System and its drawbacks?
2. Explain about architecture of DBMS
3. Explain about various Data Models?
4. Explain about EER Model?
5. Explain about the basic building blocks of ER-Model?
6. Explain about CODD Rules?
7. Explain about Relational Algebra and its operations?
8. Explain about DML & DDl commands?
9. Explain about Relational Set operators?
10.Explain about joins in sql?
11.Explain about Cursors in pl/sql?
12.Explain about procedures in pl/sql?
13.Explain about triggers in pl/sql?
14.Explain about Exception handling?
SHORTS
1. Write about the components of database?
2. List out the advantages of DBMS?
3. Write about the classification entity sets?
4. Write about the classification of attributes?
5. Write about the classification of relationships?
6. Generalization and Specialization?
7. Write about different types of keys?
8. Write about QBE?
9. Selection and projection operation?
10. Datatypes in sql?
11. Aggregate functions in sql
12. Constraints?
13. Structure of pl/sql
14. Advantages/features of pl/sql
15. Functions in pl/sql?
16. Constraints on Specialization and Generalization?
K.UMADEVI-AWDCKKD 41
BSC-DBMS
B.Sc(Computer Science)
Paper – DBMS
Previous Paper -1
Time:3 Hours Maximum marks:75
SECTION-A
SECTION – B
(OR)
(OR)
b) Discuss about generalization and specialization. What are the constraints on specialization and
generalization.
(OR)
(OR)
(OR)
K.UMADEVI-AWDCKKD 42
BSC-DBMS
B.Sc(Computer Science)
Paper – DBMS
Previous Paper- 2
Time:3 Hours Maximum marks:75
SECTION-A
8.QBE.
SECTION – B
(OR)
(OR)
(OR)
(OR)
(OR)
K.UMADEVI-AWDCKKD 43
BSC-DBMS
B.Sc(Computer Science)
Paper – DBMS
Previous Paper- 3
Time:3 Hours Maximum marks:75
SECTION-A
SECTION – B
(OR)
(OR)
(OR)
(OR)
(OR)
K.UMADEVI-AWDCKKD 44