Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Introduction To PL SQL

Uploaded by

chaukevushaka
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Introduction To PL SQL

Uploaded by

chaukevushaka
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

AIBYP2A

BUSINESS ANALYSIS 2.2 (PL/SQL)

www.vut.ac.za
Vivian Mapande
1
www.vut.ac.za

CONTENTS
1. Understand the need and benefits of PL/SQL

2. Locate Oracle resources

3. Recognize valid and invalid identifiers

4. Declare and initialize variables

5. List and describe various data types

The contents of this presentation is confidential. ©VUT


Introduction to PL/SQL

The need and benefits of PL/SQL

• Oracle PL/SQL is an extension of SQL language.


• It combines the data manipulation power of SQL with the processing power of
procedural language to create super powerful SQL queries.
• PL/SQL means instructing the compiler ‘what to do’ through SQL and ‘how to do’
through its procedural way.
• PL/SQL stands for “Procedural Language extensions to the Structured Query
Language.

3
Introduction to PL/SQL

Difference between SQL and PL/SQL


SQL PL/SQL

SQL is a single query that is used to perform DML and DDL PL/SQL is a block of codes that used to write the entire program
operations. blocks/ procedure/ function, etc.

It is declarative, that defines what need to be done, rather than PL/SQL is procedural that defines how the things needs to be
how things need to be done. done.

Execute as a single statement. Execute as a whole block.


Mainly used to manipulate data. Mainly used to create an application.
Interaction with a Database server. No interaction with the database server.

Cannot contain PL/SQL code in it. It is an extension of SQL, so that it can contain SQL inside it.

4
Introduction to PL/SQL

Benefit of PL/SQL
• PL/SQL allows sending an entire block of statements to the database at one time.
• This reduces network traffic and provides high performance for the applications.
• PL/SQL gives high productivity to programmers as it can query, transform, and update data
in a database.
• PL/SQL saves time on design and debugging by strong features, such as exception handling,
encapsulation, data hiding, and object-oriented data types.
• Applications written in PL/SQL are fully portable.
• PL/SQL provides high security level.
• PL/SQL provides access to predefined SQL packages.
• PL/SQL provides support for Object-Oriented Programming.
• PL/SQL provides support for developing Web Applications and Server Pages.

5
Introduction to PL/SQL

Locate Oracle resources


Prescribed Book: Joan Casteel, Oracle 12c: SQL, 3rd Edition
By Joan Casteel

Recommended Readings
• https://www.w3resource.com/sql-exercises/
• https://sqliteonline.com/
• https://livesql.oracle.com/apex/f?p=590:1000::::::
• https://www.mycompiler.io/online-sql-editor
• https://www.hackerrank.com/domains/sql

6
Introduction to PL/SQL

PL/SQL Block

PL/SQL is a block-structured language, meaning that PL/SQL programs are divided and
written in logical blocks of code. Each block consists of three sub-parts:
Sections Description

Declarations This section starts with the keyword DECLARE. It is an optional section and defines all
variables, cursors, subprograms, and other elements to be used in the program.

Executable Commands This section is enclosed between the keywords BEGIN and END and it is a mandatory
section. It consists of the executable PL/SQL statements of the program. It should have at
least one executable line of code, which may be just a NULL command to indicate that
nothing should be executed.

Exception Handling This section section starts with the keyword EXCEPTION. This section is again optional and
contains exception(s) that handle errors in the program.

7
Introduction to PL/SQL

PL/SQL Block (Cont.)

Every PL/SQL statement end with a semicolon (;). PL/SQL blocks can be nested within other
PL/SQL blocks using BEGIN and END. Here is the basic structure of a PL/SQL block:

8
Introduction to PL/SQL

PL/SQL Block (Cont.)

The 'Hello World' Example:

9
Introduction to PL/SQL

PL/SQL Block (Cont.)

The end; line signals the end of the PL/SQL block. To run the code from SQL command line, you
may need to type / at the beginning of the first blank line after the last line of the code. When
the above code is executed at SQL prompt, it produces following result:

1
0
Introduction to PL/SQL

Recognize valid and invalid identifiers

The PL/SQL Identifiers


PL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved
words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar
signs, underscores, and number signs and should not exceed 30 characters.

Recognize valid and invalid identifier


In PL/SQL, there are certain rules and guidelines that determine the validity of an identifier.
Here are the criteria to recognize valid and invalid identifiers in PL/SQL:

1
1
Introduction to PL/SQL

Recognize valid and invalid identifiers


Valid Identifiers:
• The identifier must start with a letter.
• The identifier can be followed by a sequence of letters, digits, or underscores.
• The identifier must not exceed 30 characters in length.
• The identifier is case-insensitive, i.e., uppercase and lowercase letters are considered the
same.
Examples of valid identifiers:
employee_name
department_id
numberOfEmployees

1
2
Introduction to PL/SQL

Recognize valid and invalid identifiers (Cont.)


Invalid Identifiers:
• The identifier must not start with a digit or special character (except underscore).
• The identifier must not contain spaces or any special characters other than an underscore.
• The identifier must not exceed 30 characters in length.
• The identifier should not be a reserved PL/SQL keyword or reserved word.
Examples of invalid identifiers:
123employee
department-id
employee name
SELECT

