Advanced SQL: Views & Triggers

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

1 Constraints, Triggers and Active Databases Chapter 9.
IC and Triggers in SQL. Find age of the youngest sailor with age
SQL Constraints and Triggers
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5 (cont.)
Database Systems More SQL Database Design -- More SQL1.
Cs3431 Triggers vs Constraints Section 7.5. cs3431 Triggers (Make DB Active) Trigger: A procedure that starts automatically if specified changes occur.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Advanced SQL Instructor: Mohamed Eltabakh 1 Part II.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
1 SQL: Structured Query Language Chapter 5 (cont.)  Constraints  Triggers.
Assertions and Triggers in SQL
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
A database trigger is a stored PL/SQL program unit associated with a specific database table. ORACLE executes (fires) a database trigger automatically.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database Management COP4540, SCS, FIU Database Trigger.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 6: Integrity (and Security)
SQL – Part 2.
Creating Database Triggers
Constraints and Triggers
Active Database Concepts
Instructor: Jason Carter
Foreign Keys Local and Global Constraints Triggers
SQL Stored Triggers Presented by: Dr. Samir Tartir
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Introduction to Database Systems, CS420
CPSC-608 Database Systems
SQL Views CS542.
Instructor: Mohamed Eltabakh
PL/SQL Programing : Triggers
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
CMSC-461 Database Management Systems
Introduction to Triggers
SQL: Structured Query Language
Chapter 7 Using SQL in Applications
Relational Database Design
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
Chapter 11 Managing Databases with SQL Server 2000
Triggers.
SQL: Structured Query Language
SQL – Constraints & Triggers
CPSC-608 Database Systems
So What are Views and Triggers anyway?
Triggers 7/11/2019 See scm-intranet.
TRIGGERS.
Assertions and Triggers
Advanced Topics: Indexes & Transactions
Presentation transcript:

Advanced SQL: Views & Triggers Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

Views

What is a View An SQL query that we register (store) inside the database Any query can be a view CREATE VIEW <name> AS <select statement>; DROP VIEW <name>; ProfNumStudents is a view with schema (pNumber, CNT) CREATE VIEW ProfNumStudents AS SELECT pNumber, count(*) AS CNT FROM Student GROUP BY pNumber;

Why Need a View Frequent queries: query is used again and again Complex queries: query written once and stored in the database Logical data independence: the base table may change but the the view is still the same Hide information (Security): allow users to see the view but not the original tables CREATE VIEW StudentBasicInfo AS SELECT sNumber, sName FROM Student; See only sNumber and sName

View Schema You can think of a view as a table, but it gets its data during runtime Only the definition is stored without data View Schema Consists of the columns produced from the select statement CREATE VIEW ProfNumStudents AS SELECT pNumber, count(*) AS CNT FROM Student GROUP BY pNumber; ProfNumStudents(pNumber, CNT) CREATE VIEW StudentBasicInfo AS SELECT sNumber, sName FROM Student; StudentBasicInfo(sNumber, sName)

Example Customers who have accounts Customers who have loans

Example (Cont’d) , ‘A’ as type , ‘L’ as type In this example, we added an extra column (constant) to differentiate between the two customer types , ‘A’ as type , ‘L’ as type

Querying a View Exactly as querying a table

Triggers

Triggers: Introduction The application constraints need to be captured inside the database Some constraints can be captured by: Primary Keys, Foreign Keys, Unique, Not NULL, and domain Other application constraints are more complex Need for assertions and triggers Sum of loans taken by a customer does not exceed 100,000

Triggers A procedure that runs automatically when a certain event occurs in the DBMS The procedure performs some actions, e.g., Check certain values Fill in some values Inserts/deletes/updates other records Check that some business constraints are satisfied Commit (approve the transaction) or roll back (cancel the transaction)

Lets see how to define these components Trigger Components Three components Event: When this event happens, the trigger is activated Condition (optional): If the condition is true, the trigger executes, otherwise skipped Action: The actions performed by the trigger Semantics When the Event occurs and Condition is true, execute the Action Lets see how to define these components

Trigger: Event Example That is the event Create Trigger <name> Before|After Insert|Update|Delete ON <tablename> …. That is the event Example Create Trigger ABC Before Insert On Students …. This trigger is activated when an insert statement is issued, but before the new record is inserted Create Trigger XYZ After Update On Students …. This trigger is activated when an update statement is issued and after the update is executed

Granularity of Event An UPDATE or DELETE statement may update (or delete) many records at the same time May insert many records in the same statement as well Does the trigger execute for each updated or deleted record, or once for the entire statement ? We define such granularity Create Trigger <name> Before| After Insert| Update| Delete For Each Row | For Each Statement …. That is the event That is the granularity

Example: Granularity of Event Create Trigger XYZ After Update ON <tablename> For each statement …. This trigger is activated once (per UPDATE statement) after all records are updated Create Trigger XYZ Before Delete ON <tablename> For each row …. This trigger is activated before deleting each record

Trigger: Condition This component is optional That is the condition Create Trigger <name> Before| After Insert| Update| Delete On <tableName> For Each Row | For Each Statement When <condition> … That is the condition If the employee salary > 150,00, then some actions will be taken Create Trigger EmpSal After Insert or Update On Employee For Each Row When (New.salary >150,000) …

Use REFERENCING Clause Trigger: Action Action depends on what you want to do, e.g.: Check certain values Fill in some values Inserts/deletes/updates other records Check that some business constraints are satisfied Commit (approve the transaction) or roll back (cancel the transaction) In the action, you may want to reference: The new values of inserted or updated records The old values of deleted or updated records Use REFERENCING Clause

Trigger: Referencing Clause Create Trigger <name> Before| After Insert| Update| Delete On <tableName> Referencing OLD AS oldRec, NEW AS newRec For Each Row | For Each Statement When <condition> Begin …. End; That is Referencing clause That is the action Now, can reference the old record as oldRec and new record as newRec Insert event does not have old record Delete event does not have new record Update event has both old and new

Example 1 If the employee salary increased by more than 10%, make sure the ‘comment’ field is not empty and its value has changed, otherwise reject the update Create Trigger EmpSal Before Update On Employee Referencing OLD ROW AS oldRec, NEW ROW AS newRec For Each Row Begin IF (newRec.salary > oldRec.salary * 1.10) Then IF (newRec.comment = ‘’ or newRec.comment is null or newRec.comment = oldRec.comment) RAISE_APPLICATION_ERROR(-20004, ‘Comment field not correct’); End IF; End;

Example 2: Using Temp Variable If the newly inserted record in employee has null date field, fill it in with the current date Create Trigger EmpDate Before Insert On Employee Referencing NEW ROW AS newRec For Each Row Declare temp date; Begin Select sysdate into temp from dual; IF (newRec.date is null) Then newRec.date := temp; End IF; End; Define variables Oracle system table always has the current date Updating the new value to be inserted

Example 3: Maintenance of Derived Attributes Keep the bonus attribute in Employee table always 3% of the salary attribute Create Trigger EmpBonus Before Insert Or Update On Employee For Each Row Begin newRec.bonus := newRec.salary * 0.03; End; Indicate two events at the same time The bonus value is always computed automatically