chapter 3 more sql copyright © 2004 pearson education, inc

56
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.

Upload: henry-hodges

Post on 31-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Chapter 3

MORE SQL

Copyright © 2004 Pearson Education, Inc.

Page 2: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Outline

More Complex SQL Retrieval QueriesSpecifying Constraints as Assertions and

Actions as TriggersViews in SQLSchema Change Statements in SQLDatabase Programming

Slide 3 - 2

Page 3: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

NULLS IN SQL QUERIESSQL allows queries that check if a value is NULL

(missing or undefined or not applicable)SQL uses IS or IS NOT to compare NULLs because it

considers each NULL value distinct from other NULL values, so equality comparison is not appropriate .

Query 14: Retrieve the names of all employees who do not have supervisors.Q14: SELECT FNAME, LNAME

FROMEMPLOYEEWHERE SUPERSSN IS NULL

Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result

Slide 3 - 3

Page 4: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

NESTING OF QUERIES

A complete SELECT query, called a nested query , can be specified within the WHERE-clause of another query, called the outer query

Many of the previous queries can be specified in an alternative form using nesting

Query 1: Retrieve the name and address of all employees who work for the 'Research' department.

Q1: SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEEWHERE DNO IN (SELECT DNUMBERFROM DEPARTMENTWHERE DNAME='Research' )

Slide 3 - 4

Page 5: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CORRELATED NESTED QUERIES

If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query , the two queries are said to be correlated

The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query

Query 12: Retrieve the name of each employee who has a dependent with the same first name as the employee.

Q12: SELECT E.FNAME, E.LNAMEFROM EMPLOYEE AS EWHERE E.SSN IN (SELECT ESSN

FROM DEPENDENTWHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)

Slide 3 - 5

Page 6: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CORRELATED NESTED QUERIES (cont.)

In Q12, the nested query has a different result for each tuple in the outer query

A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. For example, Q12 may be written as in Q12A

Q12A: SELECT E.FNAME, E.LNAMEFROM EMPLOYEE E, DEPENDENT DWHERE E.SSN=D.ESSN AND

E.FNAME=D.DEPENDENT_NAME

Slide 3 - 6

Page 7: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

THE EXISTS FUNCTION

EXISTS is used to check whether the result of a correlated nested query is empty (contains no tuples) or not

We can formulate Query 12 in an alternative form that uses EXISTS as Q12B below

Slide 3 - 7

Page 8: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

THE EXISTS FUNCTION (cont.)Query 12: Retrieve the name of each employee

who has a dependent with the same first name as the employee.

Q12B: SELECT FNAME, LNAMEFROM EMPLOYEEWHERE EXISTS (SELECT *

FROM DEPENDENTWHERE SSN=ESSN AND

FNAME=DEPENDENT_NAME)

Slide 3 - 8

Page 9: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

THE EXISTS FUNCTION (cont.)Query 6: Retrieve the names of employees who have

no dependents.

Q6: SELECT FNAME, LNAMEFROMEMPLOYEEWHERE NOT EXISTS (SELECT *

FROM DEPENDENTWHERE SSN=ESSN)

– In Q6, the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exist , the EMPLOYEE tuple is selected

– EXISTS is necessary for the expressive power of SQL

Slide 3 - 9

Page 10: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

More Comparison Operators

… {=, >, >=, <, <=, <>} {ANY, SOME, ALL} …

Query 17: Retrieve all employees whose salary is greater than the salary of all employees in dept. 5

Q17: SELECT * FROM Employee WHERE Salary > ALL (SELECT Salary

FROM Employee

WHERE DNO=5)

Slide 3 - 10

Page 11: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

EXPLICIT SETS

It is also possible to use an explicit (enumerated) set of values in the WHERE-clause rather than a nested query

Query 13: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3.

Q13: SELECT DISTINCT ESSNFROM WORKS_ONWHERE PNO IN (1, 2, 3)

Slide 3 - 11

Page 12: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Renaming of Attributes in SQL

Rename any attribute that appears in the result of a query by adding the qualifier AS followed by the desired new name

Q8A: SELECT E.Lname AS Employee_name, S.Lname AS Supervisor_name

FROM EMPLOYEE AS E, EMPLOYEE AS S

WHERE E.Super_ssn=S.Ssn

Slide 3 - 12

Page 13: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SET OPERATIONSSQL has directly incorporated some set operationsThere is a union operation (UNION), and in some

versions of SQL there are set difference (MINUS or EXCEPT) and intersection (INTERSECT) operations

The resulting relations of these set operations are sets of tuples; duplicate tuples are eliminated from the result

The set operations apply only to union compatible relations ; the two relations must have the same attributes and the attributes must appear in the same order

Slide 3 - 13

Page 14: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SET OPERATIONS (cont.)

Query 4: Make a list of all project numbers for projects that involve an employee whose last name is 'Smith' as a worker or as a manager of the department that controls the project.

