CSC 443 Database Management Systems. The SQL Programming Language



Similar documents
ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Oracle 11g PL/SQL training

When an exception occur a message which explains its cause is received. PL/SQL Exception message consists of three parts.

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

CS346: Database Programming.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Database Programming with PL/SQL: Learning Objectives

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Stored Routines in Transactional Context

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Oracle PL/SQL Language. CIS 331: Introduction to Database Systems

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

COMS20700 DATABASES 13 PL/SQL. COMS20700 Databases Dr. Essam Ghadafi

Oracle Database: SQL and PL/SQL Fundamentals

Oracle For Beginners Page : 1

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

MySQL 5.0 Stored Procedures

Oracle For Beginners Page : 1

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Oracle Database: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

Oracle Database: SQL and PL/SQL Fundamentals

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Developing SQL and PL/SQL with JDeveloper

Oracle(PL/SQL) Training

Oracle PL/SQL Injection

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle Database: Program with PL/SQL

Persistent Stored Modules (Stored Procedures) : PSM

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Oracle Database 10g: Program with PL/SQL

Oracle Database 11g: Program with PL/SQL

A basic create statement for a simple student table would look like the following.

14 Triggers / Embedded SQL

Programming Database lectures for mathema

Oracle Database: Develop PL/SQL Program Units

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Darshan Institute of Engineering & Technology PL_SQL

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: Program with PL/SQL

More SQL: Assertions, Views, and Programming Techniques

Why programming extensions? to program complex update transactions. where referential integrity is not addressed by data definition

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

Handling PL/SQL Errors

An Introduction to PL/SQL. Mehdi Azarmi

Oracle 10g PL/SQL Training

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Writing Control Structures

Introduction to PL/SQL Programming

C H A P T E R Condition Handling

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

Oracle Database: SQL and PL/SQL Fundamentals NEW

Best Practices for Dynamic SQL

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

CS 632 Advanced Database Systems Object-Relational Database (ORDB) Dr. H. Assadipour

Oracle Database 11g Express Edition PL/SQL and Database Administration Concepts -II

5.1 Database Schema Schema Generation in SQL

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Training Guide. PL/SQL for Beginners. Workbook

Bull s Database Migration Business Unit Oracle PL/SQL to PostgreSQL PL/pgSQL Translation Technical Notes

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Chapter 8: Introduction to PL/SQL 1

Creating Customized Oracle Forms

How To Name A Program In Apl/Sql

DECLARATION SECTION. BODY STATEMENTS... Required

Extracting META information from Interbase/Firebird SQL (INFORMATION_SCHEMA)

PROCEDURES, FUNCTIONS AND PACKAGES

SQL. by Steven Holzner, Ph.D. ALPHA. A member of Penguin Group (USA) Inc.

Querying Microsoft SQL Server

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Oracle8/ SQLJ Programming

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University


SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Handling PL/SQL Errors

ISYS PL/SQL Basics. Week 03

Oracle9i: Develop PL/SQL Program Units

Embedded SQL programming

Chapter 39. PL/pgSQL - SQL Procedural Language

Course ID#: W 35 Hrs. Course Content

Fine Grained Auditing In Oracle 10G

Querying Microsoft SQL Server 20461C; 5 days

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql

Data security best practices

Oracle to MySQL Migration

Transactional Updates to Enterprise GIS data sets. Presented by: Kelly Ratchinsky PBC Countywide GIS Coordinator

AUTHENTICATION... 2 Step 1:Set up your LDAP server... 2 Step 2: Set up your username... 4 WRITEBACK REPORT... 8 Step 1: Table structures...

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Transcription:

CSC 443 Database Management Systems Lecture 11 SQL Procedures and Triggers The SQL Programming Language By embedding SQL in programs written in other high-level programming languages, we produce impedance mismatch Mixing different programming paradigms SQL is a declarative language High-level language such as C is a procedural language SQL and 3GLs use different models to represent data 1

