Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual.

Similar presentations


Presentation on theme: "Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual."— Presentation transcript:

1 Chapter 7 SQL HUANG XUEHUA

2

3

4 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)

5 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

6 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’’ CREATE ASSERTION SALARY_CONSTRAINT 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))

7 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

8 SQL Triggers Objective: to monitor a database and take action when a condition occurs Triggers are expressed in a syntax similar to assertions and include the following:  event (e.g., an update operation)  condition  action (to be taken when the condition is satisfied)

9 SQL Triggers: An Example A trigger to compare an employee’s salary to his/her supervisor during insert or update operations: REATE TRIGGER reminder1 ON employee AFTER INSERT, UPDATE AS RAISERROR ('Notify Customer Relations', 16, 10) GO

10 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

11 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

12 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;

13 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 VIEW WORKS_ON_NEW;

14 Efficient View Implementation (1) 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)

15 Efficient View Implementation (2) 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

16 View Update 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

17 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

18 Database Programming Objective: to access a database from an application program (as opposed to interactive interfaces) Why? An interactive interface is convenient but not sufficient; a majority of database operations are made thru application programs (nowadays thru web applications)

19 Database Programming Approaches Embedded commands: database commands are embedded in a general-purpose programming language Library of database functions: available to the host language for database calls; known as an API A brand new, full-fledged language (minimizes impedance mismatch)


Download ppt "Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual."

Similar presentations


Ads by Google