Download presentation
Presentation is loading. Please wait.
Published byEdgardo Seawright Modified over 10 years ago
1
Chapter 8 Advanced SQL Pearson Education © 2009
2
Chapter 8 - Objectives u How to use the SQL programming language u How to use SQL cursors u How to create stored procedures u How to create triggers u How to use triggers to enforce integrity constraints Pearson Education © 2009
3
Chapter 8 – Objectives (continued) u The advantages and disadvantages of triggers u How to use recursive queries
4
Pearson Education © 2009 The SQL Programming Language u 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
5
Pearson Education © 2009 The SQL Programming Language (continued) u SQL/PSM (Persistent Stored Modules) u PL/SQL (Procedural Language/SQL) –Oracle’s procedural extension to SQL –Two versions
6
Pearson Education © 2009 Declarations u Variables and constant variables must be declared before they can be referenced u Possible to declare a variable as NOT NULL u %TYPE and %ROWTYPE
7
Pearson Education © 2009 Declarations (continued)
8
Pearson Education © 2009 Assignments u Variables can be assigned in two ways: –Using the normal assignment statement (:) –Using an SQL SELECT or FETCH statement
9
Pearson Education © 2009 Control Statements u Conditional IF statement u Conditional CASE statement u Iteration statement (LOOP) u Iteration statement (WHILE and REPEAT) u Iteration statement (FOR)
10
Pearson Education © 2009 Exceptions in PL/SQL u Exception –Identifier in PL/SQL –Raised during the execution of a block –Terminates block’s main body of actions u Exception handlers –Separate routines that handle raised exceptions u User-defined exception –Defined in the declarative part of a PL/SQL block
11
Pearson Education © 2009 Example of Exception Handling in PL/SQL
12
Pearson Education © 2009 Condition Handling u Define a handler by –Specifying its type –Exception and completion conditions it can resolve –Action it takes to do so u Handler is activated –When it is the most appropriate handler for the condition that has been raised by the SQL statement
13
Pearson Education © 2009 The DECLARE... HANDLER Statement DECLARE {CONTINUE | EXIT | UNDO} HANDLER FOR SQLSTATE {sqlstateValue | conditionName | SQLEXCEPTION |SQLWARNING | NOT FOUND} handlerAction;
14
Pearson Education © 2009 Cursors in PL/SQL u Cursor –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
15
Pearson Education © 2009 Using Cursors in PL/SQL to Process a Multirow Query
16
Pearson Education © 2009 Subprograms, Stored Procedures, Functions, and Packages u Subprograms –Named PL/SQL blocks that can take parameters and be invoked –Types »Stored procedures »Functions –Parameters
17
Pearson Education © 2009 Subprograms, Stored Procedures, Functions, and Packages (continued) u 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
18
Pearson Education © 2009 Triggers u Trigger –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
19
Pearson Education © 2009 Trigger Format
20
Pearson Education © 2009 Using a BEFORE Trigger
21
Pearson Education © 2009 Recursion u Extremely difficult to handle recursive queries –Queries about relationships that a relation has with itself (directly or indirectly) u WITH RECURSIVE u Infinite loop can occur unless the cycle can be detected –CYCLE clause
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.