Basic Databases: Relational Model of Data
Basic Databases: Relational Model of Data
NGUYEN Hoang Ha
Email: [email protected]
Objectives
Understand concepts of
Data models
Relational data model
Structure
Operations
Constraints
2
What is a Data Model?
Mathematical representation of data.
Relational model = tables;
Semi-structured model = trees/graphs.
3
Data Models in history
4
Why Relation?
Very simple model.
Often matches how we think about data.
Abstract model that underlies SQL, the most important
database language today.
5
STRUCTURE OF RELATIONAL MODEL
Relational Model in brief
Attribute
Tuple
7
Attributes
Attribute
8
Domains
Tuple
10
Equivalent representation of a relation
Relation is a bag (extended set) of tuples, not a list of tuples
Order of tuples is not important
=
There could be identical tuples
Question:
What is/are the difference(s) between a set and a list?
What is/are the difference(s) between a set and a bag?
11
Relation instances
A B A B A B
1 2 1 3 3 5
3 4 2 5 4 6
13
Keys of relations
A set of attributes forms a key for a relation if we don’t
allow 2 tuples in a relation instance to have the same values
in all the attributes of the key
Eg: The key of relation
People(ID, name, address) is ID
14
Schemas
Relation schema:
Name
Set of attributes.
Order of attributes is arbitrary. In practice we assume the order given in
the relation schema
Other structure infor: keys…
Eg: Movies (Id, title, year, length, genre)
15
DB Schema about Movies
16
OPERATIONS OF RELATIONAL MODEL
Relational Algebra (RA)
Algebra is Mathematical system consisting of:
Operands --- variables or values from which new values can be
constructed.
Operators --- symbols denoting procedures that construct new values
from given values.
Relational Algebra: an offshoot of algebra of bags
Operands:
Variables that stand for relations
Constants, which are finite relations
18 18
Notations
19
Primitive operations
In any algebra, some operators are primitive and
the others, being definable in terms of the primitive
ones, are derived
The six primitive operators of relational algebra
are:
the SELECTION,
the PROJECTION,
the CARTESIAN PRODUCT (also called the cross product
or cross join),
the SET UNION,
the SET DIFFERENCE, and
the RENAME
20
Operations on Relations - Bag Union
Example: {1,2,1} U {1,1,2,3,1} = {1,1,1,1,1,2,2,3}
21
Operations on Relations - Bag Difference
Example: {1,2,1,1} – {1,2,3} = {1,1}.
22
Set Operations on Relations - Bag Intersection
This
23
Projection
R1 := π (R2)
L
24
Projection Example
Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
R= (A B) πA+B->C, A, A (R) = C A1 A2
1 2 3 1 1
3 4 7 3 3
26
Exercises - 1
Suppose relation R(A,B,C) has the tuples:
27
Selection
R1 := σC (R2)
▪ C is a condition (as in “if” statements) that
refers to attributes of R2.
▪ R1 is all those tuples of R2 that satisfy C.
28
Selection Example
Relation Sells:
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
JoeMenu := σbar=“Joe’s”(Sells):
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
29
Renaming
30
Renaming Example
R( bar, addr )
Joe’s Maple St.
Sue’s River Rd.
31
Product
R3 := R1 Χ R2
Pair each tuple t1 of R1 with each tuple t2 of R2.
Concatenation t1t2 is a tuple of R3.
Schema of R3 is the attributes of R1 and then R2, in order.
But beware attribute A of the same name in R1 and R2: use R1.A
and R2.A.
32
R3 = R1 Χ R2
33
R3 = R1 Χ R2
34
Join
Types
Theta join
Natural join
Outer join:
Left outer join
Right outer join
Full outer join
35
Theta-Join
R3 := R1 ⋈ C R2
Take the product R1 Χ R2.
36 36
Theta-Join Example
Denoted R3 := R1 ⋈ R2.
38
Eg: Natural Join
Sells( bar, beer, price ) Bars( bar, addr )
Joe’s Bud 2.50 Joe’s Maple St.
Joe’s Miller 2.75 Sue’s River Rd.
Sue’s Bud 2.50
Sue’s Coors 3.00
40
Outer join
41
Building Complex Expressions
42 42
Sequences of Assignments
Create temporary relation names.
Renaming can be implied by giving relations a list of
attributes.
R3 := σ C (R4)
43 43
Expressions in a Single Assignment
R3 := σ
C (R1 Χ R2)
2. [Χ, ⋈].
3. ∩.
4. [∪, —]
44 44
Expression Trees
Leaves are operands --- either variables standing for
relations or particular, constant relations.
Interior nodes are operators, applied to their child or
children.
45 45
Example: Tree for a Query
Using the relations Bars(name, addr) and Sells(bar, beer,
price), find the names of all the bars that are either on Maple
St. or sell Bud for less than $3.
46 46
As a Tree:
∪
ρ R(name)
π name π bar
Bars Sells 47
47
Example: Self-Join
Using Sells(bar, beer, price), find the bars that sell two
different beers at the same price.
Strategy: by renaming, define a copy of Sells, called S(bar,
beer1, price). The natural join of Sells and S consists of
quadruples (bar, beer, beer1, price) such that the bar sells
both beers at this price.
48 48
The Tree
π bar
σ
beer != beer1
⋈
ρ S(bar, beer1, price)
Sells Sells
49
49
CONSTRAINS IN RELATIONAL MODEL
Introduction
A constraint is a relationship among data elements that the
DBMS is required to enforce.
→ mechanism that may be used to limit the values entered
into a relation.
CONSTRAINTS
Attribute-based check
Tube-based check
51
Assertions
Key of a relation
52
Different key types of a relation
Super key
Set of attributes (columns) to uniquely identify rows
Primary key
One selected from candidate keys
Alternate key
Candidate key other than PK
Foreign key
Attribute refers to a PK of another relation
53
Express Primary Key Constraints by RA
54
Example: Express Primary Key Constraints
56
Example: Express Foreign Key Constraints
❖∏Bar(Sells) - ∏Bar(Bars) = Φ
57
Express Check Constraints
Give a schema:
EMPLOYEE(EmpId, Name, Sex)
58
Exercises 1
Give a schema:
PC (Model, Speed, RAM, HDD, Price)
59
Exercises 2
Give a schema:
LAPTOP (Model, speed, RAM, HDD, Screen, Price)
A laptop with a screen size less than 15.4 inches must have
at least a 120GB hard disk or sell for less than $1000
60
Exercises 3
If a laptop has a larger RAM than a PC, then the laptop must
also have a higher price than the PC
61
Exercises 4
PC (Maker, Model)
PRINTER (Maker, Model)
62
Exercises 5
63
Exercises 6
64