Oracle Database SQL Certified Associate
1 Introduction to SQL
1-1 Overview of SQL
1-2 History of SQL
1-3 SQL Standards
2 SQL Data Types
2-1 Numeric Data Types
2-2 Character Data Types
2-3 Date and Time Data Types
2-4 Large Object (LOB) Data Types
2-5 Miscellaneous Data Types
3 Creating and Managing Tables
3-1 Creating Tables
3-2 Modifying Tables
3-3 Dropping Tables
3-4 Table Constraints
3-5 Temporary Tables
4 Data Manipulation Language (DML)
4-1 Inserting Data
4-2 Updating Data
4-3 Deleting Data
4-4 Selecting Data
4-5 Using Subqueries
5 Data Control Language (DCL)
5-1 Granting Privileges
5-2 Revoking Privileges
6 Data Definition Language (DDL)
6-1 Creating Tables
6-2 Altering Tables
6-3 Dropping Tables
6-4 Creating Indexes
6-5 Dropping Indexes
6-6 Creating Views
6-7 Dropping Views
7 SQL Functions
7-1 Single-Row Functions
7-2 Aggregate Functions
7-3 Group Functions
7-4 Analytical Functions
8 Joins and Subqueries
8-1 Inner Joins
8-2 Outer Joins
8-3 Self-Joins
8-4 Cross Joins
8-5 Subqueries
9 Set Operators
9-1 UNION
9-2 UNION ALL
9-3 INTERSECT
9-4 MINUS
10 Grouping and Aggregation
10-1 GROUP BY Clause
10-2 HAVING Clause
10-3 ROLLUP and CUBE
10-4 GROUPING SETS
11 Transactions and Concurrency
11-1 Transaction Control Statements
11-2 Locking and Concurrency
11-3 Isolation Levels
12 Oracle SQL Developer
12-1 Overview of Oracle SQL Developer
12-2 Using SQL Worksheet
12-3 Managing Connections
12-4 Running Scripts
13 Advanced SQL Topics
13-1 Recursive Queries
13-2 Model Clause
13-3 PIVOT and UNPIVOT
13-4 Flashback Query
14 Performance Tuning
14-1 Query Optimization
14-2 Indexing Strategies
14-3 Analyzing Query Performance
15 Security and Auditing
15-1 User Management
15-2 Role Management
15-3 Auditing SQL Statements
16 Backup and Recovery
16-1 Backup Strategies
16-2 Recovery Strategies
16-3 Using RMAN
17 Oracle Database Architecture
17-1 Overview of Oracle Database Architecture
17-2 Memory Structures
17-3 Process Structures
17-4 Storage Structures
18 PLSQL Basics
18-1 Introduction to PLSQL
18-2 PLSQL Block Structure
18-3 Variables and Data Types
18-4 Control Structures
18-5 Exception Handling
19 Oracle SQL Certification Exam Preparation
19-1 Exam Objectives
19-2 Sample Questions
19-3 Practice Tests
19-4 Exam Tips
PL/SQL Block Structure

PL/SQL Block Structure

Key Concepts

1. PL/SQL Block Structure Overview

A PL/SQL block is a collection of procedural and SQL statements that are grouped together to perform a specific task. It consists of three main sections: the declaration section, the executable section, and the exception handling section.

Example:

A basic PL/SQL block structure:

DECLARE -- Declaration section BEGIN -- Executable section EXCEPTION -- Exception handling section END;

2. Declaration Section

The declaration section is optional and is used to declare variables, constants, cursors, and other elements that will be used in the executable section. Variables declared here are local to the block and cannot be accessed outside of it.

Example:

Declaring a variable in the declaration section:

DECLARE v_name VARCHAR2(50); BEGIN -- Executable section END;

3. Executable Section

The executable section is mandatory and contains the SQL and procedural statements that perform the main logic of the block. This section is where the actual work is done.

Example:

Executing a SQL query in the executable section:

BEGIN SELECT name INTO v_name FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE(v_name); END;

4. Exception Handling Section

The exception handling section is optional and is used to handle any errors or exceptions that occur during the execution of the block. It allows for graceful error handling and recovery.

Example:

Handling an exception in the exception handling section:

BEGIN SELECT name INTO v_name FROM employees WHERE employee_id = 101; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;

5. Anonymous Blocks

Anonymous blocks are PL/SQL blocks that do not have a name and are executed directly. They are typically used for one-time execution of code.

Example:

An anonymous block:

BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END;

6. Named Blocks

