Presentation is loading. Please wait.

Presentation is loading. Please wait.

Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL - 2 - PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.

Similar presentations


Presentation on theme: "Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL - 2 - PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database."— Presentation transcript:

1 Text TCS INTERNAL Oracle PL/SQL – Introduction

2 TCS INTERNAL - 2 - PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database oriented programming language that extends Oracle SQL with procedural capabilities. Constructs like procedures, loops, variables, objects etc are supported by PLSQL. Oracle SQL + Procedural Language Features = PL/SQL PLSQL programs are divided into blocks. Each block can contain SQL and PL/SQL statements organized in an order to execute business logic. PLSQL blocks can be stored in memory for reusability. Using PL/SQL, complex problems can be broken down to understandable procedural code and this code can be reused across multiple applications. General syntax is based on that of ADA and Pascal programming language.

3 TCS INTERNAL - 3 - Need for PL SQL Using single SQL call, it is not possible to retrieve a set of records from database and modify each record according to certain rules. A programming language is required to perform the operation. To achieve this capability, Oracle introduced PL/SQL. SQL is a declarative language whereas PLSQL is a procedural language. In procedural language, program specifies a list of operations to be performed in a particular order to achieve the desired results. PL SQL can add selective (like case, if then statements) and iterative (loops) constructs to SQL. PL SQL Cursor enables record wise processing of a SQL query output. The need for PLSQL can be better understood while going through the advantages of PLSQL and the different features of PLSQL like cursors, procedures, functions, packages,exception handling and triggers.

4 TCS INTERNAL - 4 - PL/SQL, How does it differ from SQL? In SQL, each SQL statement is processed separately and results are returned separately In PL/SQL, any number of queries can be bundled in a block and in single network trip the bundle can be processed. SQL Query1 SQL Query2 SQL Query3 Client Server SQL Query1 SQL Query2 SQL Query3 Client Server PL-SQL Block

5 TCS INTERNAL - 5 - PL SQL Architecture The architecture includes PL/SQL engine and SQL statement executor. The PL/SQL engine executes procedural statements and sends SQL statements to the SQL engine in Oracle server. PL/SQL engine can be installed in an Oracle server or in an application development tool such as Oracle forms.

6 TCS INTERNAL - 6 - Main Advantages of PL/SQL Better Performance a) Without PL/SQL, Oracle processes SQL statements one at a time. Programs that issue many SQL statements requires multiple calls to the database, resulting in significant network and performance overhead. Whereas in PL/SQL, an entire block of statements can be sent to Oracle at one time. This reduces the network traffic between the database and application. b) PL/SQL subprograms are compiled once and stored in executable form. Thus, a single call can start a large job and the work can be divided effectively. This reduces the network traffic and improves response times. Stored procedures are cached and shared among users which lowers memory requirements and invocation overhead.

7 TCS INTERNAL - 7 - Main Advantages of PL/SQL Integration with SQL a) PL/SQL has tight integration with SQL.PL/SQL enables the use of SQL data manipulation, cursor control, transaction control commands,SQL functions, operators and pseudo columns. b) PL/SQL supports the SQL datatypes, thereby reducing the need to convert the datatypes between PL/SQL and SQL. This integration saves processing time. Using features like %TYPE and %ROWTYPE, there is no need to explicitly define the datatype and it can directly refer the datatype from Oracle table which saves maintenance work when table definitions change. c) PL/SQL supports both static and dynamic SQL. Static SQL supports DML and transaction control operations (TCL) from PL/SQL block. Dynamic SQL makes the application more flexible and allows embedding DDL statements in PL/SQL blocks and for creating run time queries.

8 TCS INTERNAL - 8 - Main Advantages of PL/SQL Portability Applications written in PL/SQL can run on any operating system and platform where the Oracle database runs. It means programs developed in PL/SQL on Windows Oracle database will load and execute in a Unix Oracle database. With PL/SQL, portable program libraries can be written and reused in different environments. Security Data hiding and user access restrictions helps in enabling security. PL/SQL triggers can control or record data changes, making sure that business rules are obeyed. PL/SQL supports easy and effective exception handling method. Other advantages are that PL/SQL provides the ability to loop through records, manipulating them one at a time whereas SQL does not. PL/SQL supports object oriented programming concepts like data encapsulation, data hiding, etc

9 TCS INTERNAL - 9 - PL/SQL Program Structure The basic unit of PL/SQL program is the block. PL/SQL programs are divided and written in logical blocks of code which can be nested inside one another. A block groups related declarations and statements. Each block consists of three sub parts  Declarations  Executable Commands  Exception Handling TCS INTERNAL

10 PL/SQL Program Sections Declaration Starts with the keyword DECLARE Optional Defines all variables, cursors, subprograms, and other elements to be used in the program. Executable Commands Enclosed between the keywords BEGIN and END Mandatory Consists of the executable PL/SQL statements of the program. 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 Handling Starts with the keyword EXCEPTION. Optional Contains exceptions that handle errors in the program.

11 TCS INTERNAL - 11 - PL/SQL Block Structure DECLARE – Optional –Variables, cursors, user-defined exceptions BEGIN – Mandatory –SQL statements –PL/SQL statements EXCEPTION – Optional –Actions to perform when errors occur END; – Mandatory DECLARE – Optional –Variables, cursors, user-defined exceptions BEGIN – Mandatory –SQL statements –PL/SQL statements EXCEPTION – Optional –Actions to perform when errors occur END; – Mandatory DECLARE BEGIN EXCEPTION END;

12 TCS INTERNAL - 12 - PL/SQL Comments Comments are explanatory statements which are written in the code to help others understand the code better. All characters available inside the comment are ignored by PL/SQL compiler. PL/SQL supports two types of comments  single-line comment (starts with --)  multi line comment (enclosed within /* and */)

13 TCS INTERNAL - 13 - Sample PL/SQL program DECLARE -- variable declaration message varchar2(20):= 'Hello, World!'; BEGIN /* PL/SQL executable statement(s) */ dbms_output.put_line(message); -- print message in the console END; / Output : Hello World PL/SQL procedure successfully completed.

14 TCS INTERNAL - 14 - PL/SQL References https://www.youtube.com/watch?v=_qBCjLKB_sM http://plsql-tutorial.com/ http://www.tutorialspoint.com/plsql/ http://docs.oracle.com/cd/A97630_01/appdev.920/a96624/01_oview.htm www.java2s.com/Tutorial/Oracle/0420__PL-SQL-Data- Types/Catalog0420__PL-SQL-Data-Types.htm http://oracle-base.com/articles/misc/introduction-to-plsql.php


Download ppt "Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL - 2 - PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database."

Similar presentations


Ads by Google