Relational Algebra and Relational Calculus July 2024
Relational Algebra and Relational Calculus July 2024
Example of a Relation
attributes
(or columns)
tuples
(or rows)
Attribute Types
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
The null value causes complications in the definition of many
operations
Relation Schema and Instance
• instructor
student
advisor
• Bad design:
univ (instructor -ID, name, dept_name, salary, student_Id, ..)
results in
• Repetition of information (e.g., two students have the same
instructor)
• The need for null values (e.g., represent an student with no
advisor)
Schema Diagram for University Database
Relational Query Languages
• Two mathematical Query Languages form the basis for “real”
languages (e.g. SQL), and for implementation:
• Relational Algebra: More operational (Procedural), very useful
for representing execution plans.
• Relational Calculus: Lets users describe what they want, rather
than how to compute it. (Non-operational, declarative.)
• Tuple relational calculus
• Domain relational calculus
Basic operations:
Selection ( ) Selects a subset of rows from relation.
Projection ( ) Deletes unwanted columns from relation.
Cross-product ( ) Allows us to combine two relations.
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
Union ( ) Tuples in reln. 1 and in reln. 2.
Rename (ρ)
Additional operations: Intersection, join, division
Selection
• Selects rows that satisfy selection condition.
• No duplicates in result!
• Schema of result is identical to schema of (only) input
relation.
• Result relation can be the input for another relational algebra
operation! (Operator composition.)
S2
sid sname rating age
28 yuppy 9 35.0
31
44
lubber
guppy
8
5
55.5
35.0
rating 8(S2)
58 rusty 10 35.0
sid sname rating age SQL:
Select * from S2 where rating > 8
O/P
28 yuppy 9 35.0
58 rusty 10 35.0
Selection of tuples
Relation r
SQL:
Select * from r where A=B and D > 5
Projection
• Deletes attributes that are not in projection list.
• Projection operator has to eliminate duplicates!
sname rating
yuppy 9
sname,rating(S2)
lubber 8
guppy 5 SQL:
rusty 10 Select sname, rating from S2
age
35.0
55.5
SQL:
Select age from S2
age(S2)
Selection of Columns (Attributes)
• Relation r:
• Select A and C
• Projection
• Π A, C (r)
Union, Intersection, Set-Difference
Example Instances
r s: r s
r – s:
Joining two relations – Cartesian Product
Relations r, s:
r x s:
Cross-Product
Each row of S1 is paired with each row of R1.
Result schema has one field per field of S1 and R1, with field names
`inherited’ if possible.
Conflict: Both S1 and R1 have a field called sid.
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96
Consider the following student and fee tables with common column names and data types;
Fees
SELECT s.Name, s.Contact, f.Amount, f.Paid FROM Student s NATURAL JOIN Fee f;
Output:
Natural Join
r s
(( Re serves) CSailors
S = C )(R S)
bid 103
Condition Join: R
S1) sid
Re serves Sailors
R1 )
103
Result schema similar to cross-product, but only one copy of fields
for which equality is specified.
Natural Join: Equijoin on all common fields.
Division
• The division operator is used for queries which involve the 'all'.
• R1 ÷ R2 = tuples of R1 associated with all tuples of R2.
Example
Retrieve the name of the
subject that is taught in all
courses.
Division
Division operator A÷B or A/B can be applied if and only if:
• The relation returned by division operator will return those tuples from
relation A which are associated to every B’s tuple.
Atomic formula:
R Rel, or R.a op S.b, or R.a op constant op is one of ,, ,,,
Formula:
an atomic formula, or
p, p q, p q , where p and q are formulas, or
X ( p( X)) , where variable X is free in p(X), or
X ( p( X)) , where variable X is free in p(X)
Free and Bound Variables
x , x , …, x )
2 3 n
Query-1: Find the loan number, branch, amount of loans of greater than or equal to
100 amount.
{≺l, b, a≻ | ≺l, b, a≻ ∈ loan ∧ (a ≥ 100)}
Unsafe Queries, Expressive Power
https://www.javatpoint.com/dbms-keys
https://www.gatevidyalay.com/keys-in-dbms/
Keys in DBMS
Super Key
A super key is a set of attributes that can identify each tuple uniquely
in the given relation.
Superkey of R:
• Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the
same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK]
Example-
Consider the following Student schema-
Student ( roll , name , gender , age , address , class , section )
Given below are the examples of super keys since each set can
uniquely identify each student in the Student table-
• ( roll , name , gender , age , address , class , section )
• ( class , section , roll ) and (class , section , roll , gender) etc
Candidate Key
Candidate Keys of R:
• Are minimal set of Super key
Example:
Composite Key in college_Info table.
{ Branch_Name, Branch_Location}
Composite Key in Student_Information table:
{ Student_Id, Student_Name }
Foreign Key
• Foreign key is used to generate the relationship between the tables.
• Foreign Key is a field in database table that is Primary key in
another table.
• The purpose of Foreign keys is to maintain data integrity.
• It acts as a cross-reference between two tables as it references the
primary key of another table.
• Foreign keys are the columns of a table that points to the candidate
key of another table.
Keys Summary
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} 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.
Foreign key constraint: Value in one relation must appear in another
Referencing relation from Referenced relation.