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.
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
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))
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
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
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);
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
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
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
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
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
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