You are on page 1of 4

1. What is PL/SQL ?

PL/SQL stands for Procedural Language extension of SQL.


PL/SQL is a combination of SQL along with the procedural features of programming
languages.

2. What is PL/SQL Table ?


Objects of type tables are called PL/SQL Table that are modeled as Database Table. PL/SQL
Tables are a way of providing arrays. Arrays are like temporary tables in memory that are
processed very quickly.
PL/SQL tables are used to move bulk data.
They simplifies moving collections of data.

3. How many datatypes are available in PL/SQL ?


There are two datatype available in PL/SQL. They are :
Scalar Datatypes : NUMBER, VARCHAR2, DATA, CHAR, LONG, BOOLEAN,
etc.
Composite Datatypes : RECORD, TABLE, etc.
a.

4. What is the basic structure of PL/SQL ?


PL/SQL uses BLOCK structure as its basic structure. Each PL/SQL Program consist of SQL
and PL/SQL statement which form a PL/SQL block.
PL/SQL block consists of 3 sections. They are :
(a) The Declaration Section (Optional)
(b) The Execution Section (Mandatory)
(c) The Exception Handling Section (Optional)
SYNTAX :
DECLARE
<declaration_section>
BEGIN
<executable_command>
EXCEPTION
<exception_handling>
END;
EXAMPLE :
DECLARE
V_MSG VARCHAR2 (50) := 'HELLO WORLD';
BEGIN
DBMS_OUTPUT.PUT_LINE(V_MSG);
EXCEPTION
<exception_handling>
END;

5. What is EXCEPTION ?
Exception is an error handling part of PL/SQL. There are two types of exceptions :
(a) Pre-defined Exception (Like TOO_MANY_ROWS, NO_DATA_FOUND,
VALUE_ERROR, ZERO_ERROR, INVALID_NUMBER,
CURSOR_ALREADY_OPEN, ZERO_DIVIDE)
(b) User-defined Exception
Whenever an error occurs Exception arises. Error is a bud whereas Exception is a warning
or error condition.
6. What is a TRIGGER ?
A trigger is a pl/sql block structure which is fired when a DML statements like Insert,
Delete, Update is executed on a database table. A trigger is triggered automatically
when an associated DML statement is executed.
Maximum number of triggers you can apply on a single table is 12. These types of
triggers in the PL/SQL contains the combination of BEFORE, AFTER, ROW,
TABLE, INSERT, UPDATE, DELETE and ALL keywords.
There are two types of triggers based on the which level it is triggered.
(a) Row level trigger - An event is triggered for each row upated, inserted or
deleted.
(b) Statement level trigger - An event is triggered for each sql statement
executed.
SYNTAX :
CREATE [OR REPLACE ] TRIGGER trigger_name

{BEFORE | AFTER | INSTEAD OF }

{INSERT [OR] | UPDATE [OR] | DELETE}

[OF col_name]

ON table_name

[REFERENCING OLD AS o NEW AS n]

[FOR EACH ROW]

WHEN (condition)

BEGIN

--- sql statements

END;
Syntax detail information :
CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a
trigger with the given name or overwrites an existing trigger with the same name.
{BEFORE | AFTER | INSTEAD OF } - This clause indicates at what time
should the trigger get fired. i.e for example: before or after updating a table.
INSTEAD OF is used to create a trigger on a view. before and after cannot be used
to create a trigger on a view.
{INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the
triggering event. More than one triggering events can be used together separated by
OR keyword. The trigger gets fired at all the specified triggering event.
[OF col_name] - This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific column is updated.
CREATE [OR REPLACE ] TRIGGER trigger_name - This clause creates a
trigger with the given name or overwrites an existing trigger with the same name.
[ON table_name] - This clause identifies the name of the table or view to which
the trigger is associated.
[REFERENCING OLD AS o NEW AS n] - This clause is used to reference the
old and new values of the data being changed. By default, you reference the values as
:old.column_name or :new.column_name. The reference names can also be changed
from old (or new) to any other user-defined name. You cannot reference old values
when inserting a record, or new values when deleting a record, because they do not
exist.
[FOR EACH ROW] - This clause is used to determine whether a trigger must
fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the
entire sql statement is executed(i.e.statement level Trigger).
WHEN (condition) - This clause is valid only for row level triggers. The trigger
is fired only for rows that satisfy the condition specified.

EXAMPLE :
CREATE TABLE product_price_history
(product_id number(5), product_name varchar2(32), supplier_name varchar2(32),
unit_price number(7,2) );

CREATE TABLE product


(product_id number(5), product_name varchar2(32), supplier_name varchar2(32),
unit_price number(7,2) );

-- Before Update Of a column of a table --


CREATE or REPLACE TRIGGER price_history_trigger

BEFORE UPDATE OF unit_price

ON product

FOR EACH ROW

BEGIN

INSERT INTO product_price_history

VALUES ;

(:old.product_id,

:old.product_name,

:old.supplier_name,

:old.unit_price);

END;

7. What is CONSISTENCY ?
CONSISTENCY simply means that users can see consistent view of data.
EXAMPLE :
If there are two users A and B. A transfers money to B's account. Here the changes are
updated in A's account (debit) but untill it will be updated in B's account (credit), till
then the other users can't see the debit of A's account. After the debit of A and credit of
B's account, one can see the updates. That's Consistency.

8. What is CURSOR ?
A Cursor is a temporary work area created in the system memory when an SQL
statement is executed.
A Cursor contains information on a selected statement and the rows of data accessed by
it. This temporary work area stores the database and manipulates this data.
A Cursor can hold more than one row, but can process only one at a time.
Cursor are required to process row individually for queries.
There are two types of cursors in PL/SQL :
(a) Implicit Cursor : These are created by default when DML statements like INSERT,
UPDATE and DELETE are executed. They are also created when a SELECT
statement that returns just one row is executed.
(b) Explicit Cursor : They must be created when you are executing a SELECT
statement that returns more than one row.