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)
37
Embed
Chapter 2 - 1 Abstraction Concrete: directly executable/storable Abstract: not directly executable/storable –automatic translation (as good as executable/storable)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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)
Chapter 2 - 2
The 3-Level Architecture
view 2 view nview 1
Conceptual Database
PhysicalDatabase
Abstract
Concrete ImplementationIndependence
Chapter 2 - 3
Abstract Data Type (ADT)
• Abstract Description of a Data Type• (Set of Values, Set of Operations)• Examples:
• Description of all possible DB States:– Description of Conceptual Database– Scheme, Schema, Model
Chapter 2 - 4
Entity-Relationship (ER) Model
• Entity -- object
• Relationship -- connection among objects
• Attribute -- properties of objects and connections
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
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
Chapter 2 - 7
Cardinality Relationships
1-1
m-1; 1-m seen the other way
m-n
Chapter 2 - 8
Entity Key
C
B
A
C
B
A
Uniquely Identifies an Individual Entity
Keys: A or BC
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
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.
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.
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.
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
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: {, , , , , , , }
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
Note: The intersection of the schemes must be empty.
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
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
=
=
=
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)
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))
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)
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
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.
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
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
Chapter 2 - 36
SQL: Negation Get names and addresses of guests not arriving on 15 May.