chapter 2 - 1 abstraction concrete: directly executable/storable abstract: not directly...

37
Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable automatic translation (as good as executable/storable) systematic translation ad hoc translation not translatable (incomplete or unclear)

Post on 19-Dec-2015

237 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 1

Abstraction

• Concrete: directly executable/storable• Abstract: not directly executable/storable

– automatic translation (as good as executable/storable)– systematic translation– ad hoc translation– not translatable (incomplete or unclear)

Page 2: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 2

The 3-Level Architecture

view 2 view nview 1

Conceptual Database

PhysicalDatabase

Abstract

Concrete ImplementationIndependence

Page 3: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 3

Abstract Data Type (ADT)

• Abstract Description of a Data Type• (Set of Values, Set of Operations)• Examples:

– Integer = ({0, -1, 1, -2, 2, …}, {+, -, *, /})– Stack = (all possible stacks, {push, pop, top, empty})– Database = (all possible DB states, {insert, remove, retrieve})

• Description of all possible DB States:– Description of Conceptual Database– Scheme, Schema, Model

Page 4: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 4

Entity-Relationship (ER) Model

• Entity -- object

• Relationship -- connection among objects

• Attribute -- properties of objects and connections

Page 5: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 5

ER Application Model

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

Page 6: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 6

ISA – Generalization/Specialization

RoomUnderConstruction

CompletionDate

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

ISA

RoomUnderConstruction

CompletionDate

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

ISA

Page 7: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 7

Cardinality Relationships

1-1

m-1; 1-m seen the other way

m-n

Page 8: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 8

Entity Key

C

B

A

C

B

A

Uniquely Identifies an Individual Entity

Keys: A or BC

Page 9: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 9

Relationship Key

B A

B A

B A

B A

B A

B A

Uniquely Identifies an Individual Relationship

Key: A or B

Key: A

Key: AB

Page 10: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 10

Key Attributes for Relationships

C

B A

C

B A

Relationship attributes are sometimes needed touniquely Identify an individual relationship.

Depending on the semantics, thekey could be: ABC, AC, BC, or C.

Page 11: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 11

Keys

• Superkey: A set of attributes that uniquely identifies

• Minimal Key: A minimal superkey.

• Candidate Key: Same as minimal key.

• Primary Key: A chosen candidate key.

Page 12: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 12

Scheme Generation• Generate a scheme for each entity set (except specializations)

– The attributes are the attributes of the entity set.– The keys are the candidate keys of the entity set; choose a primary key.

• For each relationship set, adjust or add a scheme.– 1-1: merge schemes; add any relationship attributes.– 1-m, m-1: to the scheme on the many-side, add the primary-key attribute(s) of the one-

side; add any relationship attributes.– m-m: make a new scheme from the primary-key attributes of the connected entity sets and

any relationship attributes; the key is the composite of the primary-key attributes if there are no key relationship attributes—otherwise semantically determine the key.

• Generate a scheme for each specialization.– The attributes are the attributes of the specialization plus the primary key attribute(s) of the

generalization.– The key is the primary key of the generalization.

Page 13: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 13

Scheme Generation – Example

RoomUnderConstruction

CompletionDate

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

ISA

RoomUnderConstruction

CompletionDate

Room

NrDays

ArrivalDate

Guest

City

StreetNr

Name

GuestNrRoomNr

Name

NrBeds

Cost

hasreservation for

ISA

Generated schemes, with candidate keys underlined and primary keysdouble underlined: Room(RoomNr, Name, NrBeds, Cost) Guest(GuestNr, Name, StreetNr, City) Reservation(GuestNr, RoomNr, ArrivalDate, NrDays) RoomUnderConstruction(RoomNr, CompletionDate)

Page 14: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 14

SQL DDLcreate table Room( RoomNr integer primary key, Name char(20) unique, NrBeds integer, Cost float );

create table Guest( GuestNr integer primary key, Name char(20), StreetNr char(20), City char(15), unique (Name, StreetNr, City) );

create table Reservation( GuestNr integer references Guest, RoomNr integer references Room, ArrivalDate char(6), NrDays integer, primary key (RoomNr, ArrivalDate) );

Page 15: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 15

SQL DDLcreate table Room( RoomNr integer primary key, Name char(20) unique, NrBeds integer, Cost float );

create table Guest( GuestNr integer unique, Name char(20), StreetNr char(20), City char(15), primary key (Name, StreetNr, City) );

create table Reservation( Name char(20), StreetNr char(20), City char(15), RoomNr integer references Room, ArrivalDate char(6), NrDays integer, primary key (RoomNr, ArrivalDate), foreign key (Name, StreetNr, City) references Guest );

