Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SQL PL/SQL
SQL is a natural language which is very
PL/SQL is a procedural extension of Oracle - SQL.
useful for interactive processing.
PL/SQL supports procedural capabilities as well as high
No procedural capabilities like condition
language features such as conditional statements, looping
testing, looping is offered by SQL.
statements, etc.
All SQL statements are executed by the PL/SQL statements send the entire block of statements to
database server one at a time, thus it is a the database server at the same time, thus network traffic is
time-consuming process. reduced considerably.
No error handling procedures are there in
PL/SQL supports customized error handling.
SQL.
PL/SQL allows access and sharing of the same sub programs by multiple applications.
PL/SQL is known for portability of code as code can be executed on any operating system
provided Oracle is loaded on it.
With PL/SQL user can write their own customized error handling routines.
Improved transaction performance with integration to Oracle data dictionary.
Answer: Data types define the ways to identify the type of data and their associated operations. There are
4 types of predefined data types explained as follows
Scalar Data Types: A scalar data type is an atomic data type that does not have any internal
components.
For example
o CHAR (fixed length character value between 1 and 32,767 characters)
o VARCHAR2 (variable length character value between 1 and 32,767 characters)
o NUMBER ( fixed-decimal, floating-decimal or integer values)
o BOOLEAN ( logical data type for TRUE FALSE or NULL values)
o DATE (stores date and time information)
o LONG (character data of variable length)
Composite Data Types: A composite data type is made up of other data types and internal
components that can be easily used and manipulated. For example RECORD, TABLE, and
VARRAY.
Reference Data Types: A reference data types holds values, called pointers that designate to
other program items or data items. For example REF CURSOR.
Large Object Data Types: A Large Object datatype holds values, called locators, that defines the
location of large objects( such as video clips, graphic image, etc) stored out of line.
For example
o BFILE (Binary file)
o BLOB (Binary large object)
o CLOB ( Character large object)
o NCLOB( NCHAR type large object)
Question #4) Explain the purpose of %TYPE and %ROWTYPE data types with the example?
Answer: PL/SQL uses %TYPE declaration attribute for anchoring. This attribute provides the datatype
of a variable, constant or column. %TYPE attribute is useful while declaring a variable that has the same
datatype as a table column.
For example, the variable m_empno has the same data type and size as the column empno in table emp.
m_empno emp.empno%TYPE;
%ROWTYPE attribute is used to declare a variable to be a record having the same structure as a row in a
table. The row is defined as a record and its fields have the same names and data types as the columns in
the table or view.
This declares a record that can store an entire row for DEPT table.
Answer: PL/SQL packages are schema objects that groups functions, stored procedures, cursors and
variables at one place. Packages have 2 mandatory parts
Package Specifications
Package body
Answer: PL/SQL requires a special capability to retrieve and process more than one row and that
resource is known as Cursors. A cursor is a pointer to the context area, which is an area of memory
containing SQL statements and information for processing the statements.
PL/SQL Cursor is basically a mechanism under which multiple rows of the data from the database are
selected and then each row is individually processed inside a PL/SQL program.
Answer: There are two types of cursors. They are explained as follows
1) Explicit Cursors: For queries that return more than one row, an explicit cursor is declared and named
by a programmer. In order to use explicit cursor in PL/SQL, 4 steps are followed
Where <cursor_name> is the name assigned to the cursor and SELECT statement is the query that returns
rows to the cursor active set.
Where <cursor_name> refers to the name of the previously defined cursor from which rows are being
fetched.
<record_list> represents the list of variables that will receive the data being fetched.
2) Implicit cursors: When any SQL statement is executed, PL/SQL automatically creates a cursor
without defining such cursors are known as implicit cursors.
INSERT
UPDATE
DELETE
SELECT ( queries that return exactly one row)
Answer: The word ‘Trigger’ means to activate. In PL/SQL, the trigger is a stored procedure that defines
an action taken by the database when database related event is performed. Triggers are mainly required
for the following purposes
Question #9) Explain the difference in execution of triggers and stored procedures?
Answer: A stored procedure is executed explicitly by issuing procedure call statement from another
block via a procedure call with arguments.
The trigger is executed implicitly whenever any triggering event like the occurrence of DML statements
happens.
Triggers Constraints
Affect all rows of the table including that
Only affect those rows added after the trigger is enabled.
already exist when the constraint is enabled.
Triggers are used to implement complex business rules Constraints maintain the integrity of the
which cannot be implemented using integrity constraints. database.
Question #11) What is a PL/SQL block?
Answer: In PL/SQL, statements are grouped into units called Blocks. PL/SQL blocks can include
constants, variables, SQL statements, loops, conditional statements, exception handling. Blocks can also
build a procedure, a function or a package.
1) Anonymous blocks: PL/SQL blocks without header are known as anonymous blocks. These blocks do
not form the body of a procedure, function or triggers.
Example:
1 DECLARE
2 num NUMBER(2);
3 sq NUMBER(3);
4 BEGIN
5 num:= &Number1;
6 sq := num*num;
7 DBMS_OUTPUT.PUT_LINE(‘Square:’ ||sq);
8 END;
2) Named blocks: PL/SQL blocks having header or labels are known as Named blocks. Named blocks
can either be subprograms (procedures, functions, packages) or Triggers.
Example:
Answer: Syntax errors are the one which can be easily identified by a PL/SQL compiler. These errors
can be the spelling mistake, etc.
Runtime errors are those errors in PL/SQL block for which exception handling section is to be included
for handling the errors. These errors can be SELECT INTO statement which does not return any rows.
Answer: COMMIT, SAVEPOINT, and ROLLBACK are three transaction specifications available in
PL/SQL.
COMMIT statement: When DML operation is performed, it only manipulates data in database buffer
and the database remains unaffected by these changes. To save/store these transaction changes to the
database, we need to COMMIT the transaction. COMMIT transaction saves all outstanding changes since
the last COMMIT and the following process happens
Syntax: COMMIT;
ROLLBACK statement: When we want to undo or erase all the changes that have occurred in the
current transaction so far, we require rolling back of the transaction. In other words, ROLLBACK erases
all outstanding changes since the last COMMIT or ROLLBACK.
ROLLBACK;
SAVEPOINT statement: The SAVEPOINT statement gives a name and marks a point in the processing
of the current transaction. The changes and locks that have occurred before the SAVEPOINT in the
transaction are preserved while those that occur after the SAVEPOINT are released.
Syntax:
SAVEPOINT <savepoint_name>;
Answer: A table which is currently being modified by a DML statement like defining triggers in a table
is known as a Mutating table.
A table that might need to be read from for a referential integrity constraint is known as constraining
table.
Answer: The variables or an expression referred to as parameters that appear in the procedure call
statement is known as Actual parameters.
Here in the above example, emp_num and amount are the two actual parameters.
The variables that are declared in the procedure header and are referenced in the procedure body are
called as Formal parameters.
For example:
PROCEDURE raise_sal( emp_id INTEGER) IS
curr_sal REAL:
………..
BEGIN
SELECT sal INTO cur_sal FROM emp WHERE empno = emp_id;
…….
END raise_sal;
Question #16) What is the difference between ROLLBACK and ROLLBACK TO statements?
Answer: The transaction is completely ended after ROLLBACK statement i.e. ROLLBACK command
completely undoes a transaction and release all locks.
On the other hand, a transaction is still active and running after ROLLBACK TO command as it undoes a
part of the transaction up till the given SAVEPOINT.
Question #17) Write a PL/SQL script to display the following series of numbers: 99,96,93……9,6,3?
Answer
Answer: 3 modes of the parameter are IN, OUT, IN OUT. These can be explained as follows
IN parameters: IN parameters allow you to pass values to the procedure being called and can be
initialized to default values. IN parameters acts like a constant and cannot be assigned any value.
OUT parameters: OUT parameters return value to the caller and they must be specified. OUT
parameters act like an uninitialized variable and cannot be used in an expression.
IN OUT parameters: IN OUT parameters passes initial values to a procedure and returns updated values
to the caller. IN OUT parameters act like an initialized variable and should be assigned a value.
Answer: An implicit cursor, SQL%ISOPEN attribute is always false because the implicit cursor is
opened for a DML statement and is closed immediately after the execution of DML statement.
Question #20) When a DML statement is executed, in which cursor attributes, the outcome of the
statement is saved?
Answer: The outcome of the statement is saved in 4 cursor attributes. These are
SQL%FOUND
SQL%NOTFOUND
SQL%ROWCOUNT
SQL%ISOPEN
Answer: Comments are the text which is included with the code to enhance readability and for the
understanding of the reader. These codes are never executed. There are two ways to comment in PL/SQL
2) Multi-line comment: This comment starts with /* and ends with */.
Example:
BEGIN
num := &p_num; /* This is a host variable used in program body */
……….
END
Answer: When an error occurs in PL/SQL, the exception is raised. In other words, to handle undesired
situations where PL/SQL scripts terminated unexpectedly, an error handling code is included in the
program. In PL/SQL, all exception handling code is placed in EXCEPTION section.
CURSOR_ALREADY_OPEN
INVALID_CURSOR
Question #25) Explain the difference between cursor declared in procedures and cursors declared
in the package specification?
Answer: The cursor declared in the procedure is treated as local and thus cannot be accessed by other
procedures.
The cursor declared in the package specification is treated as global and thus can be accessed by other
procedures.
Answer: The INSTEAD OF triggers are the triggers written especially for modifying views, which
cannot be directly modified through SQL DML statements.
Question #27) What are expressions?
Answer: Expressions are represented by a sequence of literals and variables that are separated by
operators. In PL/SQL, operations are used to manipulate, compare and calculate some data. An
expression is a composition of ‘Operators’ and ‘Operands’.
Operands: These are an argument to the operators. Operands can be a variable, function call or
constant.
Operators: These specify the actions to be performed on operators. E.g. ‘+’, ‘*’, etc.
Question #29) Write a program that shows the usage of WHILE loop to calculate the average of
user entered numbers and entry of more numbers are stopped by entering number 0?
Answer
1 DECLARE
2 n NUMBER;
3 avg NUMBER :=0 ;
4 sum NUMBER :=0 ;
5 count NUMBER :=0 ;
6 BEGIN
7 n := &enter_a_number;
8 WHILE(n<>0)
9 LOOP
10 count := count+1;
11 sum := sum+n;
12 n := &enter_a_number;
13 END LOOP;
14 avg := sum/count;
15 DBMS_OUTPUT.PUT_LINE(‘the average is’||avg);
16 END;
Answer: A PL/SQL records can be referred as a collection of values or say, a group of multiple pieces of
information, each of which is of simpler types and can be related to one another as fields.