0% found this document useful (0 votes)
13 views17 pages

Slide 9-1

Uploaded by

rr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views17 pages

Slide 9-1

Uploaded by

rr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Copyright © 2007 Ramez Elmasri and Shamkant B.

Navathe Slide 9- 1
Chapter 2
ASSERTIONS & TRIGGERS

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe


Chapter Outline
 General Constraints as Assertions
 Triggers
 Views in SQL
 Summary

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 3


Chapter Objectives
 Specification of more general constraints via
assertions
 SQL facilities for defining views (virtual tables)
 Various techniques for accessing and
manipulating a database via programs in general-
purpose languages
 E.g., Java, C++, etc.

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 4


Constraints as Assertions
 General constraints: constraints that do not fit in
the basic SQL categories (presented in chapter
8)
 Mechanism: CREAT ASSERTION
 Components include:
 a constraint name,
 followed by CHECK,
 followed by a condition

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 5


Assertions: An Example
 “The salary of an employee must not be greater
than the salary of the manager of the department
that the employee works for’’ constraint
name,
CREAT ASSERTION SALARY_CONSTRAINT CHECK,
condition
CHECK (NOT EXISTS (SELECT *
FROM EMPLOYEE E, EMPLOYEE M,
DEPARTMENT D
WHERE E.SALARY > M.SALARY AND
E.DNO=D.NUMBER AND
D.MGRSSN=M.SSN))

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 6


Using General Assertions
 Specify a query that violates the condition;
include inside a NOT EXISTS clause
 Query result must be empty
 if the query result is not empty, the assertion has
been violated

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 7


SQL Triggers
 Objective: to monitor a database and take initiate
action when a condition occurs
 Triggers are expressed in a syntax similar to
assertions and include the following:
 Event
 Such as an insert, deleted, or update operation
 Condition
 Action
 To be taken when the condition is satisfied

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 8


SQL Triggers: An Example
 A trigger to compare an employee’s salary to his/her
supervisor during insert or update operations:

CREATE TRIGGER INFORM_SUPERVISOR


BEFORE INSERT OR UPDATE OF
SALARY, SUPERVISOR_SSN ON EMPLOYEE
FOR EACH ROW
WHEN
(NEW.SALARY> (SELECT SALARY FROM EMPLOYEE
WHERE SSN=NEW.SUPERVISOR_SSN))
INFORM_SUPERVISOR
(NEW.SUPERVISOR_SSN,NEW.SSN);

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 9


Views in SQL
 A view is a “virtual” table that is derived from
other tables
 Allows for limited update operations
 Since the table may not physically be stored
 Allows full query operations
 A convenience for expressing certain operations

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 10


Specification of Views
 SQL command: CREATE VIEW
 a table (view) name
 a possible list of attribute names (for example,
when arithmetic operations are specified or when
we want the names to be different from the
attributes in the base relations)
 a query to specify the table contents

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 11


SQL Views: An Example
 Specify a different WORKS_ON table

CREATE VIEW WORKS_ON_NEW AS


SELECT FNAME, LNAME, PNAME, HOURS
FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE SSN=ESSN AND PNO=PNUMBER
GROUP BY PNAME;

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 12


Using a Virtual Table
 We can specify SQL queries on a newly create
table (view):
SELECT FNAME, LNAME
FROM WORKS_ON_NEW
WHERE PNAME=‘Seena’;

 When no longer needed, a view can be dropped:


DROP WORKS_ON_NEW;

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 13


Efficient View Implementation
 Query modification:
 Present the view query in terms of a query on the
underlying base tables
 Disadvantage:
 Inefficient for views defined via complex queries
 Especially if additional queries are to be applied to
the view within a short time period

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 14


Efficient View Implementation
 View materialization:
 Involves physically creating and keeping a
temporary table
 Assumption:
 Other queries on the view will follow
 Concerns:
 Maintaining correspondence between the base
table and the view when the base table is updated
 Strategy:
 Incremental update

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 15


Update Views
 Update on a single view without aggregate
operations:
 Update may map to an update on the underlying
base table
 Views involving joins:
 An update may map to an update on the
underlying base relations
 Not always possible

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 16


Un-updatable Views
 Views defined using groups and aggregate
functions are not updateable
 Views defined on multiple tables using joins are
generally not updateable
 WITH CHECK OPTION: must be added to the
definition of a view if the view is to be updated
 To allow check for updatability and to plan for an
execution strategy

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 9- 17

You might also like