0% found this document useful (0 votes)
9 views42 pages

Relational Algebra and Relational Calculus July 2024

The document provides an overview of relational algebra, including its basic operations such as selection, projection, and join, as well as the concept of relations and their schemas. It discusses the importance of relational calculus, including tuple and domain relational calculus, in querying databases. Additionally, it explains the significance of keys in databases, particularly super keys, for uniquely identifying tuples in relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views42 pages

Relational Algebra and Relational Calculus July 2024

The document provides an overview of relational algebra, including its basic operations such as selection, projection, and join, as well as the concept of relations and their schemas. It discusses the importance of relational calculus, including tuple and domain relational calculus, in querying databases. Additionally, it explains the significance of keys in databases, particularly super keys, for uniquely identifying tuples in relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Relational Algebra

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

• A1, A2, …, An are attributes


• R = (A1, A2, …, An ) is a relation schema
• Example: instructor = (ID, name, dept_name, salary)
• Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Ɛ Di

• The current values (relation instance) of a relation are specified


by a table
• An element t of r is a tuple, represented by a row in a table
Relations are Unordered
• Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
• Example: instructor relation with unordered tuples
Database
• A database consists of multiple relations
• Information about an enterprise is broken up into parts

• 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

Understanding Algebra & Calculus is key to understanding SQL,


query processing!
Relational Algebra

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

Select tuples with A=B and D > 5


σ A=B and D > 5 (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

R1 sid bid day S1 sid sname rating age


22 101 10/10/96 22 dustin 7 45.0
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
S2 sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Union, Intersection, Set-Difference

All of these operations take sid sname rating age


two input relations, which
22 dustin 7 45.0
must be union-compatible:
31 lubber 8 55.5
Same number of fields.
`Corresponding’ fields have the
58 rusty 10 35.0
same type.(Same Domain) 44 guppy 5 35.0
What is the schema of result? 28 yuppy 9 35.0
S1 S2

sid sname rating age


sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1 S2 S1 S2
Union of two relations
Relations r, s:

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

 Solution is Renaming operator:  (C(1 sid1, 5 sid2), S1 R1)


Joining two relations – Natural Join

 Let r and s be relations on schemas R and S respectively.


Then, the “natural join” of relations R and S is a relation
on schema R S obtained as follows:
 Consider each pair of tuples tr from r and ts from s.
 If tr and ts have the same value on each of the attributes
in R  S, add a tuple t to the result, where
 t has the same value as tr on r
 t has the same value as ts on s
Natural Join Example
Example Of Natural Join In SQL

Consider the following student and fee tables with common column names and data types;

Fees

The SQL query for a natural join:

SELECT s.Name, s.Contact, f.Amount, f.Paid FROM Student s NATURAL JOIN Fee f;

Output:

Name Contact Amount Paid

John 1234567890 5000 Yes

Sam 0987654321 4000 No


Natural Join Example
 Relations r, s:

 Natural Join
 r s
(( Re serves)  CSailors
S = C )(R  S)
bid 103
Condition Join: R

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96

S1 S1.sid < R1.sid R1


Result schema same as that of cross-product.
Fewer tuples than cross-product, might be able to compute more
efficiently. Sometimes called a theta-join.
Equi-Join: A special case of condition join where the condition c
contains only equalities and ^.
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96

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:

• Attributes of B is proper subset of Attributes of A.

• The relation returned by division operator will have attributes = (All


attributes of A – All Attributes of B)

• The relation returned by division operator will return those tuples from
relation A which are associated to every B’s tuple.

• Division can be expressed in terms of Cross Product , Set


Difference and Projection.
Figure in-2.1
Relational Calculus
Non-Procedural
Relational Calculus

Tuple relational calculus (TRC) and


Domain relational calculus (DRC).
Calculus has variables, constants, comparison ops, logical connectives,
and quantifiers.
TRC: Variables range over (i.e., get bound to) tuples.
DRC: Variables range over domain elements (= field values).
Both TRC and DRC are simple subsets of first-order logic.
Expressions in the calculus are called formulas.
An answer tuple is essentially an assignment of constants to variables
that make the formula evaluate to true.
Tuple Relational Calculus

 Query has the form: { T | p(T)}


 Answer includes all tuples T that make the formula p(T)
be true.
 Formula is recursively defined, starting with
 simple atomic formulas (getting tuples from
 relations or making comparisons of values),
 and building bigger and better formulas using
 the logical connectives.
TRC Formulas

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

The use of quantifiers X and X in a formula is said to bind X.


A variable that is not bound is free.
Let us revisit the definition of a query: {T|p(T)}

• There is an important restriction:


• the variable T that appears to the left of `|’ must
be the only free variable in the formula p(...).
Find all sailors with a rating above 7

{S | S  Sailors ^ S.rating > 7}


Query is evaluated on an instance of Sailors
Tuple variable S is instantiated to each tuple of this instance in turn, and the
condition “S.rating > 7” is applied to each such tuple.
Answer contains all instances of S (which are tuples of Sailors) satisfying
the condition.
Find sailors rated > 7 who’ve reserved
boat #103

{S | (S  Sailors) ^ (S.rating > 7) ^ ( R  Reserves (R.sid = S.sid ^ R.bid = 103))}


Note the use of  to find a tuple in Reserves that `joins with’ the
Sailors tuple under consideration.
R is bound, S is not
Domain Relational Calculus
• Domain Relational Calculus is a non-procedural query language
equivalent to Tuple Relational Calculus.
• Domain Relational Calculus provides only the description of the query
but,
• it does not provide the methods to solve it.
• In Domain Relational Calculus, a query is expressed as,
• { < x , x , x , ..., x > | P (x , x , x , ..., x ) }
1 2 3 n 1 2 3 n

• where, < x , x , x , …, x > represents resulting domains variables and P (x ,


1 2 3 n 1

x , x , …, x )
2 3 n

• represents the condition or formula equivalent to the Predicate calculus.

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

It is possible to write syntactically correct calculus queries that


have an infinite number of answers! Such queries are called
unsafe.
S |  S  Sailors
  
   
e.g.,    
   
 

It is known that every query that can be expressed in relational


algebra can be expressed as a safe query in DRC / TRC; the
converse is also true.
Relational Completeness: Query language (e.g., SQL) can express
every query that is expressible in relational algebra/calculus.
Keys in DBMS
Each row has a value of a data item (or set of items) that
uniquely identifies that row in the table called the key

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

Primary Key, Alternate Key


Primary Key of R:
• Is a candidate key chosen by database administrator as a primary
key
• Example: Pan No. Primary Key
• Voter Id, Passport No are Alternate Keys
Composite Key
• Composite key is a combination of more than one attributes that
can be used to uniquely identity each record.
• It is also known as “Compound” key.
• A composite key may be a candidate or primary 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.

dbit322024=# create table dept(did serial primary key,dname text);

dbit322024=# create table employee(eid serial primary key,ename text, did


int references dept(did));

You might also like