Introduction To PL SQL
Introduction To PL SQL
www.vut.ac.za
Vivian Mapande
1
www.vut.ac.za
CONTENTS
1. Understand the need and benefits of PL/SQL
3
Introduction to 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.
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
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
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
9
Introduction to PL/SQL
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
1
1
Introduction to PL/SQL
1
2
Introduction to PL/SQL
1
3
Introduction to PL/SQL
1
4
Introduction to 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
variable_name1 data_type := initial_value1;
variable_name2 data_type := initial_value2;
...
BEGIN
-- PL/SQL code
END;
1
7
Introduction to PL/SQL
1
8
Introduction to PL/SQL
To use the SET SERVEROUTPUT ON command before executing the code in PL/SQL, you need to
follow these steps:
1
9
Introduction to PL/SQL
2
0
Introduction to PL/SQL
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