Named blocks, such as procedures and functions, have a name and can be called multiple times. They are stored in the database and can be reused.

Example:

A named block (procedure):

CREATE OR REPLACE PROCEDURE greet IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END;

7. Nested Blocks

Nested blocks are PL/SQL blocks that are contained within another block. They can be used to encapsulate logic and improve code readability.

Example:

A nested block:

BEGIN DECLARE v_name VARCHAR2(50); BEGIN SELECT name INTO v_name FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE(v_name); END; END;

8. Variable Scope

The scope of a variable determines where it can be accessed within the block. Variables declared in the declaration section of an outer block are accessible in nested blocks, but not vice versa.

Example:

Variable scope in nested blocks:

DECLARE v_outer VARCHAR2(50) := 'Outer'; BEGIN DECLARE v_inner VARCHAR2(50) := 'Inner'; BEGIN DBMS_OUTPUT.PUT_LINE(v_outer || ' ' || v_inner); END; END;

9. Block Execution Flow

The execution flow of a PL/SQL block follows a sequential order. The declaration section is processed first, followed by the executable section, and finally the exception handling section if an error occurs.

Example:

Execution flow in a PL/SQL block:

DECLARE v_count NUMBER := 0; BEGIN v_count := v_count + 1; DBMS_OUTPUT.PUT_LINE('Count: ' || v_count); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); END;

10. Block Termination

A PL/SQL block terminates when the executable section completes successfully or when an unhandled exception occurs. The exception handling section can be used to handle exceptions and continue execution.

Example:

Block termination with exception handling:

BEGIN SELECT name INTO v_name FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE(v_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('Employee not found'); END;

11. Block Reusability

Named blocks, such as procedures and functions, are reusable and can be called from other PL/SQL blocks or SQL statements. This promotes code reuse and modularity.

Example:

Reusing a named block:

CREATE OR REPLACE PROCEDURE greet IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END; BEGIN greet; END;

12. Block Debugging

Debugging PL/SQL blocks involves using tools and techniques to identify and fix errors. This can include setting breakpoints, inspecting variables, and tracing execution flow.

Example:

Debugging a PL/SQL block:

DECLARE v_count NUMBER := 0; BEGIN v_count := v_count + 1; DBMS_OUTPUT.PUT_LINE('Count: ' || v_count); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('An error occurred'); END;

13. Block Performance

Optimizing the performance of PL/SQL blocks involves minimizing resource usage and improving execution speed. This can include reducing the number of SQL statements, using bulk operations, and optimizing loops.

Example:

Optimizing a PL/SQL block:

BEGIN FOR i IN 1..1000 LOOP INSERT INTO temp_table (id) VALUES (i); END LOOP; END;

14. Block Security

Securing PL/SQL blocks involves controlling access to the blocks and ensuring that sensitive data is protected. This can include using roles, privileges, and encryption.

Example:

Securing a PL/SQL block:

CREATE OR REPLACE PROCEDURE secure_procedure IS BEGIN IF USER = 'ADMIN' THEN DBMS_OUTPUT.PUT_LINE('Access granted'); ELSE DBMS_OUTPUT.PUT_LINE('Access denied'); END IF; END;

15. Block Documentation

Documenting PL/SQL blocks involves providing comments and descriptions that explain the purpose, inputs, outputs, and logic of the block. This helps in maintaining and understanding the code.

Example:

Documenting a PL/SQL block:

-- This procedure greets the user CREATE OR REPLACE PROCEDURE greet IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END;

16. Block Testing

Testing PL/SQL blocks involves verifying that the blocks perform as expected under various conditions. This can include unit testing, integration testing, and performance testing.

Example:

Testing a PL/SQL block:

BEGIN FOR i IN 1..10 LOOP INSERT INTO temp_table (id) VALUES (i); END LOOP; COMMIT; END;

17. Block Versioning

Versioning PL/SQL blocks involves managing different versions of the blocks to track changes and ensure compatibility. This can include using version control systems and maintaining change logs.

Example:

Versioning a PL/SQL block:

-- Version 1.0 CREATE OR REPLACE PROCEDURE greet IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END;

18. Block Deployment

Deploying PL/SQL blocks involves making the blocks available in the production environment. This can include compiling the blocks, granting necessary privileges, and ensuring compatibility with other components.

Example:

Deploying a PL/SQL block:

CREATE OR REPLACE PROCEDURE greet IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello, World!'); END; GRANT EXECUTE ON greet TO PUBLIC;