0% found this document useful (0 votes)
129 views

plsql11

The document discusses the basic structure of SQL and PL/SQL. It covers topics like SQL vs PL/SQL, advantages of PL/SQL, PL/SQL block structure, language elements, and control statements. PL/SQL combines features of SQL for data manipulation with features of procedural languages.

Uploaded by

damannaughty1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

plsql11

The document discusses the basic structure of SQL and PL/SQL. It covers topics like SQL vs PL/SQL, advantages of PL/SQL, PL/SQL block structure, language elements, and control statements. PL/SQL combines features of SQL for data manipulation with features of procedural languages.

Uploaded by

damannaughty1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

1.Basic structure of sql 2.Introduction to pl/sql 3.Sql vs pl/sql 4.Advantages of pl/sql 5.Architecture of pl/sql 6.PL/SQL language elements 7.

PL/SQL control statements

www.starthack.com

BASIC STRUCTURE OF SQL


1. SQL is the language used to manipulate relational database. Sql is tied very closely with the relational model 2. It is simple and power full language used to create ,manipulate and retrieve data and structure in the database.. 3. SQL is a non-procedural language i.e. it describe what is required without how result should be computed.. 4. It is industry standard language for relational database management system such as oracle, Microsoft SQL Servers, MS Access

5. ISO and ANSI has published standards for SQL implementation Join effort by ISO and ANSI has lead to standard version of SQL is called SQL/86(SQL1), SQL/92(SQL2)

www.starthack.com

INTRODUCTION TO PL/SQL
1. PL/SQL stands for Procedural Language/Structured Query Language, and it is

an extension of the SQL Language 2. It is superset of the Structured Query Language specialized for use in the oracle database. 3. Because it is procedural language, it eliminates many restriction of the SQL language

4. With the use of SQL user can only perform basic operations such as selecting the information from some prefabricated tables, inserting information into those tables, updating the information stored into tables and also used to delete information from these tables

5. PL/SQL extends SQL by adding control structure found in the other procedural languages 6. PL/SQL is the combination of SQLs languages ease of data manipulation and the procedural languages ease of programming.
www.starthack.com

7. With PL/SQL, we can use SQL statements to manipulate ORACLE data and the flow of control statements to process the data. Moreover, we can declare constants and variables, define subprograms (procedures and functions). Thus, PL/SQL combines the data manipulating power of SQL with the data processing power of procedural languages

8. While PL/SQL is just like any other programming language, it has syntax and rules that determine how programming statements work together. It is important for you to realize that PL/SQL is not a stand-alone programming language

9. PL/SQL is a part of the Oracle RDBMS, and it can reside in two environments, the client and the server 10. As a result, it is very easy to move PL/SQL modules between server-side and client-side applications 11. When the PL/SQL engine is located on the server, the whole PL/SQL block is passed to the PL/SQL engine on the Oracle server.
www.starthack.com

When the PL/SQL engine is located on the client, as it is in the Oracle Developer Tools, the PL/SQL processing is done on the client side. All SQL statements that are embedded within the PL/SQL block are sent to the Oracle server for further processing. When PL/SQL block contains no SQL statement, the entire block is executed on the client side.

SQL VS PL/SQL
SQL
SQL does not have any procedural capabilities SQL statements are passed to oracle engine(server) one at a time..Therefore each time for each statement a call is made to the server resources and these resources are opened and closed every time Due to this, Generating network traffic resulting in slow processing.

PL/SQL
ORACLE has provides all procedural capabilities in PL/SQL to support data filtration In PL/SQL it sends the bundle of SQL statements to the oracle server in the form of BLOCK and hence calling the server resources only once for that block even if that block is having more than one SQL statements After processing all the statements in a block ORACLE server closer the resources results in faster execution of SQL statements in a PL/SQL block. In PL/SQL, we can program the block of statements to handle the errors in such a way that if any of the statement fails to execute then we can display user friendly appropriate message.

In SQL, there is no provision for handling errors and exception, which means that if any SQL statements fails to execute, then oracle gives its own error message and error code many not be user friendly

SQL does not support PL/SQL statements We cannot store the intermediate results of a query in variable

PL/SQL supports SQL statements in its block PL/SQL supports declaring the variables so as to store intermediate results for later use

ADVANTAGES OF PL/SQL
Supports the declaration and manipulation of object types and collection Allowing the calling of external function and procedure Contains new libraries of built-in packages.A package is a file that group functions, cursors, stored procedures and variables in one place

TRIGGERS: Trigger is a PL/SQL program that is stored in the database and executed immediately before or after the INSERT, UPDATE, and DELETE command CURSORS: Oracle uses workspaces to executes the SQL commands. Through PL/SQL it is possible to name the workspace and access its information PL/SQL allow us to use all the SQL data manipulation commands, transection control commands, SQL functions and operators, thus allowing us to manipulates data values in a table more flexibly and effectively

