Professional Documents
Culture Documents
The Relational Model and Relational Algebra: - E.Hema B.E.,M.Tech., Anna University Tiruchirappalli
The Relational Model and Relational Algebra: - E.Hema B.E.,M.Tech., Anna University Tiruchirappalli
-E.Hema B.E.,M.Tech.,
Anna University Tiruchirappalli
The relational model
Data structures
Integrity rules
Data Manipulation 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
Relational Database Model
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
Terminology
Relational Data Model
DATA STRUCTURES - domain,
attribute, relation, tuple, primary key,
degree, cardinality
INTEGRITY CONSTRAINTS - entity
integrity and referential integrity
DATA MANIPULATION OPERATIONS -
defined through relational algebra and
equivalent relational calculus.
Structures
Primary key
A unique identifier of a row in a relation
Key attribute
Any attribute that is part of a key
Superkey
A set of attributes that uniquely identifies each row
in a relation
Candidate key
Any set of attributes that could be chosen as a key of
relation
Composite key
A key consisting more than one attribute
Key
If K of R is a superkey for R, then the
relation r(R) does not have two tuples with
the same value. So if t1 and t2 are in r t1
not= t2
Find Candidate key
R(A, B, C, D)
1 2 3 4
1 1 3 4
3 2 1 4
4 1 2 3
{A,C,D} {B, C, D}
Integrity rules
Entity integrity
No component of the primary key of a
relation can be null
Each row in a relation is uniquely identified
Row in worker represents..
Row in Building represents..
Referential integrity
A database must not contain any
unmatched foreign key values
For every foreign key there is a
corresponding primary key
Example…..
Table Level Integrity = Entity Integrity
Relationship Level Integrity = Referential Integrity
Worker(Worker-ID, Name, Hourly Rate, Skill-Type,
Supv-ID)
Foreign keys: Skill-Type REFERENCES Skill, Supv-ID
REFERENCES Worker
Skill(Skill-Type, Bonus Rate, Hours-Per-Week)
What is an “Algebra”
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.
What is Relational Algebra?
An algebra whose operands are
relations or variables that represent
relations.
Operators are designed to do the most
common things that we need to do with
relations in a database.
Procedural language for manipulating
relations
Operations
A A UNION B
B
X Y X Y
X Y x1 y1
x1 y1
x2 y2 x2 y2
x2 y2
x4 y4 x3 y3
x3 y3
x4 y4
Intersect
Creates a new relation containing rows
appearing in both relations
Relations must be union compatible
A
B
X Y A INTERSECT
X Y B
x1 y1 x2 y2 X Y
x2 y2 x4 y4 x2 y2
x3 y3
Difference
B
A A MINUS B
X Y
X Y X Y
x2 y2
x1 y1 x1 y1
x4 y4
x2 y2 x3 y3
x3 y3
Product
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
Select
Uses a condition to select rows from a
relation
Comparison operators
Project
Extracts columns from a single relation
Creates a relation by deleting columns
from an existing relation
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
Creates new relation by selecting the rows in one relation that
matches every row in another relation
A
B
X Y
x1 y1 Y
x1 y2
y1
x1 y3
y2
x2 y1
x2 y3
A DIVIDE B
x1
A primitive set of operators
Only five operators are required
Select
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
condi 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