Revision on Triggers and Cursors. Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
Chapter 4B: More Advanced PL/SQL Programming
PL/SQL Agenda: Basic PL/SQL block structure
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Some Introductory Programming 1. Structured Query Language - used for queries. - a standard database product. 2. Visual Basic for Applications - use of.
1 PL/SQL programming Procedures and Cursors Lecture 1 Akhtar Ali.
PL/SQL Introduction Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
PL/SQL Bulk Collections in Oracle 9i and 10g Kent Crotty Burleson Consulting October 13, 2006.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Agenda Journalling More Embedded SQL. Journalling.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Stored procedures1 Stored procedures and functions Procedures and functions stored in the database.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Procedure Function Trigger. create table employee ( id number, name varchar2(100), deptno number, salary float, primary key(id) ) create table deptincrease.
1. 1. Which type of argument passes a value from a procedure to the calling program? A. VARCHAR2 B. BOOLEAN C. OUT D. IN 2.
Advanced SQL: Cursors & Stored Procedures
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
CS178 Database Management PL/SQL session 8 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 ISYS Triggers. 2 Agenda Triggers Review Correlation identifiers (pseudo records) Restrictions on triggers Trigger usage Mutating tables Enabling.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Trigger Oracle PL/SQL. Triggers Associated with a particular table Associated with a particular table Automatically executed when a particular event occurs.
Database Management COP4540, SCS, FIU Oracle PL/SQL (Ch 10.5)
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Technology Jing Shen.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
Objectives Database triggers and syntax
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
implicit and an explicit cursor
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
PL/SQL programming Procedures and Cursors Lecture 1 [Part 2]
Advanced Databases More Advanced PL/SQL Programing 1.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
CS422 Principles of Database Systems Stored Procedures and Triggers Chengyu Sun California State University, Los Angeles.
1. Advanced SQL Functions Procedural Constructs Triggers.
A Guide to SQL, Seventh Edition
Active Database Concepts
Difference between Oracle PL/SQL and MySQL
More on Procedures (Internal/Local procedures)
Error Handling Summary of the next few pages: Error Handling Cursors.
PL/SQL Programing : Triggers
Unit I-2.
Chapter 8 Advanced SQL.
Information Management
Presentation transcript:

Revision on Triggers and Cursors

Walk through of exam type question. Question 1. A trigger is required to automatically update the number of rooms available at any hotel once a booking has been made. Using SQL write the trigger that would to perform this action. HOTEL : availablerooms HotelID Booking : HotelID

Solution Create or Replace Trigger roomsavailable After insert on booking For each row Begin Update HOTEL Set AvailableRooms = AvailableRooms – :new.NumOfRooms Where HotelID = :new.HotelID; End; 1.Call the create command and give the trigger a sensible name 2.State the timing o the trigger 3.What table is going to instigate the trigger 4.Row level or table level trigger 5.SQL commands

Marking scheme Create or Replace Trigger roomsavailable 1 mark for correct call After insert 1 mark for correct timing on booking 1 mark for correct table For each row 1 mark for correct level call Begin Update HOTEL 2 marks for correct SQL Set AvailableRooms = AvailableRooms – :new.NumOfRooms ½ mark for use of :new Where HotelID = :new.HotelID; End; ½ mark for correct syntax

Cursors (includes also procedures) A report is required; this report will contain details of all the hotel availability for the coming summer season. An additional field has been added to the HOTEL table, this field is to contain summer status and will be referred to as PeakAvailablility. The new field has 4 potential values: GOOD, RESTRICTED, POOR & FULL this relates to the percentage of rooms that are taken. If less than 60% of rooms are available then the availability is GOOD, between 60% and 80% RESTRICTED, 81% - 99% POOR and 100% FULL. A cursor will be required to retrieve the hotel details in order to perform the calculations. Declare the cursor. [ 3 marks] Write the extract of code which will call the cursor and calculate and then update the value of PeakAvailablility, You should use SQL or Plain English, NOTE if you use plain English you will receive a max of 4 marks [10 marks]

Solution part a) + marking scheme A cursor will be required to retrieve the hotel details in order to perform the calculations 1. Declare variable as cursor with sensible name 2. State select statement for cursor results. cursor hotel_rating is 1 mark for correct declaration select HotelID, AvailableRooms, TotalRooms ½ mark for remembering is from HOTEL 1 ½ marks for SQL commands

Solution part b) Write the code which will call the cursor and calculate and then update the value of PeakAvailablility. You should use PL/SQL. Create or replace procedure peakavail as IDinteger; AvRmsinteger; TotRmsinteger; cursor hotels is select HotelID, AvailableRooms, TotalRooms from HOTEL; Begin Open hotels; Loop Fetch hotels into ID,avrms,totrms; Exit when hotels%notfound; SQL CODE End loop; End;

Cont …. Create or replace procedure peakavail as1 mark for declaration IDinteger; AvRmsinteger; TotRmsinteger; cursor hotels is select HotelID, AvailableRooms, TotalRooms from HOTEL; Percentinteger;2 mark for variable declarations Begin Open hotels;½ mark for opening curser Loop1 mark for loop call Fetch hotels into ID,avrms,totrms;1 mark for fetch in correct order Exit when hotels%notfound;1 mark for exit criteria percent = (avrms/totrms) * mark for calculating % available if (percent <= 60) then 2 marks for IF statement code update hotel set PeakAvailablility = ‘GOOD’ where hotelID = ID if (percent >60) && (percent <=80) then update hotel set PeakAvailablility = ‘RESTRICTED’ where hotelID = ID if (percent > 80) && (percent <=99) then update hotel set PeakAvailablility = ‘POOR’ where hotelID = ID if (percent = 100) then update hotel set PeakAvailablility = ‘FULL’ where hotelID = ID End loop;½ mark for end loop command End;