PL/SQL BLOCK STRUCTURE


PL/SQL blocks contain three sections 1. Declare section 2. Executable section 3. Exception-handling section. The executable section is the only mandatory section of the block.

Both the declaration and exception-handling sections are optional.

PL/SQL block has the following structure:


DECLARE - Declaration statements BEGIN - Executable statements

EXCETION - Exception-handling statements


END ;

DECLARATION SECTION
The declaration section is the first section of the PL/SQL block.. It contains definitions of PL/SQL identifiers such as variables, constants, cursors and so on..

Example
DECLARE v_first_name VARCHAR2(35) ; v_last_name VARCHAR2(35) ; v_counter NUMBER := 0 ;

EXECUTABLE SECTION
The executable section is the next section of the PL/SQL block. This section contains executable statements that allow you to manipulate the variables that have been declared in the declaration section. BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123 ; DBMS_OUTPUT.PUT_LINE (Student name : || v_first_name || || v_last_name); END;

EXCEPTION-HANDLING SECTION
The exception-handling section is the last section of the PL/SQL block. This section contains statements that are executed when a runtime error occurs within a block. Runtime errors occur while the program is running and cannot be detected by the PL/SQL compiler.

EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ( There is no student with student id 123 ); END;

EXAMPLE
Set serveroutput on DECLARE v_first_name VARCHAR2(35); v_last_name VARCHAR2(35); BEGIN SELECT first_name, last_name INTO v_first_name, v_last_name FROM student WHERE student_id = 123; DBMS_OUTPUT.PUT_LINE ('Student name: '||v_first_name||' '||v_last_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('There is no student with student id 123'); END ;

PL/SQL LANGUAGE ELEMENTS


Operators and Indicators

Identifiers
Literals

Comments
Expression and Comparisons

Data Types and Declarations

Operator and Indicators


ARITHMETIC OPERATOR :: Used to perform arithmetic operations Example :- +, -, *, /

EXPRESSION OPERATORS :: Used to create assignment, range and string concatenation expression.
Example :Assignment Operator :: A:=2 Range Operator :: 1..4 Concatenate :: || ( dav||college)

LOGICAL OPERATOR:= COMPARISON OPERATOR:=

Literals It specifies and exact value in a programIt is and explicit


Numeric, character, string and Boolean value not represented by an identifierLiterals are used to initialize constants, variables and other data values. Example: 10, -19, 9.99 (Numeric Literals) a, A, ?, ,, ) etc..are character literals starthack, tajinder, 1234 (String Literals) TRUE & FALSE (Boolean Literals)

Comments Begin with /* and end with an asterisk-slash */


Example: /* Statements to select rate and quantity into variables and caluclate value */

Data Types and Declaration


Some commonly used data types for PL/SQL are : NUMBER(store numeric data) CHAR VARCHAR2(store variable length character data) DATE BOOLEAN

PL/SQL CONTROL STATEMENTS


We can change the logical flow of statements within the PL/SQL block with a number of control structure. Control Structure Can Be :: Conditional Control Iterative Control Sequential Control

Conditional Control :- It allows testing the truth of a condition and executing section of program depending upon the condition that may be true or false..
Iterative Control :- It allows executing a section of program repeatedly as long as a specified condition remain true.. Sequential Control :- It allows ordering the sequence of processing section of program..

CONDITIONAL CONTROL
IF THEN Statement IF THEN ELSE Statement

IF THEN ELSIF Statement

IF THEN Statement :: Syntax IF condition THEN Sequence_of_statements; END IF; Example :IF a>b THEN dbms_output.put_lines( a is greater); END IF;

IF THEN ELSE :: Syntax IF condition THEN Sequence_of_statements1; ELSE; Sequence_of_statements2; END IF;

TRUE

IF Condition

FALSE

THEN action

ELSE action

EXAMPLE

To illustrate of IF-ELSE construct PL/SQL block to find greate of two numbers. User will enter any two numbers and IF statement will check for the greater among the entered number.

SOLUTION :-

DECLARE A NUMBER := &ENTER_A; B NUMBER := &ENTER_B;

BEGIN
IF A > B THEN DBMS_OUTPUT.PUT_LINE(A IS GREATER); ELSE DBMS_OUTPUT.PUT_LINE(B IS GREATER); END IF; END;

IF THEN ELSEIF :: The third form of IF statement uses the keyword ELSIF (not ELSEIF) to introduce additional Condition, as follows
SYNTAX IF condition1 THEN Sequence_of_statements1; ELSIF condition2 THEN Sequence_of_statements2; ELSE Sequence_of_statements3; END IF;

You might also like