You are on page 1of 10

Q4.

Discuss the pl SQL and it's features in detail


Ans4.PL/SQL stands for Procedural Language extensions to the Structured Query
Language (SQL). PL/SQL is a combination of SQL along with the procedural
features of programming languages. Oracle uses a PL/SQL engine to process the
PL/SQL statements. PL/SQL includes procedural language elements like conditions
and loops. It allows declaration of constants and variables, procedures and
functions, types and variables of those types and triggers. PL/SQL is a block
structured language that enables developers to combine the power of SQL with
procedural statements. All the statements of a block are passed to oracle engine all
at once which increases processing speed and decreases the traffic. PL/SQL is a
completely portable, high-performance transaction processing language. It provides
a built-in, interpreted and OS independent programming environment.PL/SQL can
also directly be called from the command-line SQL*Plus interface. PL/SQL's general
syntax is based on that of ADA and Pascal programming language. Apart from
Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.
Features of PL/SQL
1. PL/SQL is tightly integrated with SQL.
2. It offers extensive error checking.
3. It offers numerous data types.
4. It offers a variety of programming structures.
5. It supports object-oriented programming.
6. It supports the development of web applications and server pages.
7. It is a procedural language, which provides the functionality of decision
making, iteration and more features of procedural programming languages.
8. PL/SQL can execute a number of queries in one block using a single
command. One can create a PL/SQL unit such as procedures, functions,
packages, triggers, and types, which are stored in the database for reuse by
applications.
9. PL/SQL provides a feature to handle the exception which occurs in PL/SQL
block known as exception handling block.
10. Applications written in PL/SQL are portable to computer hardware or
operating systems where Oracle is operational.

Q5.Discuss the trigger. Name some instances when triggers can be used.
Ans5. Triggers in PL/SQL are special types of stored procedures that are
automatically executed or fired in response to specific events occurring in a
database. These events can be data manipulation operations (such as INSERT,
UPDATE, DELETE) or database operations (such as database startup or shutdown).
Triggers can be used in various scenarios to enforce business rules, maintain data
integrity, and automate tasks. Here are some instances when triggers can be used:

1. Enforcing Data Integrity: Triggers can be used to enforce complex data integrity
rules that cannot be enforced using constraints alone. For example, a trigger can be
created to prevent the deletion of a customer record if that customer has pending
orders in the system.

2. Audit Trail: Triggers can be used to track changes to specific tables for auditing
purposes. By creating triggers on INSERT, UPDATE, and DELETE operations,
developers can capture information such as who made the change, when the
change was made, and what data was modified.

3. Deriving Values: Triggers can be used to automatically derive values for certain
columns based on other column values. For example, a trigger can be created to
automatically update a "last_modified" column whenever a row is updated in a table.

4. Synchronisation: Triggers can be used to synchronise data between tables or


databases. For instance, a trigger can be created to update a summary table
whenever data in the underlying detail table is modified.

5. Notification: Triggers can be used to send notifications or alerts based on certain


database events. For example, a trigger can be created to send an email notification
to the administrator whenever a critical error occurs in the database.

6. Data Transformation: Triggers can be used to transform data before or after it is


inserted, updated, or deleted. This can include data validation, normalisation, or
conversion tasks.

7. Security Enforcement: Triggers can be used to enforce security policies or access


controls. For instance, a trigger can be created to prevent unauthorised users from
modifying sensitive data in a table.

8. Automatic Maintenance: Triggers can be used to perform automatic maintenance


tasks, such as archiving old data, based on predefined criteria.

9. Complex Constraint Enforcement: Triggers can be used to enforce complex


constraints that cannot be expressed using standard SQL constraints. For example,
enforcing a constraint that limits the total number of hours an employee can work in
a week.

10. Logging and Error Handling: Triggers can be used to log errors or exceptions
that occur during data manipulation operations, providing valuable information for
debugging and troubleshooting purposes.

Overall, triggers are powerful database objects that can be used to automate tasks,
enforce business rules, and maintain data integrity in a database system. However,
they should be used judiciously to avoid performance issues and unintended
consequences.
Q6.Difference between pl and SQL
Ans6.

Basis of Comparison SQL PL/SQL

Definition It is a database Structured It is a database programming