SQL Programming Language (continued) SQL/PSM (Persistent Stored Modules) PL/SQL (Procedural Language/SQL) Oracle s procedural extension to SQL Two versions Many of these features also exist in MySQL. Declarations Variables and constant variables must be declared before they can be referenced Examples vstaffno VARCHAR(25); vrent NUMBER(6, 2) NOT NULL := 600 Possible to declare a variable as NOT NULL %TYPE and %ROWTYPE 2

Declarations (continued) %TYPE and %ROWTYPE Examples Individual variables vstaffno Staff.staffNo%TYPE; vstaffno1 vstaffno%type; Example Row as a Variable vstaffrec Staff%ROWTYPE; %TYPE and %ROWTYPE are not standard SQL. General Structure of a SQL Procedure Block [DECLARE --declarations] BEGIN -executable statements [EXCEPTION --exception handlers] END; Optional Mandatory Optional Mandatory 3

Assignments Variables can be assigned in two ways: Using the normal assignment statement (:) Using an SQL SELECT or FETCH state Examples vstaffno := SG14 ; vrent := 500; SELECT COUNT(*) INTO x FROM PropertyForRent WHERE staffno=vstaffno; Control Statements Conditional IF statement Conditional CASE statement Iteration statement (LOOP) Iteration statement (WHILE and REPEAT) 4

Conditional IF statement IF search_condition THEN statement_list [ELSEIF search_condition THEN statement_list]... [ELSE statement_list] END IF Conditional IF statement - Example DELIMITER // CREATE FUNCTION SimpleCompare(n INT, m INT) RETURNS VARCHAR(20) BEGIN DECLARE s VARCHAR(20); IF n > m THEN SET s = '>'; ELSEIF n = m THEN SET s = '='; ELSE SET s = '<'; END IF; SET s = CONCAT(n, ' ', s, ' ', m); RETURN s; END // 5

Conditional CASE statement CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_list]... [ELSE statement_list] END CASE Conditional CASE statement Alternate Form CASE WHEN search_condition THEN statement_list [WHEN search_condition THEN statement_list]... [ELSE statement_list] END CASE 6

Conditional CASE statement Example DELIMITER CREATE PROCEDURE p() BEGIN DECLARE v INT DEFAULT 1; CASE v WHEN 2 THEN SELECT v; WHEN 3 THEN SELECT 0; ELSE BEGIN END; END CASE; END; Iteration statement (LOOP) Syntax: [begin_label:] LOOP statement_list END LOOP [end_label] The statements within the loop are repeated until the loop is terminated. Usually, this is accomplished with a LEAVE statement. Within a stored function, RETURN can also be used, which exits the function entirely. 7

LOOP Statement - Example CREATE PROCEDURE doiterate(p1 INT) BEGIN label1: LOOP SET p1 = p1 + 1; IF p1 < 10 THEN ITERATE label1; END IF; LEAVE label1; END LOOP label1; SET @x = p1; END; Iteration statement (WHILE ) Syntax: [begin_label:] WHILE search_condition DO statement_list END WHILE [end_label] The statement list within a WHILE statement is repeated as long as the search_condition expression is true. statement_list consists of one or more SQL statements, each ending with a semicolon (;) 8

WHILE Statement - Example CREATE PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5; WHILE v1 > 0 DO... SET v1 = v1-1; END WHILE; END; Iteration statement (REPEAT) Syntax: [begin_label:] REPEAT statement_list UNTIL search_condition END REPEAT [end_label] The statement list within a REPEAT statement is repeated until the search_condition expression is true; a REPEAT always enters the loop at least once 9

mysql> delimiter // REPEAT Statement - Example mysql> CREATE PROCEDURE dorepeat(p1 INT) -> BEGIN -> SET @x = 0; -> REPEAT -> SET @x = @x + 1; -> UNTIL @x > p1 END REPEAT; -> END -> // Query OK, 0 rows affected (0.00 sec) mysql> mysql> CALL dorepeat(1000)// Query OK, 0 rows affected (0.00 sec) mysql> SELECT @x// +------+ @x +------+ 1001 +------+ 1 row in set (0.00 sec) 10

