2 Relation
2 Relation
2 Relation
Relation 关系
A two-dimensional table called a relation.
Why Relations?
Very simple model.
Often matches how we think
about data.
Abstract model that
underlies SQL, the most
important database language
today.
2.2.1 Attributes
Attribute 属性
Names for the columns of the relation,
describe the meaning of entries in the
column below. Such as length of
Movies.
An attribute have a name.
Any two attributes of a relation can’t
have same name.
title year length filmType
Attributes
1977 124 color
(column Star Wars
1992 95 color
headers) Wayne’s World
Mighty Ducks 1991 104 color
… … … …
2.2.2 Schemas
关系模式
Tuples 元组
The rows of a relation, other than
the header row containing the
attributes, are called tuples.
There may be no tuple in a relation.
分量
A tuple has one component for each
attribute of the relation.
Domains 域
A domain is an elementary type, such
as integer, char(n), date, time.
Each attribute of a relation is a
domain, that is, a particular elementary
type.
Each component of any tuple must be
atomic.
Movies ( title: string, year: int, length:
int, filmtype: string)
Can not be broken into
smaller components
2.2.5 Equivalent Representations of a Relation
| [CONSTRAINT [constraint_name]]
FOREIGN KEY index_name (index_col_name, ...)
REFERENCES another_table_name (index_col_name, ...)
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE { RESTRICT | CASCADE | SET NULL | NO ACTION } ]
[ ON UPDATE { RESTRICT | CASCADE | SET NULL | NO ACTION } ]
);
2.3.5 Default Values 缺省值
Conditions on R and S:
R and S must have schemas with
identical sets of attributes.
Before we compute the set-
theoretic operations, the columns
of R and S must be ordered so that
the order of attributes is the same
for both relations.
2.4.4 Set Operations on Relations
R S
A B A B
a1 b1 a1 b1
a1 b2 a1 b3
Projection 投影 :
Produce from a relation R1 a new
relation that has only some of R2’s
columns.
Denotation: R1:=∏A1,A2,…,An (R2)
A1,A2,…,An is a list of attributes
from the schema of R2.
R1 is constructed by looking at each
tuple of R2, extracting the attributes
on list A1,A2,…,An, in the order
specified, and creating from those
components a tuple for R1.
Eliminate duplicate tuples, if any.
2.4.5 Projection
Example: Movie
title year lengt inColo studioNa ProducerC#
h r me
Star Wars 1977 124 True Fox 12345
Mighty Ducks 1991 104 True Disney 67890
Wayne’s 1992 95 True Paramount 99999
World
πtitle,year,length(Movie) πinColor(Movie)
Selection 选择 :
Produce from a relation R a new
relation with a subset of R’s tuple.
Denotation: σC(R)
Result: The schema for the resulting
relation is the same as R’s schema, and
the tuples in the resulting relation are
those that satisfy some condition C
that involves the attributes of R.
Expression of condition C:
Operands: constants or attributes of R
Operators: = ≠ > ≥ < ≤ NOT ˄(AND)
˅(OR)
2.4.6 Selection
Example: Movie
title year lengt inColo studioNam ProducerC
h r e #
Star Wars 1977 124 True Fox 12345
Mighty Ducks 1991 104 True Disney 67890
Wayne’s 1992 95 True Paramount 99999
World
σlength ≥ 100(Movie):
title year lengt inColo studioNam ProducerC
h r e #
Star Wars 1977 124 True Fox 12345
Mighty 1991 104 True Disney 67890
σlength ≥ 100 ˄ studioName =‘FOX’(Movie):
Ducks
Result:
The relation schema for the
resulting relation is the attributes
of R and then S, in order.
To disambiguate an attribute A
that is in the schemas of both R
and S, we use R.A for the attribute
from R and S.A for the attribute
from S.
The tuples in the resulting
relation are pairs the tuples of R
and S in all possible ways.
The number of tuples is NR×NS.
2.4.7 Cartesian Product
Example: R S
A B B C D
1 2 2 5 6
3 4 4 7 8
9 10 11
R×S ?
A R.B S.B C D
1 2 2 5 6
1 2 4 7 8
1 2 9 10 11
3 4 2 5 6
3 4 4 7 8
3 4 9 10 11
2.4.8 Natural Joins
Example: R S
A E B C D
1 2 2 5 6
3 4 4 7 8
9 10 11
R S
Example: U V
A B C B C D
1 2 3 2 3 4
6 7 8 2 3 5
9 7 9 7 8 10
U V
A B C D A tuple that fails to pair
1 2 3 4 with any tuple of the other
1 2 3 5 relation in join is sometimes
6 7 8 10
said to be a dangling tuple.
2.4.9 Theta-Joins
Theta-join:
Pair tuples using an arbitrary
condition.
Denotation: R C S
Result:
Take the product of R and S.
Select from the product only
those tuples that satisfy the
condition C.
2.4.9 Theta-Joins
Example: U V
A B C B C D
1 2 3 2 3 4
6 7 8 2 3 5
9 7 9 7 8 10
U A<D
V
A U.B U.C V.B V.C D
1 2 3 2 3 4
1 2 3 2 3 5
1 2 3 7 8 10
6 7 8 7 8 10
9 7 9 7 8 10
2.4.10 Combining Operations to Form Queries
Combining operations:
Form expressions of arbitrary
complexity by applying operators
either to given relations or to
relations that are the result of
applying one or more relational
operators to relations.
Use parentheses when necessary
to indicate grouping of operands.
2.4.10 Combining Operations to Form Queries
Suppose the values of relation R and relation S
are as follows :
R A B C S B C D
a1 b1 c1 b2 c1 d1
a2 b2 c2 b2 c2 d2
a3 b3 c3 b2 c2 d3
Renaming 改名
Denotation: ρS (R)
(A1, A2,…,An)
Example: R S
A B B C D
1 2 2 5 6
3 4 4 7 8
9 10 11
1 . Find the IDs and phone numbers of all salesmen named “Zhangsan”.
2 . Find the names and phone numbers of all customers who have transacted
with the salesman whose ID is “A0043”.
3 . Find the types and specifications of all merchandises which are sold by
the salesman whose ID is “A0043”.
2.5 Constraints on Relations
π (R) - π (S)=
A B
Example:
π custid (Salesorder) πcustid(Customer)
2.5.3 Key constraints
Example:
Salesman (empid, idno, name,
gender, phone, deptid)
S1:=S1(Salesman)
S2:= S2 (Salesman)
S1.empid=S2.empid ˄ S1.nameS2.name (S1S2) =
2.5.4 Additional Constraint Examples
Domain constraints:
The only legal values for the
gender attribute of Salesman are
0(female) and 1(male).
σgender 0 ˄ gender 1(Salesman) = Φ