Query Language. language using SQL.

Variables Variables are not available Variables, constraints, and data


in SQL. types features are available in
PL/SQL.

Control structures No Supported Control Control Structures are available


Structures like for loop, if, like, for loop, while loop, if, and
and other. other.

Nature of Orientation It is a Data-oriented It is an application-oriented


language. language.

Operations Query performs the single PL/SQL block performs Group


operation in SQL. of Operation as a single block
resulting in reduced network
traffic.

Declarative/ Procedural SQL is a declarative PL/SQL is a procedural


Language language. language.

Embed SQL can be embedded in PL/SQL can’t be embedded in


PL/SQL. SQL.
Interaction with Server It directly interacts with the It does not interact directly with
database server. the database server.

Exception Handling SQL does not provide error PL/SQL provides error and
and exception handling. exception handling.

Writes It is used to write queries The code blocks, functions,


using DDL (Data Definition procedures triggers, and
Language) and DML (Data packages can be written using
Manipulation Language) PL/SQL.
statements.

Processing Speed SQL does not offer a high PL/SQL offers a high
processing speed for processing speed for
voluminous data. voluminous data.

Q7.Discuss implicit cursor and explicit cursor


Ans.7.

Implicit Cursors Explicit Cursors


Implicit cursors are automatically Explicit cursors need to be defined
created when select statements are explicitly by the user by providing a name.
executed.

They are capable of fetching a Explicit cursors can fetch multiple rows.
single row at a time.

Closes automatically after Need to close after execution.


execution.

They are more vulnerable to errors They are less vulnerable to errors(Data
such as Data errors, etc. errors etc.)

Provides less programmatic control User/Programmer has the entire control.


to the users

Implicit cursors are less efficient. Comparative to Implicit cursors, explicit


cursors are more efficient.

Implicit Cursors are defined as: Explicit cursors are defined as:

BEGIN DECLARE
SELECT attr_name from CURSOR cur_name IS
table_name SELECT attr_name from table_name
where CONDITION; where CONDITION;
BEGIN
END
...
Implicit cursors requires anonymous Explicit cursors use user-defined memory
buffer memory for storage purpose. space for storage purpose

Cursor attributes use prefix “SQL”. Structure for explicit cursors: cur_name
%attr_name
Structure for implicit cursors: SQL
%attr_name

Few implicit cursors attributes are: Few explicit cursors are: cur_name
SQL%FOUND, SQL%NOTFOUND, %FOUND, cur_name%NOTFOUND,
SQL%ROWCOUNT cur_name%ROWCOUNT

Q8. Discuss rollback and rollback 2 in pl sql


Ans 8.The ROLLBACK statement is used to back out changes.
The ROLLBACK statement can be used to either:
End a unit of work and back out all the relational database changes that were made
by that unit of work. If relational databases are the only recoverable resources used
by the application process, ROLLBACK also ends the unit of work.
Back out only the changes made after a savepoint was set within the unit of work
without ending the unit of work. Rolling back to a savepoint enables selected
changes to be undone.

ROLLBACK is not allowed in a trigger if the trigger program and the triggering
program run under the same commitment definition. ROLLBACK is not allowed in a
procedure if the procedure is called on a Distributed Unit of Work connection to a
remote application server or if the procedure is defined as ATOMIC. ROLLBACK is
not allowed in a function.
TO SAVEPOINT
Specifies that the unit of work is not to be ended and that only a partial rollback (to a
savepoint) is to be performed. If a savepoint name is not specified, rollback is to the
last active savepoint. For example, if in a unit of work, savepoints A, B, and C are set
in that order and then C is released, ROLLBACK TO SAVEPOINT causes a rollback
to savepoint B. If no savepoint is active, an error is returned.

Q9. Discuss what are the commit rollback and savepoint


Ans9.In database management systems, transactions play a crucial role in ensuring
data integrity and consistency. `COMMIT`, `ROLLBACK`, and `SAVEPOINT` are
commands used to manage transactions in SQL and PL/SQL environments. Here's a
detailed discussion of each:

