Foundations of Relational Implementation (2) IS 240 – Database Management Lecture #14 –...

Post on 26-Mar-2015

218 views 4 download

Tags:

transcript

Foundations of Relational

Implementation (2)

IS 240 – Database ManagementLecture #14 – 2004-04-06

Prof. M. E. Kabay, PhD, CISSP

Norwich University

mkabay@norwich.edu

2 Copyright © 2004 M. E. Kabay. All rights reserved.

Topics

Today we will work on relational algebraApplying operators to relationsExpressing queries as relational algebra

The RelationsDoctor WaterburyStateDoctor Name Speciality Doctor Ext

111 Smith Neurology 111 3415243 Ben Avram Internal 243 2874516 Korovky Anesthesiology

Patient CentralVermontID LastName FirstName Birthday Physician Phone

010-31-2243 Watkins Barbara 1973-05-12 111 823468-77-1119 Chatsworth Harrison 1981-12-01 516 851222-37-2220 Salamanthos Tsoukas 1957-07-08438-51-8735 Davidel Aaron 1968-04-24300-93-4876 Farzi Ahmed 1976-11-13

PatientDoctorPatient Hospital MedicalStaff Bed

222-37-2220 WaterburyState 111 A5222-37-2220 WaterburyState 243 B12222-37-2220 CentralVermont 111 1740A438-51-8735 CentralVermont 516 1620B300-93-4876 WaterburyState 111 A9010-31-2243 CentralVermont 823 1635A

4 Copyright © 2004 M. E. Kabay. All rights reserved.

The Operators

Union ( + or )Difference ( - ) Intersection () [Cartesian] Product ( X )Projection ( Relation[attributes] )Selection ( WHERE )JOIN

[ Relation1 JOIN (condition) Relation2]

5 Copyright © 2004 M. E. Kabay. All rights reserved.

Union ( + or )

Combine rows from one table to those of another to form a third, combined table

Discard duplicatesTables (relations) must be union-compatible

Structure must be the sameSame number of fields (attributes)Same domain for each corresponding field

6 Copyright © 2004 M. E. Kabay. All rights reserved.

Union ( + or )

REFER TO SLIDE 3 FOR THE RELATIONSWhich relations are union-compatible? Why?

What normal English question does the union of these relations answer?

Write out the tuples of A+B or AB where A and B are the relations that are union-compatible.

7 Copyright © 2004 M. E. Kabay. All rights reserved.

Difference ( - )

Locate the rows that are in the first table that are NOT in the second table

Create a table based on those remaining records

Difference, unlike union, is not transitiveI.e., the order of the operands mattersThus X – Y is not necessarily the same as

Y - X

8 Copyright © 2004 M. E. Kabay. All rights reserved.

1 2

2 3

3 4

A B

9 Copyright © 2004 M. E. Kabay. All rights reserved.

Difference ( - )

What is WaterburyState – CentralVermont?

What does this difference mean in English?

What is CentralVermont – WaterburyState?

What does this difference mean in English?

10 Copyright © 2004 M. E. Kabay. All rights reserved.

Intersection ()

Find the rows in the tables that occur in bothPut those in a third (new) table

11 Copyright © 2004 M. E. Kabay. All rights reserved.

Intersection ( )

What is WaterburyState CentralVermont?

What does this intersection mean in English?

12 Copyright © 2004 M. E. Kabay. All rights reserved.

[Cartesian] Product ( X )

Concatenate* (join) each row from one table with every row of the second table

May produce nonsensical rowsUsually have to extract meaningful data with

other operators

* Etymology from Latin:

• Con = with

• Catena = chain

13 Copyright © 2004 M. E. Kabay. All rights reserved.

[Cartesian] Product ( X )

Does the Cartesian product of two relations necessarily make sense by itself?

What is Doctor X WaterburyState?

14 Copyright © 2004 M. E. Kabay. All rights reserved.

Projection (Relation[attributes])

Extract certain columns from the rows in a table

Thus result is table with fewer columns (attributes)

Remove duplicate records that may result

15 Copyright © 2004 M. E. Kabay. All rights reserved.

Projection ( Relation[attributes] )

What is the projection Patient[LastName,Birthday]? Express this projection as a question.

What is the projection PatientDoctor[Patient,Hospital,Bed]? Express this projection as a question.

16 Copyright © 2004 M. E. Kabay. All rights reserved.

Selection ( WHERE )

Choose certain rows from a table according to specific conditions

Put the qualifying rows in a new table

17 Copyright © 2004 M. E. Kabay. All rights reserved.

Selection ( WHERE )

What is PatientDoctor WHERE Hospital = “CentralVermont”? Express this selection as a question in English.

What is Patient WHERE Birthday < “1980-01-01”? Express this selection as a question in English.

18 Copyright © 2004 M. E. Kabay. All rights reserved.

JOIN [Relation1 JOIN (condition) Relation2]

Complex operation involving product, selection and projection1st: create product table2nd: select appropriate rows from that table3rd: choose particular columns needed

Equijoin may include columns that have the same meaning

Natural join removes duplicate columns

19 Copyright © 2004 M. E. Kabay. All rights reserved.

JOIN [ Relation1 JOIN (condition) Relation2]

What is PatientDoctor JOIN (MedicalStaff = Doctor) Doctor? Use a natural join.

What kinds of questions could you answer by such a Join operation?

20 Copyright © 2004 M. E. Kabay. All rights reserved.

Homework

Complete your study of Chapter 8 using Read-Recite-Review phases of SQ3R

For Thursday 8 April 2004, use the Survey-Question phases of SQ3R in Chapter 9 on SQL

REQUIRED: For Tuesday 13 April 2004, complete written answers to questions 8.18-8.28 for 34 points

21 Copyright © 2004 M. E. Kabay. All rights reserved.

DISCUSSION