1. What is PL/SQL?Answer:
PL/SQL is a Procedural Language extension to SQL and is used in oracle. It combines SQL with procedural feature of programming language and allows to write code in procedural format. As the PL/SQL program are compiled by Oracle database server, it has robustness, security and portability of Oracle.
PL/SQL architecture comprises of 3 components, namely:
It has actual PL/SQL code.
Here processing of the code takes place.
This stores data. PL/SQL uses SQL to interact with database server.
2. What are the advantages of using PL/SQL?Answer:
PL/SQL engine provides fast code execution. It processes a block with multiple SQL statements simultaneously that reduces network traffic, thereby giving better performance.
With PL/SQL, we can use procedural language constructs such as conditional statements and loops along with SQL data manipulation.
PL/SQL offers error handling features to handle exceptions and give user-friendly message to resolve the issue.
PL/SQL application is highly portable to any OS where Oracle is operational.
It supports Object Oriented Programming concepts.
3. Explain the difference between PL/SQL and SQL.Answer:
Unlike SQL, PL/SQL supports procedural language capabilities like conditional statements, looping etc.
PL/SQL sends entire block of statements to the database server for processing, thereby reducing network traffic. SQL statements are processed one at a time.
PL/SQL offers highly effective error handling mechanism. SQL doesn't support error handling.
4. What are the datatypes available in PL/SQL?Answer:
A datatype of a variable, constant or parameter specifies a storage format.
PL/SQL offers following datatypes:
such as CHAR, VARCHAR2, NUMBER, BOOLEAN, DATE, LONG.
Large Object (LOB):
It is pointer to larger objects that are stored separately from other data item such as video clips, graphic image etc.
This type is made up of other internal components such as RECORD, TABLE, and VARRAY.
This type holds pointers to other data item. REF CURSOR is an example of reference type.
5. What are the purposes of %Type and %RowType data types?Answer:
%Type attribute provides the datatype of a variable, constant or column. It is used to declare a variable that has the same datatype as a table column.
Here, m_studentid has the same data type and size as the column studentid in table student.
%ROWTYPE is used to declare a variable of a record having a same structure as a row in a table.
This declares a record that can store an entire row for STUDENT table.
6. Explain packages in PL/SQL.Answer:
PL/SQL package is a logical grouping of a related subprogram (procedure/function) into a single element. Package is compiled and stored as a database object that can be used later.
PL/SQL package have two components:
consists of a declaration of all the public variables, cursors, objects, procedures, functions, and exception
consists of definition of all the elements that are present in the package specification.
7. Explain PL/SQL cursors.Answer:
Cursors allows to select multiple rows of the data from the database and manipulate each row. It is a temporary work area created in the memory to hold rows return by a SQL Statement. It holds multiple rows but can process only one row at a time.
There are two types of cursors:
This gets created automatically when a SQL statement is executed. All DML statements like Insert, Update and Delete are associated with an implicit cursor. For Insert statement, the cursor holds the data to be inserted. It has attributes such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.
An explicit cursor names the unnamed work area that stores rows returned by the SQL statement. The programmer can access and process rows using explicit cursor.
8. What are triggers in PL/SQL?Answer:
A Trigger is a special type of stored procedure that fires automatically when a DML statements like Insert, Delete, Update are executed on a database table.
Triggers are used for the following purposes:
Types of triggers based on which level it is triggered:
Row level trigger -
- It can be used to audit changes occurred in the table.
- It can be used to enforce complex business rules.
- It can also prevent invalid transactions.
An event is triggered for each row updated, inserted or deleted.
Statement level trigger -
An event is triggered for each SQL statement executed.