1. What is PL/SQL? How is it different from SQL?
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension of SQL. It allows combining SQL with procedural constructs like loops, conditionals, and error handling to create powerful database programs.
Differences Between PL/SQL and SQL:
Feature | SQL | PL/SQL |
---|---|---|
Purpose | Data querying and manipulation. | Procedural logic and SQL integration. |
Execution | Executes one statement at a time. | Executes a block of statements. |
Error Handling | Limited | Robust exception handling. |
Code Example:
SQL:
sqlSELECT salary FROM employees WHERE employee_id = 101;
PL/SQL:
plsqlDECLARE v_salary NUMBER; BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); END; /
2. Explain the structure of a PL/SQL block.
A PL/SQL block is divided into three sections:
- Declaration Section (
DECLARE
): Used for declaring variables, constants, and other objects (optional). - Execution Section (
BEGIN
...END
): Contains the actual executable code (mandatory). - Exception Handling Section (
EXCEPTION
): Handles runtime errors (optional).
Code Example:
plsqlDECLARE v_salary NUMBER; -- Declaration section BEGIN SELECT salary INTO v_salary FROM employees WHERE employee_id = 101; -- Execution section DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No employee found.'); END; /
3. What are the advantages of using PL/SQL?
- Tight Integration with SQL: Combines SQL with procedural logic.
- Error Handling: Robust mechanisms for handling runtime errors.
- Improved Performance: Executes blocks of SQL statements on the server, reducing network traffic.
- Modularity: Supports reusable procedures, functions, and packages.
- Portability: PL/SQL code runs on any Oracle Database.
4. What is the difference between an anonymous block and a named block?
Feature | Anonymous Block | Named Block |
---|---|---|
Definition | Unnamed block of PL/SQL code. | Named and stored in the database. |
Reusability | Not reusable. | Reusable across sessions. |
Storage | Not stored in the database. | Stored in the database. |
Execution | Executed directly. | Invoked using its name. |
Anonymous Block Example:
plsqlBEGIN DBMS_OUTPUT.PUT_LINE('This is an anonymous block.'); END; /
Named Block (Procedure) Example:
plsqlCREATE OR REPLACE PROCEDURE greet_user IS BEGIN DBMS_OUTPUT.PUT_LINE('This is a named block (procedure).'); END; /
Calling the Named Block:
plsqlBEGIN greet_user; END; /
5. What are the different sections in a PL/SQL block?
Declaration Section:
- Declares variables, constants, and cursors.
- Optional.
Execution Section:
- Contains executable statements.
- Mandatory.
Exception Handling Section:
- Handles errors or exceptions that occur in the execution section.
- Optional.
Code Example:
plsqlDECLARE v_name VARCHAR2(50); -- Declaration BEGIN v_name := 'John Doe'; -- Execution DBMS_OUTPUT.PUT_LINE('Name: ' || v_name); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred.'); -- Exception Handling END; /
6. Explain the differences between PL/SQL and other procedural programming languages.
Feature | PL/SQL | Other Procedural Languages (e.g., Java) |
---|---|---|
Integration | Built-in support for SQL. | Requires connectors (e.g., JDBC for Java). |
Execution Environment | Runs directly on the Oracle Database. | Runs on application servers or VMs. |
Error Handling | Exception handling tied to SQL errors. | General-purpose error handling. |
Code Comparison:
PL/SQL Example:
plsqlBEGIN DBMS_OUTPUT.PUT_LINE('Hello from PL/SQL!'); END; /
Java Example:
javaSystem.out.println("Hello from Java!");
7. What is the use of DECLARE
in PL/SQL?
The DECLARE
section is used to define variables, constants, and other PL/SQL elements that can be used within the block.
Code Example:
plsqlDECLARE v_salary NUMBER := 50000; -- Variable declaration c_bonus_rate CONSTANT NUMBER := 0.1; -- Constant declaration BEGIN DBMS_OUTPUT.PUT_LINE('Salary: ' || v_salary || ', Bonus Rate: ' || c_bonus_rate); END; /
8. How does PL/SQL handle NULL values?
- NULL represents an unknown or undefined value in PL/SQL.
- Operations involving
NULL
usually returnNULL
.
Key Points:
Comparisons with NULL:
NULL
cannot be compared using=
or!=
.- Use
IS NULL
orIS NOT NULL
to check for NULL values.
Arithmetic Operations:
- Any arithmetic operation with
NULL
results inNULL
.
- Any arithmetic operation with
Code Example:
plsqlDECLARE v_value NUMBER := NULL; BEGIN IF v_value IS NULL THEN DBMS_OUTPUT.PUT_LINE('Value is NULL.'); ELSE DBMS_OUTPUT.PUT_LINE('Value is NOT NULL.'); END IF; -- Arithmetic operation with NULL DBMS_OUTPUT.PUT_LINE('Result: ' || (v_value + 10)); -- Result will be NULL END; /
Summary
- PL/SQL enhances SQL by adding procedural capabilities.
- A PL/SQL block consists of DECLARE, BEGIN, and EXCEPTION sections.
- Advantages include integration with SQL, error handling, and modularity.
- Anonymous blocks are one-time use, while named blocks are reusable.
- The
DECLARE
section is for defining variables, constants, and cursors. - NULL values require special handling with
IS NULL
checks.