The Relational Model and Relational Algebra: Nothing Is So Practical As A Good Theory
The Relational Model and Relational Algebra: Nothing Is So Practical As A Good Theory
The Relational Model and Relational Algebra: Nothing Is So Practical As A Good Theory
Data structures
Integrity rules
Operators
Data structures
Domain
A set of values all of the same data type
All the legal values of an attribute
Defines what comparisons are legal
Only attributes from the same domain
should be compared
The domain
concept is rarely
implemented
Data structures
Relations
A table of n columns and m rows
A relation’s cardinality is its number of rows
A relation’s degrees is its number of columns
A relational database is a collection of
relations
No explicit linkages between tables
Cardinality is
easy to change
but not degrees
Structures
Primary key
A unique identifier of a row in a relation
Can be composite
Candidate key
An attribute that could be a primary key
Alternate key
A candidate key that is not selected as the primary key
Foreign key
An attribute of a relation that is the primary key of a relation
Can be composite
Integrity rules
Entity integrity
No component of the primary key of a
relation can be null
Each row in a relation is uniquely identified
Referential integrity
A database must not contain any
unmatched foreign key values
For every foreign key there is a
corresponding primary key
Operations
v1 w1 x1 y1 z1
v2 w2 x2 y2 z2
v3 w3
A TIMES B
V W X Y Z
v1 w1 x1 y1 z1
v1 w1 x2 y2 z2
v2 w2 x1 y1 z1
v2 w2 x2 y2 z2
v3 w3 x1 y1 z1
v3 w3 x2 y2 z2
Union
Creates a new relation containing rows appearing in
one or both relations
Duplicate rows are automatically eliminated
Relations must be union compatible
A A UNION B
X Y B X Y
x1 y1 X Y x1 y1
x2 y2 x2 y2 x2 y2
x3 y3 x4 y4 x3 y3
x4 y4
Intersect
Creates a new relation containing rows
appearing in both relations
Relations must be union compatible
A
X Y B
A INTERSECT B
x1 y1 X Y
x2 y2 x2 y2 X Y
x3 y3 x4 y4 x2 y2
Difference
A B A MINUS B
X Y X Y X Y
x1 y1 x2 y2 x1 y1
x2 y2 x4 y4 x3 y3
x3 y3
Join
Creates a new relation from all combinations of
rows satisfying the join condition
A join B where W = Z
A B
V W X Y Z
v1 wz1 x1 y1 wz1
v2 wz2 x2 y2 wz3
v3 wz3
A EQUIJOIN B
V W X Y Z
v1 wz1 x1 y1 wz1
v3 wz3 x2 y2 wz3
Divide
Is there a value in the X column of A (e.g., x1) that has a value in
the Y column of A for every value of y in the Y column of B?
A B
X Y Y
x1 y1 y1
x1 y2 y2
x1 y3
x2 y1
x2 y3
A DIVIDE B
x1
A primitive set of operators
Only five operators are required
Restrict
Project
Product
Union
Difference
Relational algebra and SQL
Relational algebra is a standard for
judging a data retrieval language
Relational algebra SQL
Restrict A whe r e SELECT * FROM A
c ondi t i on WHERE c ondi t i on
Project A [ X] SELECT X FROM A
Product A t i me s B SELECT * FROM A, B
Union A uni on B SELECT * FROM A UNI ON SELECT *
FROM B
Difference A mi nus B SELECT * FROM A
WHERE NOT EXI STS
( SELECT * FROM B WHERE
A. X = B. X AND A. Y = B. Y AND …) 1