1. COMMIT:
- The `COMMIT` command is used to permanently save the changes made in a
transaction to the database.
- When a `COMMIT` command is executed, all changes made in the current
transaction are made permanent, and the database is updated accordingly.
- Once a transaction is committed, the changes become visible to other users and
transactions.
- It's important to note that a `COMMIT` command ends the transaction, and after
committing, it's not possible to rollback the changes made in that transaction.

Example of `COMMIT`:
```sql
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 30;
DELETE FROM employees WHERE employee_id = 101;
COMMIT; -- Save changes made in the current transaction
END;
```

2. ROLLBACK:
- The `ROLLBACK` command is used to undo all changes made in the current
transaction since the last `COMMIT` or `SAVEPOINT`.
- When a `ROLLBACK` command is executed, all data changes (inserts, updates,
deletes) made in the current transaction are undone, and locks acquired by the
transaction are released.
- If a transaction encounters an error or needs to be aborted for any reason, a
`ROLLBACK` command can be issued to discard all changes made in that
transaction and return the database to its state before the transaction begins.

Example of `ROLLBACK`:
```sql
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 30;
DELETE FROM employees WHERE employee_id = 101;
ROLLBACK; -- Undo changes made in the current transaction
END;
```

3. SAVEPOINT:
- `SAVEPOINT` is used to define a point within a transaction to which you can later
rollback using the `ROLLBACK TO SAVEPOINT` command.
- Savepoints are useful when you want to selectively rollback part of a transaction
without discarding changes made later in the transaction.
- Multiple savepoints can be defined within a transaction, allowing for finer control
over rollback operations.

Example of `SAVEPOINT` and `ROLLBACK TO SAVEPOINT`:


```sql
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 30;
SAVEPOINT before_delete;
DELETE FROM employees WHERE employee_id = 101;
SAVEPOINT after_delete;
-- Some other operations...
ROLLBACK TO SAVEPOINT before_delete; -- Undo changes up to
'before_delete' savepoint
END;
```

In summary, `COMMIT` is used to permanently save the changes made in a


transaction, `ROLLBACK` is used to undo changes made in the current transaction,
and `SAVEPOINT` is used to define intermediate points within a transaction to which
you can later rollback selectively. These commands are essential for managing
transactions and ensuring data integrity in database systems.

Q10.Discuss the structure of PL SQL block


Ans7.In PL/SQL, All statements are classified into units that are called Blocks.
PL/SQL blocks can include variables, SQL statements, loops, constants, conditional
statements and exception handling. Blocks can also build a function or a procedure
or a package.

The Declaration section: Code block start with a declaration section, in which
memory variables, constants, cursors and other oracle objects can be declared and
if required initialized.

The Begin section: Consist of a set of SQL and PL/SQL statements, which describe
processes that have to be applied to table data. Actual data manipulation, retrieval,
looping and branching constructs are specified in this section.
The Exception section: This section deals with handling errors that arise during
execution data manipulation statements, which make up PL/SQL code blocks. Errors
can arise due to syntax, logic and/or validation rules.

The End section: This marks the end of a PL/SQL block.

Broadly, PL/SQL blocks are two types: Anonymous blocks and Named blocks are
as follows:

1. Anonymous blocks: In PL/SQL, That’s blocks which is not have header are known
as anonymous blocks. These blocks do not form the body of a function or triggers or
procedure. Example: Here a code example of find greatest number with Anonymous
blocks.

DECLARE
-- declare variable a, b and c
-- and these three variables datatype are integer
a number;
b number;
c number;
BEGIN
a:= 10;
b:= 100;
--find largest number
--take it in c variable
IF a > b THEN
c:= a;
ELSE
c:= b;
END IF;
dbms_output.put_line(' Maximum number in 10 and 100: ' || c);
END;
/
-- Program End
Output:

Maximum number in 10 and 100: 100


2. Named blocks: That’s PL/SQL blocks which having header or labels are known as
Named blocks. These blocks can either be subprograms like functions, procedures,
packages or Triggers. Example: Here a code example of find greatest number with
Named blocks means using function.
DECLARE

-- declare variable a, b and c


-- and these three variables datatype are integer
DECLARE
a number;
b number;
c number;
--Function return largest number of
-- two given number
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 10;
b:= 100;
c := findMax(a, b);
dbms_output.put_line(' Maximum number in 10 and 100 is: ' || c);
END;
/
-- Program End

You might also like