Q4: (SELECT PNAMEFROM PROJECT, DEPARTMENT, EMPLOYEEWHERE DNUM=DNUMBER AND

MGRSSN=SSN AND LNAME='Smith')UNION (SELECT PNAMEFROM PROJECT, WORKS_ON, EMPLOYEEWHERE PNUMBER=PNO AND ESSN=SSN AND

LNAME='Smith')

Slide 3 - 14

Page 15: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

5.7

Slide 2-15

Page 16: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SET OPERATIONS (cont.) Q3: Retrieve the name of each employee who works on

all the projects controlled by department number 5

Q3A: SELECT Fname, Lname

FROM EMPLOYEE

WHERE NOT EXISTS (

(SELECT Pnumber FROM PROJECT)

EXCEPT

(SELECT Pno FROM WORKS_ON WHERE Ssn=Essn));

Slide 3 - 16

Page 17: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Joined Relations Feature in SQL2

Can specify a "joined relation" in the FROM-clause

Looks like any other relation but is the result of a join

Includes inner join and outer joinAllows the user to specify different types of joins

(regular "theta" JOIN, NATURAL JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN, CROSS JOIN, etc)

Slide 3 - 17

Page 18: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Joined Relations Feature in SQL2 (cont.)

Q1: SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND DNUMBER=DNO

could be written as:

Q1: SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE JOIN DEPARTMENT

ON DNUMBER=DNO)WHERE DNAME='Research’

or as:

Q1: SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE NATURAL JOIN (DEPARTMENT

AS DEPT(DNAME, DNO, MSSN, MSDATE)))WHERE DNAME='Research’

Slide 3 - 18

Page 19: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Joined Relations Feature in SQL2 (cont.)

Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME

FROM EMPLOYEE E SWHERE E.SUPERSSN=S.SSN

can be written as:

Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME

FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEESON E.SUPERSSN=S.SSN)

Slide 3 - 19

Page 20: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Joined Relations Feature in SQL2 (cont.)

Another Example; Q2: SELECT Pnumber, Dnum, Lname, Address, Bdate

FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND Plocation=‘Stafford’;

Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS

FROM (PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER)

JOIN EMPLOYEE ON MGRSSN=SSN) ) WHERE PLOCATION='Stafford’

Slide 3 - 20

Page 21: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

AGGREGATE FUNCTIONS

Include COUNT, SUM, MAX, MIN, and AVGQuery 15: Find the maximum salary, the minimum salary,

and the average salary among all employees.

Q15: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)

FROM EMPLOYEE

– Some SQL implementations may not allow more than one function in the SELECT-clause

Slide 3 - 21

Page 22: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

AGGREGATE FUNCTIONS (cont.)

Query 16: Find the maximum salary, the minimum salary, and the average salary among employees who work for the 'Research' department.

Q16: SELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)

FROM EMPLOYEE, DEPARTMENTWHERE DNO=DNUMBER AND

DNAME='Research'

Slide 3 - 22

Page 23: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

AGGREGATE FUNCTIONS (cont.)

Queries 17 and 18: Retrieve the total number of employees in the company (Q17), and the number of employees in the 'Research' department (Q18).

Q17: SELECT COUNT (*)FROM EMPLOYEE

Q18: SELECT COUNT (*)FROM EMPLOYEE,

DEPARTMENTWHERE DNO=DNUMBER AND

DNAME='Research’

Slide 3 - 23

Page 24: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

GROUPINGIn many cases, we want to apply the

aggregate functions to subgroups of tuples in a relation

Each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s)

The function is applied to each subgroup independently

SQL has a GROUP BY-clause for specifying the grouping attributes, which must also appear in the SELECT-clause

Slide 3 - 24

Page 25: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

GROUPING (cont.) Query 20: For each department, retrieve the department

number, the number of employees in the department, and their average salary.

Q20: SELECT DNO, COUNT (*), AVG (SALARY)FROM EMPLOYEEGROUP BY DNO

– In Q20, the EMPLOYEE tuples are divided into groups--each group having the same value for the grouping attribute DNO

– The COUNT and AVG functions are applied to each such group of tuples separately

– The SELECT-clause includes only the grouping attribute and the functions to be applied on each group of tuples

– A join condition can be used in conjunction with grouping

Slide 3 - 25

Page 26: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

GROUPING (cont.)Query 21: For each project, retrieve the project number,

project name, and the number of employees who work on that project.

Q21: SELECT PNUMBER, PNAME, COUNT (*)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAME

– In this case, the grouping and functions are applied after the joining of the two relations

Slide 3 - 26

Page 27: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

THE HAVING-CLAUSE

Sometimes we want to retrieve the values of these functions for only those groups that satisfy certain conditions

The HAVING-clause is used for specifying a selection condition on groups (rather than on individual tuples)

Slide 3 - 27

Page 28: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

THE HAVING-CLAUSE (cont.)

Query 22: For each project on which more than two employees work , retrieve the project number, project name, and the number of employees who work on that project.

