So What are Views and Triggers anyway?

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.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
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.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
-Software School of Hunan University-
Database Systems More SQL Database Design -- More SQL1.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
-Software School of Hunan University-
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
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
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 9 Database Triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
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 
A database trigger is a stored PL/SQL program unit associated with a specific database table. ORACLE executes (fires) a database trigger automatically.
10 1 Chapter 10 - A Transaction Management Database Systems: Design, Implementation, and Management, Rob and Coronel.
Database Management COP4540, SCS, FIU Database Trigger.
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 13 Triggers. Trigger Overview A trigger is a program unit that is executed (fired) due to an event Event such as updating tables, deleting data.
Copyright © 2004 Pearson Education, Inc.. Chapter 24 Enhanced Data Models for Advanced Applications.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database System Implementation CSE 507
Fundamentals of DBMS Notes-1.
Chapter 6: Integrity (and Security)
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
Creating Database Triggers
Database Security and Authorization
Constraints and Triggers
Active Database Concepts
Foreign Keys Local and Global Constraints Triggers
SQL Stored Triggers Presented by: Dr. Samir Tartir
Introduction to Database Systems, CS420
CPSC-310 Database Systems
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Advanced SQL: Views & Triggers
Chapter 2 Views.
Instructor: Mohamed Eltabakh
Index Use Cases.
Instructor: Mohamed Eltabakh
Session #, Speaker Name Views 1/2/2019.
Chapter 2 Views.
Introduction to Triggers
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Chapter 7 Using SQL in Applications
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
CPSC-608 Database Systems
Triggers.
Prof. Arfaoui. COM390 Chapter 9
IST 318 Database Administration
SQL – Constraints & Triggers
Query-by-Example Transparencies
TRIGGERS.
-Transactions in SQL -Constraints and Triggers
Assertions and Triggers
Presentation transcript:

So What are Views and Triggers anyway? Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa

Views A view is the dynamic result of a query over the base relations to produce another relation. It is considered virtual because it does not usually exist inside the database, but rather is calculated when needed. To the user, a view appears just like any other table and can be present in any SQL query where a table is present. A view may either be: virtual - produced by a SQL query on demand. materialized - the view is stored as a derived table that is updated when the base relations are updated.

Creating Views Views are created using the CREATE VIEW command: Notes: CREATE VIEW viewName [(col1,col2,...,colN)] AS selectStatement [WITH [CASCADED|LOCAL] CHECK OPTION] Notes: Select statement can be any SQL query and is called the defining query of the view. It is possible to rename the columns when defining the view, otherwise they default to the names produced by the query. WITH CHECK OPTION is used to insure that if updates are performed through the view, the updated rows must satisfy the WHERE clause of the query.

Views Example Create a view that has only the employees of department 'D2': CREATE VIEW empD2 AS SELECT * FROM emp WHERE dno = 'D2'; Create a view that only shows the employee number, title, and name: CREATE VIEW staff (Number,Name,Title) AS SELECT eno,ename,title FROM emp; Note that the first example is a horizontal view because it only contains a subset of the tuples. The second example is a vertical view because it only contains a subset of the attributes.

Using views A view name may be used in exactly the same way as a table name in any SELECT query. One of the most important uses of views is in large multi-user systems, where they make it easy to control access to data for different types of users. You could create separate views even on an Employees table, and control access to them like this: CREATE VIEW phone_view AS SELECT empFName, empLName, empPhone FROM Employees; GRANT SELECT ON phone_view TO public; CREATE VIEW job_view AS SELECT employeeID, empFName, empLName, jobTitle, managerID FROM Employees; GRANT SELECT, UPDATE ON job_view TO managers; CREATE VIEW pay_view AS SELECT employeeID, empFName, empLName, payRate FROM Employees; GRANT SELECT, UPDATE ON pay_view TO payroll;

Triggers One of the problems with assertions is that they may have to be checked on every database modification unless the DBMS is intelligent enough to know when they should be checked. A solution to this problem is to explicitly tell the DBMS what events are of interest and provide an action that should be performed when that event occurs. Triggers are used to perform actions when a defined event occurs. They are newly standardized in SQL3 but have been present in various DBMSs for some time.

Triggers Example Consider this situation where triggers are useful. The WorksOn relation has a foreign key to Emp (eno). If a user inserts a record in WorksOn and the employee does not exist, the insert fails. However with triggers, we can accept the insertion into WorksOn and then create a new record in Emp so that the foreign key constraint is not violated.

Example Trigger

Triggers Syntax Notes: CREATE TRIGGER <name> BEFORE | AFTER | INSTEAD OF <events> [<referencing clause>] [FOR EACH ROW] [WHEN (<condition>)] <action> Notes: BEFORE, AFTER, INSTEAD OF indicate when a trigger is executed. <events> is the events that the trigger will be executed for. It will be one of these events: INSERT ON R DELETE ON R UPDATE [OF A1,A2,..,An] on R

Triggers Syntax - FOR EACH ROW There are two types of triggers: row-level triggers that are executed for each row that is updated, deleted, or inserted. statement-level triggers that are only executed once per statement regardless of how many tuples are affected. Inserting the clause FOR EACH ROW indicates a row-level trigger (the default is a statement-level trigger).

Why Use Triggers? Triggers supplement the standard capabilities of a DBMS. For example, you can use triggers to: Automatically generate derived column values Prevent invalid transactions Enforce complex security authorizations Enforce complex business rules Provide transparent event logging Gather statistics on table access Modify table data when statements are issued against views