Relational Algebra
Relational Algebra
By Vijay Trivedi
VIT Bhopal
Outline
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• The Relational Algebra
Example of a Instructor Relation
attributes
(or columns)
tuples
(or rows)
Relation Schema and Instance
• A1, A2, …, An are attributes
• R = (A1, A2, …, An ) is a relation schema
• Example:
• instructor = (ID, name, dept_name, salary)
• A relation instance r defined over schema R is denoted by r (R).
• The current values a relation are specified by a table
• An element t of relation r is called a tuple and is represented by a
row in a table
Attributes
• The set of allowed values for each attribute is called the domain of
the attribute
• Attribute values are (normally) required to be atomic; that is,
indivisible
• The special value null is a member of every domain. Indicated that
the value is “unknown”
• The null value causes complications in the definition of many
operations
Relations are Unordered
• Order of tuples is irrelevant (tuples may be stored in an arbitrary
order)
• Example: instructor relation with unordered tuples
Database Schema
• Database schema -- is the logical structure of the database.
• Database instance -- is a snapshot of the data in the database at a
given instant in time.
• Example:
• schema: instructor (ID, name, dept_name, salary)
• Instance:
Keys
• Let K R
• K is a superkey of R if values for K are sufficient to identify a unique
tuple of each possible relation r(R)
• Example: {ID} and {ID,name} are both superkeys of instructor.
• Superkey K is a candidate key if K is minimal
Example: {ID} is a candidate key for Instructor
• One of the candidate keys is selected to be the primary key.
• Which one?
• Foreign key constraint: Value in one relation must appear in another
• Referencing relation
• Referenced relation
• Example: dept_name in instructor is a foreign key from instructor
referencing department
Schema Diagram for University Database
Relational Query Languages
• Procedural versus non-procedural, or declarative
• “Pure” languages:
• Relational algebra
• Tuple relational calculus
• Domain relational calculus
• The above 3 pure languages are equivalent in computing power
• We will concentrate in this chapter on relational algebra
• Not Turing-machine equivalent
• Consists of 6 basic operations
Relational Algebra
• A procedural language consisting of a set of operations that take
one or two relations as input and produce a new relation as their
result.
• Six basic operators
• select:
• project:
• union:
• set difference: –
• Cartesian product: x
• rename:
Select Operation
• The select operation selects tuples that satisfy a given predicate.
• Notation: p (r)
• p is called the selection predicate
• Example: select those tuples of the instructor relation where the
instructor is in the “Physics” department.
• Query
•
dept_name=“Physics” (instructor)
•
• Result
Select Operation (Cont.)
• We allow comparisons using
• =, , >, . <.
• in the selection predicate.
• We can combine several predicates into a larger predicate by using
the connectives:
• (and), (or), (not)
• Example: Find the instructors in Physics with a salary greater $90,000,
we write:
•
dept_name=“Physics” salary > 90,000 (instructor)
•
• The select predicate may include comparisons between two
attributes.
• Example, find all departments whose name is the same as their building
name:
• dept_name=building (department)
Project Operation
• A unary operation that returns its argument relation, with certain
attributes left out.
• Notation:
• A1,A2,A3 ….Ak (r)
• where A1, A2, …, Ak are attribute names and r is a relation
name.
• The result is defined as the relation of k columns obtained by erasing
the columns that are not listed
• Duplicate rows removed from result, since relations are sets
Project Operation Example
• Example: eliminate the dept_name attribute of instructor
• Query:
r s: A r
B is branch-name
1and 2 here show two specific branch-
names
(Find customers who have an account in all
branches of the bank)
Another Division Example
Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
e.g.
r s: Students who have taken both
A B C "a” and “b” courses, with
instructor “1”
a
(Find students who have taken
a all courses given by instructor 1)
How Division Works
• Example of writing division with set difference, projection,
and assignments: r s
temp1 R-S (r )
temp2 R-S ((temp1 x s ) – R-S,S (r ))
result = temp1 – temp2
* Try executing the above query at home on the previous example, to convince
yourself about its equivalence to the division operation
Equivalent Queries
• There is more than one way to write a query in relational algebra.
• Example: Find information about courses taught by instructors in the
Physics department with salary greater than 90,000
• Query 1
• dept_name=“Physics” salary > 90,000 (instructor)
•
• Query 2
• dept_name=“Physics” ( salary > 90.000 (instructor))
•
• The two queries are not identical; they are, however, equivalent --
they give the same result on any database.
Equivalent Queries
Thank you