Docsity

Database Systems: Subqueries, Quantified Queries, and Indexing in EECS 647 - Prof. Jun Hua, Study notes of Deductive Database Systems

Partial preview of the text

Download Database Systems: Subqueries, Quantified Queries, and Indexing in EECS 647 - Prof. Jun Hua and more Study notes Deductive Database Systems in PDF only on Docsity!

EECS 647: Introduction to

Database Systems^ Instructor: Luke Huan

Spring 2009

4/1/^

Luke Huan Univ. of Kansas

Summary of SQL Features

z^ Query^ z^

SELECT

- FROM

- WHERE

statements

z^ Set and bag operations z^ Table expressions, subqueries z^ Aggregation and grouping z^ Ordering z^ Outerjoins z Table creation, constraints, and content updates

4/1/^

Luke Huan Univ. of Kansas Quantified subqueries

z^ A quantified subquery can be used as a value in a

WHERE

condition z Universal quantification (for all):…^ WHERE

x op^ ALL

( subquery

) …

z^ True iff for all

t^ in the result of

subquery

,^ x op t

z^ Existential quantification (exists):…^

WHERE

x op^ ANY

( subquery

) …

z^ True iff there exists some

t^ in the result of

subquery

such that

x op t

)^ Beware^ z^ In common parlance, “any” and “all” seem to be synonyms^ z^ In SQL,

ANY^ really means “some” z^ Also may use

EXISTS (NOT EXISTS), IN (NOT IN)

4/1/^

Luke Huan Univ. of Kansas

Examples of quantified subqueries

z^ Which employees have the highest salary?Employee (Sid, Name, Salary)^ z^

SELECT

FROM

Employee WHERE

Salary

>=^

ALL

(SELECT

Salary

FROM

Employee);

z^ How about the lowest salary?^ z^

SELECT

FROM

Employee WHERE

Salary

<=^

ALL

(SELECT

salary

FROM

Employee);

4/1/^

Luke Huan Univ. of Kansas

Nested Queries: Scoping

z^ To find out which table a column belongs to^ z^

Start with the immediately surrounding query z If not found, look in the one surrounding that; repeat ifnecessary

z^ Use

table_name

. column_name

notation and

AS

(renaming) to avoid confusion

4/1/^

Luke Huan Univ. of Kansas An Example

SELECT * FROM Student sWHERE EXISTS

(SELECT * FROM Enroll eWHERE SID = s.SIDAND EXISTS

(SELECT * FROM EnrollWHERE SID = s.SIDAND CID <> e.CID));

Students who are taking at least two courses

4/1/^

Luke Huan Univ. of Kansas Database Design

z^ Active database z^ View z^ Indexing z^ Thinking about the following “real-world” situationYou need to design a database for students to record studentname, gpa (>0), id, email address, home department, and images.Constraints: student id is the primary key, student gpa can not benegative, a student may take up to 10 courses in a semester(unless they are senior), and whenever a new honor student isentered in the table, this student is automatically enrolled a honorcourse “Bio101, How Jayhawk fly”

4/1/^

Luke Huan Univ. of Kansas Active Database

z^ Constraints are efficient ways to improve the quality ofthe database. z^ We have covered domain constraint z^ We will talk about^ z^

Check z Assertion z Trigger

4/1/^

Luke Huan Univ. of Kansas Assertion

z^ Make sure each student can take up to 10 courseprojects, unless the student is a senior (level >=4)^ CREATE ASSERTION uplimitCourse

CHECK ( (SELECT MAX COUNT (sid)

FROM student, enrollmentWHERE level < 4GROUP BY sid) <= 10

4/1/^

Luke Huan Univ. of Kansas Trigger example

CREATE TRIGGER EECS168AutoRecruitAFTER INSERT ON StudentREFERENCING NEW ROW AS newStudentFOR EACH ROWWHEN (newStudent.isHonor)INSERT INTO Enroll

VALUES(newStudent.SID, ’BIO101’);

Event

Condition

Action

4/1/^

Luke Huan Univ. of Kansas Why use views?

z^ To hide data from users z^ To hide complexity from users z^ Logical data independence^ z^

If applications deal with views, we can change theunderlying schema without affecting applications z Recall physical data independence: change the physicalorganization of data without affecting applications

z^ To provide a uniform interface for differentimplementations or sources )^ Real database applications use tons of views

4/1/^

Luke Huan Univ. of Kansas

Creating and dropping views

z^ Example: EECS647roster^ z^

CREATE

VIEW

EECS647Roster

AS

SELECT

SID,

name,

age,

GPA

FROM

Student WHERE

SID

IN^

(SELECT

SID

FROM

Enroll

WHERE

CID

=^ ’EECS647’);

z^ To drop a view^ z^

DROP

VIEW

view_name

Called “base tables”

4/1/^

Luke Huan Univ. of Kansas Modifying views

z^ Does not seem to make sense since views are virtual z^ But does make sense if that is how users see thedatabase z^ Goal: modify the

base tables

such that the modification

would appear to have been accomplished on the view z Be careful! z^ There may be one way to modify z^ There may be many ways to modify z^ There may be no way to modify

4/1/^

Luke Huan Univ. of Kansas A simple case

CREATE VIEW StudentGPA ASSELECT SID, GPA FROM Student;DELETE FROM StudentGPA WHERE SID = 123; translates to: DELETE FROM Student WHERE SID = 123;