Exceptions in PL/SQL Exception Identifier in PL/SQL Raised during the execution of a block Terminates block s main body of actions Exception handlers Separate routines that handle raised exceptions User-defined exception Defined in the declarative part of a PL/SQL block Example of Exception Handling in PL/SQL DECLARE vpcount NUMBER; vstaffno PropertyForRent.staffNo%TYPE := 'SG14'; --define an exception for the enterprise constraint --that prevents a member of staff from managing more --than 100 properties e_too_many_properties EXCEPTION; PRAGMA EXCEPTION_INIT(e_too_many_properties, - 20000); BEGIN SELECT COUNT(*) INTO VPCount; FROM PropertyForRent WHERE staffno = vstaffno; 11

IF vpcount = 100 --raise an exception for the general constraint RAISE e_too_many_properties; END IF; UPDATE PropertyForRentset staffno = vstaffno WHERE propertyno='pg4'; EXCEPTION --handle the exception for the general constraint WHEN e_too_many_properties THEN dbms_output.put_line('member of staff' staffno 'already managing 100 properties'); END; Define a handler by Condition Handling Specifying its type Exception and completion conditions it can resolve Action it takes to do so Handler is activated When it is the most appropriate handler for the condition that has been raised by the SQL statement 12

The DECLARE... HANDLER Statement DECLARE handler_action HANDLER FOR condition_value [, condition_value]... statement handler_action: CONTINUE EXIT UNDO (not supported by MySQL) condition_value: mysql_error_code SQLSTATE [VALUE] sqlstate_value condition_name SQLWARNING NOT FOUND SQLEXCEPTION Cursor Cursors in SQL Allows the rows of a query result to be accessed one at a time Must be declared and opened before use Must be closed to deactivate it after it is no longer required Updating rows through a cursor 13

Using Cursors in PL/SQL to Process a Multirow Query DECLARE vpropertyno PropertyForRent.propertyNo%TYPE; vstreet PropertyForRent.street%TYPE; vcity PropertyForRent.city%TYPE; vpostcode PropertyForRent.postcode%TYPE; CURSOR propertycursor IS SELECT propertyno,street, city, postcode FROM PropertyForRent WHERE staffno = 'SG14' ORDER BY propertyno; BEGIN --Open the cursor to start of seelction, then loop --to fetch each row of the result table. OPEN propertycursor; LOOP --Fetch next row of the result table FETCH propertycursor INTO vpropertyno, vstreet, vcity, vpostcode; EXIT WHEN propertycursor%notfound; --Display data dbms_output.put_line ('Property number:' vpropertyno); dbms_output.put_line ('Street ' vstreet); 14

dbms_output.put_line('city ' vcity); IF postcode IS NOT NULL THEN dbms_output.put_line ('Postal Code ' vpostcode); ELSE dbms_output.put_line('postal Code NULL'); END IF; END LOOP; IF propertycursor%isopen THEN CLOSE propertycursor END IF; --Error condition print out error EXCEPTION WHEN OTHER THEN dbms_output.put_line('error detected'); IF propertycursor%isopen THEN CLOSE propertycursor END IF: END; 15

Subprograms, Stored Procedures, Functions, and Packages Package Collection of procedures, functions, variables, and SQL statements that are grouped together and stored as a single program unit Specification Declares all public constructs of the package Body Defines all constructs (public and private) of the package Subprograms, Stored Procedures, Functions, and Packages (continued) Subprograms Named PL/SQL blocks that can take parameters and be invoked Types Stored procedures Functions Parameters 16

Trigger Triggers Defines an action that the database should take when some event occurs in the application Format of a trigger Types TRIGGER Privilege Advantages and disadvantages of triggers CREATE TRIGGER Syntax CREATE [DEFINER = { user CURRENT_USER }] TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_body trigger_time: { BEFORE AFTER } trigger_event: { INSERT UPDATE DELETE } 17