Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.

Similar presentations


Presentation on theme: "SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language."— Presentation transcript:

1

2 SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language offers, such as control-of-flow construct ( sequence, selection and iteration construct ), or the facility to declare and use variables Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language. Although SQL is a very powerful tool, it's set of disadvantages prevent it from being a fully structured programming language. For a fully structured programming language, Oracle provides PL/SQL. PL/SQL PL/SQL is Oracle's Procedural Language extension to SQL.

3 Advantages of PL/SQL for the Developer and the Database Administrator : 1. PL/SQL is development tool that not only supports SQL data manipulation but also provides facilities of conditional checking, branching and looping. 2. PL/SQL sends an entire block of statements to the Oracle engine at one time. The communication between the program block and the Oracle engine reduces considerably. This in turn reduces network traffic. 3. PL/SQL also permits dealing with errors as required, and facilitates displaying user-friendly messages, when errors are encountered. 4. PL/SQL allows declaration and use of variables in blocks of code. These variables can be used to store intermediate results of a query for later processing, or calculate values and insert them into an Oracle table later. 5. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without the use of the Oracle engine.

4 Benefits of PL/SQL  Integration Application Oracle Server Sharedlibrary

5 Benefits of PL/SQL Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL

6 Benefits of PL/SQL Improved maintenance Improved maintenance Improved data security and integrity Improved data security and integrity Improved performance Improved performance

7 Anonymous block Application trigger Stored procedure/ function Database trigger Application procedure/ function PackagePackage DECLARE BEGIN EXCEPTION END; PL/SQL Program Constructs

8 PL/SQL Anonymous Block Structure  DECLARE (optional) Declare PL/SQL objects to be used within this block  BEGIN (mandatory) Define the executable statements  EXCEPTION (optional) Define the actions that take place if an error arises  END; (mandatory)

9 Subprogram block structure  Header  IS|AS Declaration section  BEGIN Executable section  EXCEPTION (optional) Exception section  END;

10 PL/SQL The character set: The basic character set includes the following:  uppercase alphabets { A - Z }.  lowercase alphabets {a-z }  numerals { 0 - 9 }`  symbols: () + - */ <> = !; :.‘ @ %,“ # $ ^& _ \ { } ? [ ] Words used in a PL/SQL block are called Lexical Units. Blank spaces can be freely insert between lexical units in a PL/SQL block. The spaces have no effect on the PL/SQL block. PL/SQL Data Types Both PL/ SQL and Oracle have their foundations in SQL. Most PL/SQL data types are native to Oracle's data dictionary. hence, there is a very easy integration of PL/SQL code with the Oracle Engine. The default data types that can be declared in PL%SQL are number (for storing numeric data}, char (for storing character data}, date (for storing date and time data}, boolean (for storing TRUE, FALSE or NULL), number, char and date data types can have NULL values. The %TYPE attribute provides for further integration. PL/SQL can use the %TYPE attribute to declare variables based on definitions of columns in a table.

11 Use of Variables  Use variables for: Temporary storage of data Temporary storage of data Manipulation of stored values Manipulation of stored values Reusability Reusability Ease of maintenance Ease of maintenance

12 Handling Variables in PL/SQL Declare and initialize variables in the declaration section. Declare and initialize variables in the declaration section. Assign new values to variables in the executable section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. Pass values into PL/SQL blocks through parameters. View results through output variables. View results through output variables.

13 Types of Variables PL/SQL variables: PL/SQL variables: ScalarScalar CompositeComposite ReferenceReference LOB (large objects)LOB (large objects) Non-PL/SQL variables: Bind and host variables Non-PL/SQL variables: Bind and host variables

14 Variables: ` Variables in PL/SQL blocks are named variables. A variable name must begin with a character and can be followed by a maximum of 29 other characters. Reserved words cannot be used as variable names unless enclosed within double quotes. Variables must be separated from each other by at least one space or by a punctuation mark. Constants: Declaring a constant is similar to declaring a variable except that you have to add the keyword 'constant' and immediately assign a value to it. Thereafter, no further assignments to the constant are possible, while the constant is within the scope of the PL/SQL block.