Page 16: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 16

Sample Database Instance

r = Room(RoomNr Name NrBeds Cost) ------------------------------------------ 1 Kennedy 2 90 2 Nixon 2 80 3 Carter 2 80 4 Blue 1 60 5 Green 1 50

g = Guest(GuestNr Name StreetNr City) --------------------------------------------------- 101 Smith 12 Maple Boston 102 Carter 10 Main Hartford 103 Jones 6 Elm Hartford 104 Smith 4 Oak Providence 105 Green 10 Main Boston 106 Johnson 15 Main Boston

s = Reservation(GuestNr RoomNr ArrivalDate NrDays) ------------------------------------------------------ 101 1 10 May 2 101 2 20 May 1 101 3 15 May 2 102 3 10 May 5 103 1 12 May 3 104 4 10 May 2 104 4 17 May 2 104 4 24 May 2 105 1 15 May 7 106 2 11 May 2

Page 17: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 17

Relational Algebra

• An Algebra is a Pair: (set of values, set of operations)• Note that an Algebra is the same idea as an ADT• Relational Algebra: (relations, relational operators)

– set of values = relations– set of operations = relational operators

• Relational Operators– update operators:

• insert a tuple

• delete one or more tuples

• modify one or more tuples

– retrieval operators: {, , , , , , , }

Page 18: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 18

– Selection

General Form: <condition> <relation>

Examples:

Cost > 75 r

ArrivalDate = 10 May NrDays > 2 s

RoomNr Name NrBeds Cost--------------------------------------- 1 Kennedy 2 90 2 Nixon 2 80 3 Carter 2 80

GuestNr RoomNr ArrivalDate NrDays---------------------------------------------------- 102 3 10 May 5

Page 19: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 19

– Projection

General Form: <attributes> <relation>

Examples:

City g GuestNr,RoomNr s

City------BostonHartfordProvidence

GuestNr RoomNr------------------------ 101 1 101 2 101 3 102 3 103 1 104 4 105 1 106 2

Page 20: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 20

Closed Set of Operators

• Results are relations• Closed implies we can nest operations in expressions• Example:

GuestNr,RoomNr ArrivalDate = 10 May s

GuestNr RoomNr------------------------ 101 1 102 3 104 4

Page 21: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 21

Set Operators: , ,

Name--------CarterGreen

RoomNr----------- 5

RoomNr----------- 4 5 1 3

Name r Name g

RoomNr r RoomNr s

RoomNr Cost < 75 r RoomNr ArrivalDate = 10 May s

Note: schemes must be compatible.

Page 22: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 22

– Renaming

General Form: <old attribute> <new attribute> <relation>

Examples:

RoomName Name RoomName Cost < 75 r

Nr, Name RoomNr Nr r Nr, Name GuestNr Nr g

Note: The old attribute must be in the scheme and thenew attribute must not be in the scheme.

RoomName---------------- Blue Green

Nr Name--------------

Page 23: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 23

– Cross Product

NrBeds r RoomNr s

NrBeds---------- 2 1

RoomNr----------- 1 2 3 4

NrBeds RoomNr----------------------- 2 1 2 2 2 3 2 4 1 1 1 2 1 3 1 4

=

Note: The intersection of the schemes must be empty.

Page 24: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 24

– Natural Join

r g =RoomNr,Name,NrBeds,Cost,GuestNr,StreetNr,City Name = Name

(r Name Name g)

RoomNr Name NrBeds Cost GuestNr StreetNr City------------------------------------------------------------------------------- 3 Carter 2 80 102 10 Main Hartford 5 Green 1 50 105 10 Main Boston

Page 25: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 25

Natural Join – Examples

A B------1 23 24 56 7

B C------1 22 35 75 8

A B C----------1 2 33 2 34 5 74 5 8

A B------1 23 4

C D------1 32 4

A B C D--------------1 2 1 31 2 2 43 4 1 33 4 2 4

A B C----------1 2 32 2 34 5 6

B C D----------2 3 45 6 72 6 0

A B C D--------------1 2 3 42 2 3 44 5 6 7

=

=

=

Page 26: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 26

Query ExamplesList names and cities of guests arriving on 15 May.

Name,City ArrivalDate = 15 May (g s)

List names of each guest who has a reservation for a roomthat has the same name as the guest’s name.

Name (g s r)

List names of guests who have a reservation for roomswith two beds.

Name (g s RoomNr NrBeds = 2 r)

Page 27: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 27

More Query Examples

List the names of guests from Hartford who are arrivingafter 10 May.

Name (GuestNr,Name City = Hartford g GuestNr ArrivalDate > 10 May s)

List names of rooms for which no guest is arriving on 10 May.

