SQL Introduction by Ravi
SQL Introduction by Ravi
Ravi Kumar.S
Introduction to PL/SQL
What is PL/SQL
PL/SQL is a sophistical programming language used to access an Oracle database from a various environments. PL/SQL stands for Procedural Language/SQL. It extends SQL by adding constructs found in other procedural languages, such as: loops, conditional statements, declared variables, accessing individual records one at a time, and many others.
Slide 3 of 36
BEGIN
/* Executable section (required).
EXCEPTION
/* Exception handling section (optional). */
END;
/
Slide 6 of 36
Slide 8 of 36
Slide 9 of 36
Every variable has a specific data type associated with it. Variables can be associated with a table structure.
v_salary employee.salary%TYPE;
Slide 10 of 36
Data Types
Scalar type
Numeric: INT, DEC,NUMBER,BINARY_INTEGER Character: CHAR, CHARACTER, STRING, VARCHAR, VARCHAR2 Boolean: TRUE, FALSE. Date: DATE
Lob types
Slide 11 of 36
CURSORS
A cursor is used to process a single row 'at a time' from multiple rows retrieved from the database . Cursors are declared in the Declaration Section.
CURSOR c_query IS SELECT lname, salary FROM employee;
The cursor can be declared for complex queries involving joins and conditions. Cursors must be OPENed to be accessed and CLOSEd before ending the program.
OPEN c_query; CLOSE c_query;
Slide 12 of 36
The FETCH statement is used to retrieve the output of a single record from the CURSOR SELECT statement INTO associate variables.
FETCH c_query INTO v_last_name, v_salary;
CURSORS
Cursors can be opened and closed more than once in a block and if the a WHERE statement exists, the values of the binding variables can be modified. Cursor FOR loop is a special type of for loop which the SQL cursor operations are carried out implicitly.
Slide 13 of 36
Conditional Statements
Conditional Processing The specified conditions are evaluated by the system and the result determines which sequence of statements is to be carried out. IF <boolean expression> THEN <sequence of statements> END IF;
---------------------------------------------------------------------------------------------
IF <boolean expression> THEN <sequence of statements> ELSE <sequence of statements> END IF;
Slide 14 of 36
Loop Structures
Unconstrained Loops
LOOP <sequence of statements> EXIT WHEN <condition> <sequence of statements> END LOOP;
WHILE LOOP
WHILE <condition> LOOP <statements> END LOOP; Note: The loop will continue to process as long as the condition is TRUE or an EXIT (or EXIT WHEN) statement is encountered.
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 18 of 36
FOR LOOP
FOR <loop_counter> IN [REVERSE]
GOTO statement
The label is defined in the block by being enclosed in double angle brackets. LOOP <sequence of statements> IF <condition> THEN GOTO get_out_of_loop; <sequence of statements> END LOOP; <<get_out_of_loop>>
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 20 of 36
NAMED BLOCKS
The following are types of NAMED BLOCKS Stored Procedures
Similar to an anonymous block except it can be stored in the database, can accept parameters, and can be executed over and over again (with different parameters)
Functions
Type of named blocks that is executed within a DML or SQL statement. It may take in one or more parameters and RETURNs only one value back to the calling application.
Triggers
A named block that executes only when an associated DML statement is executed, such as an INSERT, UPDATE, or DELETE statement.
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 21 of 36
(Procedures or Functions)
Program Comments Header IS|AS Declaration Section BEGIN Executable Section EXCEPTION Exception Section END; /
What is a Trigger
Similar to stored procedures and functions. Contains a Declaration, Executable, and Exception sections Differences Triggers are not executed explicitly, they are implicitly execute when a triggering event occurs. (This is called firing the trigger) Triggers do not accept parameters Triggering events are fired by DML Statements ( INSERTs, UPDATEs, or DELETEs) against tables or views AND certain system events
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 30 of 36
Complex integrity constraints are not always possible through declarative constraints enabled at table creation time, such as salary may not be lowered. Auditing information, such as who updated an employee's salary, may be required. Remember triggers happen at the basic DML level. Triggers can signal other application that action needs to take place when changes are made to a table. Example, update employee statistics contained in another table.
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 31 of 36
NOTE: a_trigger_event may be any combination of an INSERT, DELETE, and/or UPDATE on a table or view
Dr. James Dullea, CSC8490 Introduction to PL/SQL
Slide 32 of 36
Error Handling
When errors occur during the execution, control will be branched to the exception handling section.
Practice 1
Use the Company database schema, write a stored procedure to add an employee to the employee table, using parameters to input the data. Use your name and following information to test the procedure.
FNAME MINIT LNAME SSN BDATE STREET CITY STATE ZIP SEX SALARY SUPERSSN DNO use your first name use your middle init or a blank space use your last name make up a 9 digit number use your birthday (be careful of the date format) make up data Villanova PA use the Villanova zip code M or F 38000 333445555 5
Practice 2
Write a function (called GetDay) that will take in a date as a parameter and return the actual name of the day for that date. Use the function to solve the following problem. Using the data in the employee table from Assignment 1, write an SQL statement or an anonymous block (containing the above function GetDay) that uses your first and last name in a where clause to access the record and returns the actual day of the week that you were born. Hint: GetDay(bdate)