15 Naming Rules Two variables can have the same name, provided they are in different blocks. Two variables can have the same name, provided they are in different blocks. The variable name (identifier) should not be the same as the name of table columns used in the block. The variable name (identifier) should not be the same as the name of table columns used in the block. DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END; DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END;

16 Assigning Values to Variables v_ename := 'Maduro'; v_hiredate := '31-DEC-98'; SyntaxExamples Set a predefined hiredate for new employees. Set the employee name to “Maduro.”   identifier := expr;

17 Variable Initialization and Keywords  Using: Assignment operator (:=) Assignment operator (:=) DEFAULT keyword DEFAULT keyword NOT NULL constraint NOT NULL constraint

18 Scalar Datatypes Hold a single value Have no internal components 25-OCT-99 25-OCT-99 Atlanta TRUE 256120.08 256120.08

19 Base Scalar Datatypes VARCHAR2 (maximum_length) VARCHAR2 (maximum_length) NUMBER [(precision, scale)] NUMBER [(precision, scale)] DATE DATE CHAR [(maximum_length)] CHAR [(maximum_length)] LONG LONG LONG RAW LONG RAW BOOLEAN BOOLEAN BINARY_INTEGER BINARY_INTEGER PLS_INTEGER PLS_INTEGER

20 Scalar Variable Declarations v_jobVARCHAR2(9); v_countBINARY_INTEGER := 0; v_total_salNUMBER(9,2) := 0; v_orderdateDATE := SYSDATE + 7; c_tax_rateCONSTANT NUMBER(3,2) := 8.25; v_validBOOLEAN NOT NULL := TRUE;  Examples

21 The %TYPE Attribute Declare a variable according to: Declare a variable according to: A database column definitionA database column definition Another previously declared variableAnother previously declared variable Prefix %TYPE with: Prefix %TYPE with: The database table and columnThe database table and column The previously declared variable nameThe previously declared variable name

22 Declaring Variables with the %TYPE Attribute  Examples v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10; v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;

23 Declaring Boolean Variables Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. The variables are connected by the logical operators AND, OR, and NOT. The variables are connected by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value. Arithmetic, character, and date expressions can be used to return a Boolean value.

24 Composite Datatypes PL/SQL TABLES PL/SQL TABLES PL/SQL RECORDS PL/SQL RECORDS

25 LOB Datatype Variables Recipe(CLOB) Photo(BLOB) Movie(BFILE) NCLOB

26 Bind Variables Server O/S Bind Variable

27 Referencing Non-PL/SQL Variables  Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). Prefix the references with a colon (:).  Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). Prefix the references with a colon (:). :g_monthly_sal := v_sal / 12;

28 Displaying user Messages on the Screen: Programming tools require a method through which message can be displayed to the user on the VDU screen. DBMS_OUTPUT is a package that includes a number of procedure and functions that accumulate information in a buffer so that it can be retrieved later. These functions can also be used to display messages to the user. PUT_LINE: Put a piece of information in the package buffer followed by an end-of-line marker, It can also be used to display message to the user. Put line expects a single parameter of character data type. if used to display a message, it is the message ‘string’. To display messages to the user the SERVEROUTPUT should be set to ON. SERVEROUTPUT is a SQL*PLUS environment parameter that displays the information passed as a parameter to the PUT_LINE function. Syntax SET SERVEROUTPUT (ON/OFF]

29 Comments : A comment can have two forms'  The comment line begins with a double hyphen (--). The entire line will be treated as a comment.  The comment line begins with a slash followed by an asterisk (/*) till the occurrence of an asterisk followed by a slash (*/). All lines within are treated as comments. This form of specifying comments can be used to span across multiple lines. This technique can also be used to enclose a section of a PL/SQL block that temporarily needs to be isolated and ignored.

30 Summary PL/SQL blocks are composed of the following sections: PL/SQL blocks are composed of the following sections: Declarative (optional)Declarative (optional) Executable (required)Executable (required) Exception handling (optional)Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. A PL/SQL block can be an anonymous block, procedure, or function. DECLARE BEGIN EXCEPTION END;


Download ppt "SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language."

Similar presentations


Ads by Google