Q22: SELECT PNUMBER, PNAME, COUNT (*)

FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAMEHAVING COUNT (*) > 2

Slide 3 - 28

Page 29: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

ORDER BYThe ORDER BY clause is used to sort the tuples

in a query result based on the values of some attribute(s) in ascending or descending order

Query 28: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name.

Q28: SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE,

WORKS_ON, PROJECTWHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBERORDER BY DNAME, LNAME [DESC|ASC]

Slide 3 - 29

Page 30: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Outline

More Complex SQL Retrieval QueriesSpecifying Constraints as Assertions and

Actions as TriggersViews in SQLSchema Change Statements in SQLDatabase Programming

Slide 3 - 30

Page 31: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Constraints as Assertions

General constraints: constraints that do not fit in the basic SQL categories

Mechanism: CREAT ASSERTION– components include: a constraint name,

followed by CHECK, followed by a condition

Slide 3 - 31

Page 32: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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’’

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

Slide 3 - 32

Page 33: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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)

Slide 3 - 33

Page 34: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL Triggers: An Example

A trigger to compare an employee’s salary to his/her supervisor during insert or update operations:

CREATE TRIGGER INFORM_SUPERVISORBEFORE INSERT OR UPDATE OF

SALARY, SUPERVISOR_SSN ON EMPLOYEEFOR EACH ROW

WHEN(NEW.SALARY> (SELECT SALARY FROM EMPLOYEE

WHERE SSN=NEW.SUPERVISOR_SSN))INFORM_SUPERVISOR (NEW.SUPERVISOR_SSN,NEW.SSN;

Slide 3 - 34

Page 35: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL Triggers

More example

How to update Total_sal automatically??

Slide 3 - 35

Page 36: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL TriggersFirstly, determine the events which can impact on what

we want

Secondly, for each event, do we need a condition?– Exp: event 1: if dno is not null

Finally, write the action to solve– Exp: recompute the total salary of department

Slide 3 - 36

Page 37: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 3 - 37

Page 38: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 3 - 38

Page 39: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL Triggers - Syntax

Slide 3 - 39

Page 40: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Outline

More Complex SQL Retrieval QueriesSpecifying Constraints as Assertions and

Actions as TriggersViews in SQLSchema Change Statements in SQL

Slide 3 - 40

Page 41: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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 operationsA convenience for expressing certain

operations

Slide 3 - 41

Page 42: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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

Slide 3 - 42

Page 43: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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;

Slide 3 - 43

Page 44: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Using a Virtual Table

We can specify SQL queries on a newly 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;

Slide 3 - 44

Page 45: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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)

– Exp:

Slide 3 - 45

Page 46: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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– kept as a materialized (physically stored) table

as long as it is being queried

Slide 3 - 46

Page 47: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

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

Slide 3 - 47

Page 48: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 3 - 48

Page 49: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Un-updatable Views

Views defined using groups and aggregate functions are not updateable

Views defined on multiple tables using joins are generally not updateable

CREATE VIEW syntax has a WITH CHECK OPTION will prevent data being added or modified within the view that cannot subsequently be retrieved from the view

Slide 3 - 49

Page 50: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Un-updatable Views

WITH CHECK OPTION

Exp:

CREATE VIEW emp_dep5ASSELECT SSN, Lname, Fname, Dno FROM EMPLOYEE WHERE DNO = 5 WITH CHECK OPTION

INSERT INTO emp_dep5 VALUES (‘111112222’, ‘Bob’, ‘Smith’, 3)

Cannot insert successfully

Slide 3 - 50

Page 51: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Views in SQL

CREATE VIEW [<database_name>.][<owner>.]view_name [ (column[,...n])] AS select_statement [ WITH CHECK OPTION ]

Slide 3 - 51

Page 52: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Outline

More Complex SQL Retrieval QueriesSpecifying Constraints as Assertions and

Actions as TriggersViews in SQLSchema Change Statements in SQL

Slide 3 - 52

Page 53: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

DROP Command

Two drop behavior options: CASCADE & RESTRICT

Examples:

DROP SCHEMA COMPANY CASCADE;

DROP TABLE DEPENDENT RESTRICT;

Slide 3 - 53

Page 54: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

ALTER TABLEUsed to add an attribute to one of the base relationsThe new attribute will have NULLs in all the tuples of the relation

right after the command is executed; hence, the NOT NULL constraint is not allowed for such an attribute

Example:ALTER TABLE EMPLOYEE ADD COLUMN JOB VARCHAR(12);

ALTER TABLE COMPANY.EMPLOYEE DROP COLUMN Address CASCADE;

ALTER TABLE COMPANY.DEPARTMENT ALTER COLUMN Mgr_ssn DROP DEFAULT;

ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE;

View the syntax in book or google

Slide 3 - 54

Page 55: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Summary

Slide 3 - 55

Page 56: Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Summary

Slide 3 - 56