Name (r (RoomNr r - RoomNr ArrivalDate = 10 May s))

Page 28: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 28

SQLCorrespondence with Relational Algebra

select Afrom rwhere B = 1

Assume r(AB) and s(BC).

select B from rminusselect B from s

select A as “D” from r

select A, r.B, Cfrom r, swhere r.B = s.B

A B = 1 r

B r - B s

DA D r

A, r.B, C r.B = s.B (r s)

Page 29: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 29

SQL: Basic Retrieval

Get full details of rooms.

select RoomNr, Name, NrBeds, Costfrom Room

select * from Room

Get costs.

select Costfrom Room

select distinct Costfrom Room

COST-------- 90 80 80 60 50

COST-------- 50 60 80 90

Page 30: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 30

SQL: Aggregate Operations

Get average cost.

select avg(Cost)from Room

select avg(Cost) as “AVE$”from Room

AVE$-------72.00

-------72.00

AVG(COST)----------------- 72.00

Also: min, max, sum, count.

Page 31: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 31

SQL: Sorting

Get distinct costs in descending order.

select distinct Costfrom Roomorder by Cost desc

Get cost and number of beds, sorted with beds inascending order and cost in descending order.

select distinct NrBeds, Costfrom Roomorder by NrBeds asc, Cost desc

COST-------- 90 80 60 50

NRBEDS COST---------------------- 1 60 1 50 2 90 2 80

Page 32: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 32

SQL: Grouping

Get the average cost of rooms with the same number of beds.

select NrBeds, avg(Cost)from Roomgroup by NrBeds

Get guest numbers of guests who have reservations for atotal of 5 or more days.

select GuestNrfrom Reservationgroup by GuestNrhaving sum(NrDays) >= 5

NRBEDS AVG(COST)-------------------------------- 1 55.00 2 83.33

GUESTNR--------------- 101 102 104 105

Page 33: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 33

SQL: Nested Queries

Get Names and Addresses of guests who have reservationsfor a total of 5 or more days.

select Name, StreetNr, Cityfrom Guestwhere GuestNr in ( select GuestNr from Reservation group by GuestNr having sum(NrDays) >= 5)

NAME STREETNR CITY-------------------------------------Smith 12 Maple BostonCarter 10 Main HartfordSmith 4 Oak ProvidenceGreen 10 Main Boston

Page 34: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 34

SQL: Multiple-Relation Retrieval

Get names of guests whose names match room names.

select Guest.Namefrom Guest, Roomwhere Guest.Name = Room.Name

Get names of each guest who has a reservation for a roomthat has the same name as the guest’s name.

select g.Namefrom Guest g, Room r, Reservation swhere g.Name = r.Name and g.GuestNr = s.GuestNr and s.RoomNr = r.RoomNr

NAME---------CarterGreen

NAME---------Carter

Page 35: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 35

SQL: Select/Project/Join Queries

Get names and addresses of guests arriving on 15 May.

select distinct Name, StreetNr, Cityfrom Guest g, Reservation swhere g.GuestNr = s.GuestNr and ArrivalDate != “15 May”

… arriving on a date other than 15 May.

select distinct Name, StreetNr, Cityfrom Guest g, Reservation swhere g.GuestNr = s.GuestNr and ArrivalDate = “15 May”

NAME STREETNR CITY-------------------------------------Smith 12 Maple BostonGreen 10 Main Boston

NAME STREETNR CITY-------------------------------------Smith 12 Maple BostonCarter 10 Main HartfordJones 6 Elm HartfordSmith 4 Oak ProvidenceJohnson 15 Main Boston

Page 36: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 36

SQL: Negation Get names and addresses of guests not arriving on 15 May.

select Name, StreetNr, Cityfrom Guest minusselect Name, StreetNr, Cityfrom Guest g, Reservation swhere g.GuestNr = s.GuestNr and ArrivalDate = “15 May”

select Name, StreetNr, Cityfrom Guestwhere GuestNr not in ( select GuestNr from Reservation where ArrivalDate = “15 May”)

NAME STREETNR CITY-------------------------------------Carter 10 Main HartfordJones 6 Elm HartfordSmith 4 Oak ProvidenceJohnson 15 Main Boston

Page 37: Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)

Chapter 2 - 37

SQL: Joining a Relation with ItselfGet pairs of guest numbers of guests arriving on the samedate. (The pairs should be unique.)

select s.GuestNr, t.GuestNrfrom Reservation s, Reservation twhere s.ArrivalDate = t.ArrivalDate and s.GuestNr < t.GuestNr

GUESTNR GUESTNR-------------------------------- 101 102 101 104 102 104 101 105