Chapter 6 Relational Algebra

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Chapter 6

The Relational Data Model


and the Relational Algebra
The Relational Model Concepts
A Relational Model of Data for Large Shared Data Banks.
The concept derives from his principles of relational
algebra.
Most of the database systems in use today are based on
the relational system, known as Relational Database
Management Systems (RDBMS).
The relational model represents the database as a
collection of relations. Informally, each relation
resembles a table of values or, to some extent, a "flat" file
of records.
When a relation is thought of as a table of values, each
row in the table represents a collection of related data
values. We introduced entity types and relationship types
as concepts for modeling real-world data in Chapter 3.
In the relational model, each row in the table represents a fact
that typically corresponds to a real-world entity or
relationship. The table name and column names are used to
help in interpreting the meaning of the values in each row.
Relational Constraints And Relational Database
Schemas
Relational Constraints:- specify that the value of each
attribute A must be an atomic value from the domain dom
(A).
A relation is defined as a set of tuples. By definition, all
elements of a set are distinct; hence, all tuples in a
relation must also be distinct. This means that no two
tuples can have the same combination of values for all
their attributes. Usually, there are other subsets of
attributes of a relation schema R with the property that no
two tuples in any relation state r of R should have the
same combination of values for these attributes.
So far, we have discussed single relations and single relation
schemas. A relational database usually contains many
relations, with tuples in relations that are related in various
ways. In this section we define a relational database and a
relational database schema. A relational database schema S is a
set of relation schemas S = {R1, R2, . . ., Rm} and a set of
integrity constraints IC. A relational database state (Note 6)
DB of S is a set of relation states DB = {r1, r2, . . ., rm} such
that each ri is a state of Ri and such that the ri relation states
satisfy the integrity constraints specified in IC. Figure 07.05
shows a relational database schema that we call COMPANY =
{EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS,
PROJECT, WORKS_ON, DEPENDENT}.
Entity Integrity, Referential Integrity, and
Foreign Keys
The entity integrity constraint states that no primary key
value can be null. This is because the primary key value is
used to identify individual tuples in a relation; having null
values for the primary key implies that we cannot identify
some tuples.
For example, if two or more tuples had null for their primary
keys, we might not be able to distinguish them.
•Key constraints and entity integrity constraints
are specified on individual relations. The
referential integrity constraint is specified
between two relations and is used to maintain
the consistency among tuples of the two
relations. Informally, the referential integrity
constraint states that a tuple in one relation that
refers to another relation must refer to an
existing tuple in that relation.
To define referential integrity more formally, we first define
the concept of a foreign key. The conditions for a foreign key,
given below, specify a referential integrity constraint between
the two relation schemas R1 and R2. A set of attributes FK in
relation schema R1 is a foreign key of R1 that references
relation R2 if it satisfies the following two rules:
1. The attributes in FK have the same domain(s) as the
primary key attributes PK of R2; the attributes FK are said to
reference or refer to the relation R2.
2. A value of FK in a tuple t1 of the current state r1(R1) either
occurs as a value of PK for some tuple t2 in the current state
r2(R2) or is null. In the former case, we have t1[FK] = t2[PK],
and we say that the tuple t1 references or refers to the tuple
t2. R1 is called the referencing relation and R2 is the
referenced relation.
Relational Operations
It is straightforward to specify many operations of the
relational algebra in the form of Datalog rules that define the
result of applying these operations on the database relations
(fact predicates). This means that relational queries and views
can easily be specified in Datalog.
relational operations that were added to the basic relational
algebra because of their importance to many database
applications.
Relational Algebra
Relational Algebra is a procedural query language that
consists of a set of operations that take one or two relations
as input and produce a new relation as a result. The algebra
operations enable a user to retrieve specific request on a
relational model.
• The operations that produce a new relation can be further
manipulated using operations of the relation algebra.
• The sequence of the relational algebra that produces new
relation forms a relational algebra expression.
Fundamental Operations of Relational Algebra
• The core relational algebra that has traditionally
been thought of as the relational algebra consists of
the Fundamental operations that can be grouped
into two based on the number of relation operands
of the operator.
• These are: Binary Operators
Unary Operators. • Product (Cartesian Product)
• Selection (σ) (X)
• Projection (Π) • Union ( U )
Unary Operations
1. Select Operation
• The select operation selects a subset of tuples from a
relation instance that satisfies a given predicate (condition).
• It is denoted by
(R) C σ
Where σ represents the SELECT operator, C is a Boolean
expression of the select condition, and R is the relation or
relational algebra expression.
Example
• From the “EMPLOYEES” relation to extract Senior
Mangers, selection operation can be written as
Employees(EmpId, Name, BDate, Age, Gender, Position,
Salary)

2. Project Operation
While the select operation is picking certain rows from a
relation, projection operation forms a
Rename Operation
Unlike relations in the relational model the new relations
driven from the relational algebra expression do not have
name that will allow us to refer to them in other expressions.
The renaming operator can be used to explicitly rename
resulting relations of an expression.
It is denoted by:

Where ρ represents the RENAME operator and S is a name for


the new relation and A1,
A2, … An are new names for the attributes in the relation R.
After the renaming the name of the relation and the attributes
can be used as ordinary relation
and attributes in a sequence of relational algebra expressions:
Binary Operations
Cartesian Product Operation
The Cartesian product operation (also known as Cross Product
or Cross Join or Product) is binary set operation that generates
a new relation from two relation in a combinatorial fashion.
It is denoted by
RΧS
Where X represents the PRODUCT operator and R and S are
relations to be joined.
The product operation is just like the product operation in set
theory that maps each tuple in relation with every tuple in S.
Union Operation
The union operation on R and S denoted by R U S results a
relation that includes all tuples either in R or in S or in both.
Duplicates are eliminated from the result.
Intersection Operation
The intersection operation on R and S denoted by R S
results a relation that includes all tuples
in both R and S.
Set Difference Operation
The result of the set difference operation on R and S denoted
by R − S is the set of elements in R
but not in S.
For the set operations (Union, Intersection, Set difference)
the two relational operands R and S
must have same type of tuples, this condition is known as
Union
Compatibility.

You might also like