Está en la página 1de 5

Question #1) Differentiate PL/SQL and SQL?

Answer: Difference between SQL and PL/SQL can be categorized as follows

SQL PL/SQL
SQL is a natural language which is very
PL/SQL is a procedural extension of Oracle - SQL.
useful for interactive processing.
No procedural capabilities like PL/SQL supports procedural capabilities as well as
condition testing, looping is offered by high language features such as conditional
SQL. statements, looping statements, etc.
All SQL statements are executed by the PL/SQL statements send the entire block of
database server one at a time, thus it is a statements to the database server at the same time,
time-consuming process. thus network traffic is reduced considerably.
No error handling procedures are there
PL/SQL supports customized error handling.
in SQL.

Question #2) Enlist the characteristics of PL/SQL?

Answer: Characteristics of PL/SQL are as follows

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.

Question #3) What are the data types available in PL/SQL?

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.

For example: dept_rec dept%ROWTYPE;

This declares a record that can store an entire row for DEPT table.

Question #5) What do you understand by PL/SQL packages?

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

Question #6) What do you understand by PL/SQL cursors?

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.

Question #7) Explain cursor types?

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
Declare the cursor

Syntax: CURSOR <cursor_name> is


SELECT statement;

Where <cursor_name> is the name assigned to the cursor and SELECT statement is the query
that returns rows to the cursor active set.

Open the cursor

Syntax: OPEN <cursor_nam>;

Where, <cursor_name> is the name of the previously defined cursor.

Fetch rows from the cursor

Syntax: FETCH <cursor_name> INTO <record_list>;

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.

Closing the cursor

Syntax: CLOSE <cursor_name>;

Where <cursor_name> is the name of the cursor being closed.

2) Implicit cursors: When any SQL statement is executed, PL/SQL automatically creates a
cursor without defining such cursors are known as implicit cursors.

For following statements, PL/SQL employs implicit cursors

INSERT
UPDATE
DELETE
SELECT ( queries that return exactly one row)

Question #8) When do we use triggers?

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

To maintain complex integrity constraints


Auditing table information by recording the changes
Signaling other program actions when changes are made to table
Enforcing complex business rules
Preventing invalid transactions
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.

Question #10) Explain the difference between Triggers and Constraints?

Answer: Triggers are different from constraints in the following ways

Triggers Constraints
Affect all rows of the table including
Only affect those rows added after the trigger is
that already exist when the constraint is
enabled.
enabled.
Triggers are used to implement complex business
Constraints maintain the integrity of the
rules which cannot be implemented using integrity
database.
constraints.

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.

Broadly, PL/SQL blocks are two types

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:

1 FUNCTION sqr (num IN NUMBER)


2 RETURN NUMBER is sq NUMBER(2);
3 BEGIN
4 sq:= num*num;
5 RETURN sq;
6 END;

Question #12) Differentiate between syntax and runtime errors?

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.