1
3
Introduction to PL/SQL

The PL/SQL Delimiters


A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL:

1
4
Introduction to PL/SQL

The PL/SQL Delimiters (Cont.)


A delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL:

1
5
Introduction to PL/SQL

Data types
Data type Description
1. NUMBER This data type is used to store numeric values. It can handle both integer and floating-point numbers.

2. VARCHAR2 This data type is used to store character strings of varying lengths. The maximum length allowed is 32767 bytes.

3. CHAR This data type is used to store fixed-length character strings. It is similar to VARCHAR2, but it pads the string with
spaces to the fixed length if the actual string is shorter.
4. DATE: This data type is used to store date and time values. It can store dates from January 1, 4712 BC, to December 31,
9999 AD with a precision of up to one second.
This data type is used to store logical values, such as TRUE or FALSE.
5. BOOLEAN
6. BLOB This data type is used to store binary large objects, such as images, audio files, or video files.

7. CLOB This data type is used to store character large objects, such as large text documents.

8. RAW This data type is used to store raw binary data. It can store up to 2000 bytes.
9. LONG This data type is used to store variable-length character strings of up to 2 GB in size.
10. INTERVAL This data type is used to store intervals of time. It can store the difference between two dates or timestamps.

11. ROWID This data type is used to store the unique address of a row in a database table.
12. UROWID This data type is used to store the unique address of a row in a database table in a universal format.
13. XMLType This data type is used to store XML data.
14. RECORD This data type is used to define a structure containing multiple fields. It is similar to a struct or record in other
programming languages.
15. TABLE This data type is used to define collections of values of the same data type. It can be used to store multiple rows or 1
columns of data. 6
Introduction to PL/SQL

Declare and initialize variables


In PL/SQL, variables are declared and initialized using the DECLARE statement. The syntax for
declaring and initializing variables is as follows:

DECLARE
variable_name1 data_type := initial_value1;
variable_name2 data_type := initial_value2;
...
BEGIN
-- PL/SQL code
END;

1
7
Introduction to PL/SQL

Declare and initialize variables


Here's an example of declaring and initializing variables in PL/SQL:
DECLARE
employee_name VARCHAR(50) := 'John Doe';
employee_age NUMBER := 30;
employee_salary NUMBER := 5000.00;
BEGIN
-- PL/SQL code
-- Use the declared variables in your code
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || employee_name);
DBMS_OUTPUT.PUT_LINE('Employee Age: ' || employee_age);
DBMS_OUTPUT.PUT_LINE('Employee Salary: ' || employee_salary);
END;
/
In the above example, three variables (employee_name, employee_age, and employee_salary)
are declared and initialized with their respective data types and initial values.

1
8
Introduction to PL/SQL

Declare and initialize variables


Note: The DBMS_OUTPUT.PUT_LINE() statement is used to display the variable values in the
output console. To see the output, you may need to enable the output display in your PL/SQL IDE
or use the SET SERVEROUTPUT ON command before executing the code.

To use the SET SERVEROUTPUT ON command before executing the code in PL/SQL, you need to
follow these steps:

Open your PL/SQL environment (e.g., SQL*Plus, SQL Developer, Toad).

Before running your code, enter the following command:


SET SERVEROUTPUT ON
This command enables the output display for the DBMS_OUTPUT.PUT_LINE() statements in your
PL/SQL code.

1
9
Introduction to PL/SQL

Declare and initialize variables


Run or execute your PL/SQL code SET SERVEROUTPUT ON
that contains the DECLARE
DBMS_OUTPUT.PUT_LINE()
employee_name VARCHAR(50) := 'John Doe';
statements.
employee_age NUMBER := 30;
After executing the code, you
should now be able to see the employee_salary NUMBER := 5000.00;
output of the BEGIN
DBMS_OUTPUT.PUT_LINE()
statements in your PL/SQL -- PL/SQL code
environment. DBMS_OUTPUT.PUT_LINE('Employee Name: ' ||
An example of how to use the employee_name);
SET SERVEROUTPUT ON DBMS_OUTPUT.PUT_LINE('Employee Age: ' || employee_age);
command before executing the
code in PL/SQL: DBMS_OUTPUT.PUT_LINE('Employee Salary: ' ||
employee_salary);
SET SERVEROUTPUT ON
END;
/

2
0
Introduction to PL/SQL

Declare and initialize variables


The SET SERVEROUTPUT ON command is placed at the beginning of the code. It enables the output
display for the DBMS_OUTPUT.PUT_LINE() statements.

After executing the code, you will see the output in your PL/SQL environment:
Employee Name: John Doe
Employee Age: 30
Employee Salary: 5000

Note that the output may vary depending on the PL/SQL environment you are using (e.g.,
SQL*Plus, SQL Developer, Toad), but the essential part is enabling the SET SERVEROUTPUT ON to
display the output.

2
1
Introduction to PL/SQL

Next chapter:
Basic PL/SQL block structures
• Identify lexical units in a PL/SQL block
• Use built-in SQL functions in PL/SQL
• Describe when implicit conversions take place and when explicit conversions have to be dealt with
• Perform calculations with variables
• Use decision structures: IF-THEN and CASE
• Use loop structures: FOR and WHILE

2
2

You might also like