Learn PL/SQL with Ivan Bayross - The Best Book on Oracle's Programming Language
PL/SQL: The Programming Language of Oracle by Ivan Bayross
Are you interested in learning PL/SQL, the powerful extension of SQL for Oracle databases? Do you want to master the concepts and techniques of this versatile programming language? If yes, then you should read this article till the end. In this article, I will introduce you to PL/SQL, its features and applications, and how you can get the best book on this topic by Ivan Bayross.
pl sql programming language oracle ivan bayross pdf free
Introduction
What is PL/SQL?
PL/SQL stands for Procedural Language/Structured Query Language. It is a programming language that combines the features of SQL (Structured Query Language) with procedural elements. SQL is the standard language for querying and manipulating data in relational databases, such as Oracle. However, SQL has some limitations when it comes to complex logic, error handling, and performance optimization. That's where PL/SQL comes in handy.
PL/SQL allows you to write blocks of code that can be executed by the Oracle database server. You can use PL/SQL to create stored procedures, functions, triggers, packages, and other database objects that can perform various tasks, such as data validation, business logic, security checks, and more. PL/SQL also supports object-oriented features, such as inheritance, polymorphism, and encapsulation.
Why learn PL/SQL?
Learning PL/SQL can benefit you in many ways. Here are some of the reasons why you should learn PL/SQL:
PL/SQL is widely used in the industry for developing applications that interact with Oracle databases. If you want to work as a database developer, administrator, or analyst, you need to have a good knowledge of PL/SQL.
PL/SQL can help you improve the performance and efficiency of your SQL queries. By using PL/SQL, you can reduce the network traffic between the application and the database server, as well as take advantage of the caching and optimization features of Oracle.
PL/SQL can help you enhance the functionality and security of your database applications. By using PL/SQL, you can implement complex business logic, error handling, auditing, logging, and encryption in your database objects.
PL/SQL can help you learn other programming languages that are similar to it, such as Java, C#, or PHP. PL/SQL has a syntax that is based on Ada and Pascal, and it also supports many common programming concepts, such as variables, data types, control structures, loops, arrays, etc.
How to get the book by Ivan Bayross?
If you are looking for a comprehensive and practical guide on PL/SQL, you should check out the book PL/SQL: The Programming Language of Oracle by Ivan Bayross. This book covers all the topics that you need to know to master PL/SQL, from the basics to the advanced features. The book also includes many examples, exercises, and projects that will help you practice and apply your skills.
The book by Ivan Bayross is available in both print and digital formats. You can buy the paperback version from various online stores, such as Amazon, Flipkart, or Snapdeal. You can also download the PDF version for free from the Internet Archive website. The link to the PDF file is given below:
https://archive.org/details/sqlplsqlprogramm0000bayr
Features of PL/SQL
Block structure
The basic unit of PL/SQL code is a block. A block is a group of statements that are executed as a single unit. A block can be nested inside another block, creating a hierarchical structure. A block has three sections: declaration, executable, and exception. The declaration section is optional and it contains the declarations of variables, constants, cursors, and other identifiers. The executable section is mandatory and it contains the statements that perform the actions of the block. The exception section is optional and it contains the handlers for the errors that may occur during the execution of the block.
A PL/SQL block has the following syntax:
BEGIN -- executable statements EXCEPTION -- exception handlers END;
Variables and constants
Variables and constants are identifiers that store values in PL/SQL. Variables can change their values during the execution of the program, while constants have fixed values that cannot be changed. Variables and constants must be declared before they can be used in a PL/SQL block. The declaration consists of the name of the identifier, its data type, and an optional initialization value.
A variable or constant declaration has the following syntax:
identifier_name [CONSTANT] data_type [:= DEFAULT initial_value];
For example:
v_name VARCHAR2(20) := 'John'; c_pi CONSTANT NUMBER := 3.14;
Data types
Data types specify the kind of values that can be stored in variables and constants. PL/SQL supports various data types, such as scalar, composite, reference, and large object (LOB) data types. Scalar data types are single-valued data types, such as numbers, characters, strings, dates, booleans, etc. Composite data types are collection data types, such as records, arrays, nested tables, etc. Reference data types are pointer data types, such as cursors, ref cursors, etc. LOB data types are large object data types, such as binary large objects (BLOBs), character large objects (CLOBs), etc.
Some examples of data types are:
v_age NUMBER(3); v_salary NUMBER(10,2); v_gender CHAR(1); v_email VARCHAR2(50); v_dob DATE; v_flag BOOLEAN; v_emp RECORD (empno NUMBER(4), ename VARCHAR2(10), deptno NUMBER(2)); v_dept ARRAY(10) OF VARCHAR2(20); v_cursor CURSOR IS SELECT * FROM emp; v_image BLOB; v_text CLOB;
Control structures
Control structures are statements that control the flow of execution in a PL/SQL block. PL/SQL supports three types of control structures: conditional, iterative, and sequential. Conditional control structures are statements that execute different blocks of code based on some conditions, such as IF-THEN-ELSE or CASE statements. Iterative control structures are statements that execute a block of code repeatedly until some condition is met, such as LOOP or FOR statements. Sequential control structures are statements that transfer the control to another point in the program, such as GOTO or EXIT statements.
Some examples of control structures are:
IF v_age > 18 THEN DBMS_OUTPUT.PUT_LINE('You are an adult'); ELSE DBMS_OUTPUT.PUT_LINE('You are a minor'); END IF; CASE v_gender WHEN 'M' THEN DBMS_OUTPUT.PUT_LINE('You are male'); WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('You are female'); ELSE DBMS_OUTPUT.PUT_LINE('You are unknown'); END CASE; LOOP DBMS_OUTPUT.PUT_LINE('Hello'); EXIT WHEN v_flag = TRUE; END LOOP; FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; GOTO end_label; DBMS_OUTPUT.PUT_LINE('This line will be skipped'); >
DBMS_OUTPUT.PUT_LINE('This line will be executed');
Cursors and exceptions
Cursors and exceptions
Cursors and exceptions are two important concepts in PL/SQL that deal with query processing and error handling. A cursor is a pointer that identifies a specific row in a result set returned by a SQL query. You can use cursors to fetch and manipulate data from the database in a row-by-row manner. There are two types of cursors in PL/SQL: implicit and explicit. Implicit cursors are automatically created and managed by PL/SQL for SQL statements that return at most one row, such as SELECT INTO or DML statements. Explicit cursors are user-defined cursors that can handle SQL statements that return multiple rows, such as SELECT statements. You can declare, open, fetch, and close explicit cursors using PL/SQL statements.
An exception is an error condition that occurs during the execution of a PL/SQL block. Exceptions can be raised by the Oracle database server or by the PL/SQL program. When an exception is raised, the normal execution of the block is interrupted and the control is transferred to the exception section of the block, where you can handle the error using exception handlers. There are two types of exceptions in PL/SQL: predefined and user-defined. Predefined exceptions are built-in exceptions that are defined by Oracle, such as NO_DATA_FOUND, TOO_MANY_ROWS, ZERO_DIVIDE, etc. User-defined exceptions are custom exceptions that are defined by the programmer using the EXCEPTION keyword.
Applications of PL/SQL
Stored procedures and functions
Stored procedures and functions are subprograms that are stored in the database and can be invoked from other programs. A stored procedure is a subprogram that performs a specific task and may or may not return a value. A function is a subprogram that always returns a single value. You can use stored procedures and functions to modularize your code, reuse your code, improve performance, and enhance security.
To create a stored procedure or function, you use the CREATE PROCEDURE or CREATE FUNCTION statement. To invoke a stored procedure or function, you use the EXECUTE or CALL statement, or simply use the name of the subprogram in an expression.
For example:
CREATE PROCEDURE add_emp (p_empno NUMBER, p_ename VARCHAR2, p_deptno NUMBER) AS BEGIN INSERT INTO emp VALUES (p_empno, p_ename, NULL, NULL, NULL, NULL, p_deptno); DBMS_OUTPUT.PUT_LINE('Employee added successfully'); END; / CREATE FUNCTION get_sal (p_empno NUMBER) RETURN NUMBER AS v_sal NUMBER; BEGIN SELECT sal INTO v_sal FROM emp WHERE empno = p_empno; RETURN v_sal; END; / EXECUTE add_emp(1234, 'Mark', 10); SELECT ename, get_sal(empno) FROM emp;
Triggers and packages
Triggers and packages are two types of database objects that can be created using PL/SQL. A trigger is a subprogram that is automatically executed when a specific event occurs on a table or view, such as INSERT, UPDATE, DELETE, or DDL statements. You can use triggers to enforce business rules, maintain data integrity, audit data changes, and perform complex calculations.
To create a trigger, you use the CREATE TRIGGER statement. To disable or enable a trigger, you use the ALTER TRIGGER statement.
For example:
CREATE TRIGGER audit_emp AFTER INSERT OR UPDATE OR DELETE ON emp FOR EACH ROW BEGIN INSERT INTO emp_audit VALUES (:OLD.empno, :OLD.ename, :OLD.deptno, :NEW.empno, :NEW.ename, :NEW.deptno, USER, SYSDATE); END; / ALTER TRIGGER audit_emp DISABLE;
A package is a collection of related subprograms and variables that are grouped together under a single name. You can use packages to organize your code, avoid name conflicts, increase performance, and share data among subprograms.
To create a package, you use the CREATE PACKAGE statement. A package has two parts: specification and body. The specification declares the public elements of the package that can be accessed from other programs. The body defines the private elements of the package and implements the subprograms declared in the specification.
For example:
CREATE PACKAGE math_pkg AS -- specification PI CONSTANT NUMBER := 3.14; FUNCTION square (n NUMBER) RETURN NUMBER; PROCEDURE swap (x IN OUT NUMBER, y IN OUT NUMBER); END; / CREATE PACKAGE BODY math_pkg AS -- body FUNCTION square (n NUMBER) RETURN NUMBER IS BEGIN RETURN n * n; END; PROCEDURE swap (x IN OUT NUMBER, y IN OUT NUMBER) IS temp NUMBER; BEGIN temp := x; x := y; y := temp; END; END; / SELECT math_pkg.square(5) FROM dual; DECLARE a NUMBER := 10; b NUMBER := 20; BEGIN math_pkg.swap(a, b); DBMS_OUTPUT.PUT_LINE('a = ' a ', b = ' b); END;
Dynamic SQL and collections
Dynamic SQL and collections are two advanced features of PL/SQL that allow you to write more flexible and powerful programs. Dynamic SQL is a technique that enables you to execute SQL statements that are constructed at run time, rather than at compile time. You can use dynamic SQL to execute SQL statements that are unknown or variable, such as user input, complex queries, or DDL statements.
To execute dynamic SQL, you can use the EXECUTE IMMEDIATE statement or the DBMS_SQL package. The EXECUTE IMMEDIATE statement is a simple and convenient way to execute a single SQL statement that does not return any rows. The DBMS_SQL package is a complex and powerful way to execute multiple SQL statements that can return rows and bind variables.
For example:
DECLARE v_table_name VARCHAR2(30) := 'emp'; v_sql VARCHAR2(100); BEGIN v_sql := 'DROP TABLE ' v_table_name; EXECUTE IMMEDIATE v_sql; END; / DECLARE v_cursor INTEGER; v_sql VARCHAR2(100); v_empno NUMBER(4); BEGIN v_sql := 'SELECT empno FROM emp WHERE deptno = :dno'; v_cursor := DBMS_SQL.OPEN_CURSOR; DBMS_SQL.PARSE(v_cursor, v_sql, DBMS_SQL.NATIVE); DBMS_SQL.BIND_VARIABLE(v_cursor, ':dno', 10); DBMS_SQL.DEFINE_COLUMN(v_cursor, 1, v_empno); IF DBMS_SQL.EXECUTE(v_cursor) > 0 THEN LOOP IF DBMS_SQL.FETCH_ROWS(v_cursor) > 0 THEN DBMS_SQL.COLUMN_VALUE(v_cursor, 1, v_empno); DBMS_OUTPUT.PUT_LINE('Employee number: ' v_empno); ELSE EXIT; END IF; END LOOP; END IF; DBMS_SQL.CLOSE_CURSOR(v_cursor); END;
Collections are data structures that can store multiple values of the same data type. You can use collections to manipulate data in bulk, rather than one by one. PL/SQL supports three types of collections: associative arrays, nested tables, and varrays. Associative arrays are arrays that are indexed by arbitrary values, such as strings or numbers. Nested tables are tables that can be stored in a database column or a PL/SQL variable. Varrays are arrays that have a fixed size and can be stored in a database column or a PL/SQL variable.
To create a collection, you use the TYPE statement. To initialize and populate a collection, you use the constructor function of the collection type.
For example:
DECLARE TYPE empno_tab IS TABLE OF NUMBER(4); -- nested table type TYPE ename_arr IS VARRAY(10) OF VARCHAR2(10); -- varray type TYPE dept_map IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; -- associative array type v_empno empno_tab := empno_tab(7369,7499,7521); -- initialize with constructor v_ename ename_arr := ename_arr('SMITH','ALLEN','WARD'); -- initialize with constructor v_dept dept_map; -- declare without initialization BEGIN v_dept(10) := 'ACCOUNTING'; -- populate with assignment v_dept(20) := 'RESEARCH'; -- loop through the collections using index or iterator FOR i IN v_empno.FIRST..v_empno.LAST LOOP DBMS_OUTPUT.PUT_LINE('Employee number: ' v_empno(i)); END LOOP; FOR i IN 1..v_ename.COUNT LOOP DBMS_OUTPUT.PUT_LINE('Employee name: ' v_ename(i)); END LOOP; DBMS_OUTPUT.PUT_LINE('Department number: ' i ', name: ' v_dept(i)); END LOOP; END;
Object-oriented features
Object-oriented features are features that enable you to model real-world entities and concepts using objects. Objects are instances of classes that have attributes and methods. Attributes are data members that store the state of the object. Methods are subprograms that define the behavior of the object. You can use object-oriented features to create reusable and modular code, as well as to implement abstraction, encapsulation, inheritance, and polymorphism.
To create a class in PL/SQL, you use the CREATE TYPE statement. A class can have a constructor method that initializes the object, and a destructor method that cleans up the object. A class can also have a map method that defines how the object is compared with other objects, and an order method that defines how the object is sorted with other objects.
For example:
CREATE TYPE person_typ AS OBJECT ( name VARCHAR2(20), age NUMBER(3), gender CHAR(1), CONSTRUCTOR FUNCTION person_typ (name VARCHAR2, age NUMBER, gender CHAR) RETURN SELF AS RESULT, MEMBER PROCEDURE display, MEMBER FUNCTION get_name RETURN VARCHAR2, MEMBER FUNCTION get_age RETURN NUMBER, MEMBER FUNCTION get_gender RETURN CHAR ); / CREATE TYPE BODY person_typ AS CONSTRUCTOR FUNCTION person_typ (name VARCHAR2, age NUMBER, gender CHAR) RETURN SELF AS RESULT IS BEGIN SELF.name := name; SELF.age := age; SELF.gender := gender; RETURN; END; MEMBER PROCEDURE display IS BEGIN DBMS_OUTPUT.PUT_LINE('Name: ' name ', Age: ' age ', Gender: ' gender); END; MEMBER FUNCTION get_name RETURN VARCHAR2 IS BEGIN RETURN name; END; MEMBER FUNCTION get_age RETURN NUMBER IS BEGIN RETURN age; END; MEMBER FUNCTION get_gender RETURN CHAR IS BEGIN RETURN gender; END; END; / DECLARE p1 person_typ := person_typ('Alice',25,'F'); p2 person_typ := person_typ('Bob',30,'M'); BEGIN p1.display; p2.display; END;
Conclusion
Summary of the main points
In this article, I have given you an overview of PL/SQL, the programming language of Oracle. I have explained what PL/SQL is, why you should learn it, and how you can get the book by Ivan Bayross on this topic. I have also discussed some of the features and applications of PL/SQL, such as block structure, variables and constants, data types, control structures, cursors and exceptions, stored procedures and functions, triggers and packages, dynamic SQL and collections, and object-oriented features.
Benefits of reading the book by Ivan Bayross
If you want to learn more about PL/SQL and become an expert in this programming language, I highly recommend you to read the book PL/SQL: The Programming Language of Oracle by Ivan Bayross. This book will teach you everything you need to know about PL/SQL in a clear and concise manner. You will learn the syntax, concepts, techniques, and best practices of PL/SQL through many examples, exercises, and projects. You will also gain a solid foundation in SQL and Oracle database concepts that will help you develop robust and efficient database applications.
FAQs
Here are some frequently asked questions about PL/SQL and the book by Ivan Bayross:
What are the prerequisites for learning PL/SQL?
To learn PL/SQL, you should have some basic knowledge of SQL and relational database concepts. You should also have access to an Oracle database server where you can practice your PL/SQL code.
What are the advantages of PL/SQL over SQL?
PL/SQL has several advantages over SQL, such as:
PL/SQL can handle complex logic and computations that SQL cannot.
PL/SQL can improve the performance and efficiency of SQL queries by reducing network traffic and taking advantage of Oracle's caching and optimization features.
PL/SQL can enhance the functionality and security of SQL queries by implementing error handling, auditing, logging, encryption, and other features.
What are the differences between PL/SQL and other programming languages, such as Java, C#, or PHP?
PL/SQL is a programming language that is designed specifically for Oracle databases. It is tightly integrated with SQL and Oracle's data types, functions, and features. PL/SQL is also a compiled language, which means that the code is checked for errors and converted into an executable form before it is run. Other programming languages, such as Java, C#, or PHP, are general-purpose languages that can be used