0% found this document useful (0 votes)
3 views17 pages

21IT304 Notes-3

The document outlines a course on Database Management Systems focusing on Relational Algebra and Tuple Relational Calculus for 4th-semester B.Tech students in the CSE department. It includes objectives, intended learning outcomes, and teaching methodologies, alongside detailed explanations of relational model concepts, operations, and various types of joins and set operations. The document also provides examples of relational algebra queries and introduces the non-procedural nature of relational calculus.

Uploaded by

kalpananakka35
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)
3 views17 pages

21IT304 Notes-3

The document outlines a course on Database Management Systems focusing on Relational Algebra and Tuple Relational Calculus for 4th-semester B.Tech students in the CSE department. It includes objectives, intended learning outcomes, and teaching methodologies, alongside detailed explanations of relational model concepts, operations, and various types of joins and set operations. The document also provides examples of relational algebra queries and introduces the non-procedural nature of relational calculus.

Uploaded by

kalpananakka35
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/ 17

GMR Institute of Technology GMRIT/ADM/F-44

REV.: 00
Rajam, AP
(An Autonomous Institution Affiliated to JNTUGV, AP)

Cohesive Teaching – Learning Practices (CTLP)

Class 4th Sem. – B. Tech Department: CSE, CSE-AIML, CSE-AIDS


Course Database Management Systems Course Code 21IT304
Prepared by Ms. Y Nagamani, Mr. G Suneel, Dr. P Kanchanamala, Mr. N L V Venugopal, Ms. K
Kiranmai
Lecture Topic Relational Algebra Operations: Selection-Projection-Rename-Set Operators-
Joins- Outer Joins-Division-Relational Calculus: Tuple Relational Calculus
Course Outcome (s) CO2,CO3 Program Outcome (s) PO1, PO2, PO3, PO12, PSO2
Duration 60 Min Lecture 13-18 Unit II
Pre-requisite (s) Relational Algebra

1. Objective

❖ To impart the knowledge of Relational model and integrity constraints over relation
and its implementation.
❖ To impart the knowledge of Tuple Relational calculus.
❖ To Impart the knowledge of views in SQL

2. Intended Learning Outcomes (ILOs)

At the end of this session the students will able to:

A. Comprehend the concepts of integrity constraints in Relational model


B. Demonstrate the implementation tuple relational calculus
C. Classify the different types of views
D. Demonstrate the implementation view update commands
E. Use integrity constraints over relations in real time database projects

3. 2D Mapping of ILOs with Knowledge Dimension and Cognitive Learning Levels of


RBT

Cognitive Learning Levels


Knowledge
Remember Understand Apply Analyze Evaluate Create
Dimension
Factual A
Conceptual A,B
Procedural C,D,E
Meta Cognitive
4. Teaching Methodology

❖ Power Point Presentation, Chalk Talk, visual presentation

5. Evocation
6. Deliverables

Lecture -13

Introduction to Relational Model: Relational model can represent as a table with


columns and rows. Each row is known as a tuple. Each table of the column has a name or
attribute.
Domain: It contains a set of atomic values that an attribute can take.
Attribute: It contains the name of a column in a particular table. Each attribute Ai must
have a domain, dom (Ai)
Relational instance: In the relational database system, the relational instance is
represented by a finite set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all
columns or attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify
the row in the relation uniquely.

Properties of Relations:
• Name of the relation is distinct from all other relations.
• Each relation cell contains exactly one atomic (single) value
• Each attribute contains a distinct name
• Attribute domain has no significance
• tuple has no duplicate value
• Order of tuple can have a different sequence

Lecture-14

The Relational Algebra

• Relational Algebra is one of the two formal query languages associated with the
relational model.
• Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
• Relational algebra is a widely used procedural query language. It collects instances
of relations as input and gives occurrences of relations as output.
• It uses various operations to perform this action. Relational algebra operations are
performed recursively on a relation.
• The output of these operations is a new relation, which might be formed from one
or more input relations. Relational Algebra assumes that duplicate elimination is
always done, so that relations are always set of tuples.

Selection and Projection: Relational Algebra includes operators to select rows from a
relation andto project columns.

Selection:
This is used to fetch rows (tuples) from table (relation) which satisfies a given
condition.
Syntax: σ p(r)
• Where, σ represents the Select Predicate, r is the name of relation (table
name in which you want to look for data), and p is the prepositional logic,
where we specify the conditions that must be satisfied by the data.
• In prepositional logic, one can use unary and binary operators like =, <, >
etc, to specify the conditions.
• Let's take an example of the Student table we specified above in the
Introduction of relational algebra, and fetch data for students with age
more than 17.
σ age > 17 (Student) This will fetch the tuples (rows) from table Student, for
which age will be greater than 17.
• You can also use, and, or etc operators, to specify two conditions, for
example,σage > 17 and gender = 'Male' (Student)
• This will return tuples (rows) from table Student with information of male
students, of age morethan 17.(Consider the Student table has an attribute
Gender too.)
Projection:
• Project operation is used to project only a certain set of attributes of a
relation.
• In simple words, If you want to see only the names all of the students in the
Student table, thenyou can use Project Operation.
• It will only project or shows the columns or attributes asked for, and will
also remove duplicatedata from the columns.
Syntax: ∏A1, A2...(r)
Where A1, A2 etc are attribute names (column names).
For example,
∏Name, Age (Student)
Above statement will show us only the Name and Age columns for all the rows of
data inStudent table.

Lecture-15

Renaming:
• The results of relational algebra are also relations but without any name.
• The rename operation provides database designers to rename the output
relation.
• The rename-operation is denoted using small Greek letter rho (ρ).
• It is written as: ρ x (E).
• Example: We can use the rename operator to rename STUDENT relation to
STUDENT1. ρ(STUDENT1, STUDENT)

Set operations

Union:

• For R 𝖴 S, The union of two relations R and S defines a relation that contains all the
tuples of R, or S, or both R and S, duplicate tuples being eliminated. R and S must be
union- compatible.
• For a union operation to be applied, the following rules must hold −r and s must
have the same quantity of attributes.
• Attribute domains must be compatible.
• Duplicate tuples get automatically eliminated.
Syntax: A 𝖴 B where A and B are relations.
• For example, if we have two tables RegularClass and ExtraClass, both have a
column student to save name of student, then,
∏Student(RegularClass) 𝖴 ∏Student(ExtraClass)
Above operation will give us name of Students who are attending both regular
classes and extra classes, eliminating repetition.

Intersection:

• An intersection is defined by the symbol ∩


A∩B
• Defines a relation consisting of a set of all tuple that are in both A and B. However, A
and B must be union-compatible.
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

Set Difference (-):

• This operation is used to find data present in one relation and not present in the
second relation. This operation is also applicable on two relations, just like Union
operation.
• The attribute name of A has to match with the attribute name in B.
• The two-operand relations A and B should be either compatible or Union
compatible.
• It should be defined relation consisting of the tuples that are in relation A, but not in
B. Syntax: A - B
Where A and B are relations.
• For example, if we want to find name of students who attend the regular class but
not the extra class, then, we can use the below operation:
∏Student(RegularClass) - ∏Student(ExtraClass)

Cross Product (X):


• Cross product between two relations let say A and B, so cross product between A X
B will results all the attributes of A followed by each attribute of B. Each record of A
will pairs with every record of B. below is the example

A
(Name Age Sex ) B
(Id Course)

Ram 14 M 1 DS
Sona 15 F 2 DBMS
kim 20 M
AXB
Name Age Sex Id Course
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS

σtime = 'morning' (RegularClass X ExtraClass)


• For the above query to work, both RegularClass and ExtraClass should have the
attributetime.

Lecture-16

Division operation:
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.
Name Course

System Btech

Database Mtech

Database Btech

Algebra Btech
÷
Course

Btech

Mtech
=
Name

database
The resulting operation must have all combinations of tuples of relation S that are present
in the first relation or R.
Join Operations
Join operation is essentially a Cartesian product followed by a selection criterion. Join
operation denoted by ⋈. JOIN operation also allows joining variously related tuples from
different relations. A Join operation combines related tuples from different relations, if and
only if a given join condition is satisfied.

Types of JOIN:
Various forms of join operation are:
Inner Joins:
• Conditional join
• EQUI join
• Natural join
Outer join:
• Left Outer Join
• Right Outer Join
• Full Outer Join

Inner Join:
In an inner join, only those tuples that satisfy the matching criteria are included, while the
rest are excluded. Let's study various types of Inner Joins:
Conditional Join:
The general case of JOIN operation is called a Condition join. A pair of relation instances as
arguments and returns a relation instances.
Example
A ⋈c B=σc(AxB)
Theta join can use any conditions in the selection criteria.
For example:
A ⋈ A.column 2 > B.column 2 (B)
A ⋈A.column 2> B.column 2 (B)
column 1 column 2
1 2

EQUI join:
When a theta join uses only equivalence condition, it becomes a equi join.
For example:
A ⋈ A.column 2 = B.column 2 (B)
A ⋈ A.column 2 = B.column 2 (B)
column 1 column 2
1 1
EQUI join is the most difficult operations to implement efficiently in an RDBMS and one
reason why RDBMS have essential performance problems.
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute (column) between the
relations. The name and type of the attribute must be same.
Example
Consider the following two tables
C
Num Square
2 4
3 9
D
Num Cube
2 8
3 18
C⋈D
C ⋈ D
Num Square Cube
2 4 4
3 9 9

OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria, we also include some
or all tuples that do not match the criteria.

Left Outer Join(A B)


In the left outer join, operation allows keeping all tuple in the left relation. However, if
there is no matching tuple is found in right relation, then the attributes of right relation in
the join result are filled with null values.

Consider the following 2 Tables


A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
A B
A B
Num Square Cube
2 4 4
3 9 9
4 16 -

Right Outer Join: ( A B)


In the right outer join, operation allows keeping all tuple in the right relation. However, if
there is no matching tuple is found in the left relation, then the attributes of the left
relation in the join result are filled with null values.

A B
Num Cube Square
2 8 4
3 18 9
5 75 -

Full Outer Join: ( A B)


In a full outer join, all tuples from both relations are included in the result, irrespective of
the matching condition.
A B
A B
Num Cube Square
2 4 8
3 9 18
4 16 -
5 - 75

Lecture-17

Examples of Relational Algebra Queries:


Some Queries to solve:

Find the colors of boats reserved by lubber.


Find the names of sailors who have reserved at least one boat.
Find the names of sailors who have reserved at least two boat.
Find the sid’s of sailors with age over 20 who have not reserved a red boat.
Find the names of sailors who have reserved all boats called Interlake.

Lecture-18

Relational Calculus: Tuple Relational Calculus:


Relational Calculus is a non-procedural query language unlike relational algebra. Calculus
provides only the description of the query but it does not provide the methods to solve it.
Thus, it explains what to do but not how to do.
Relational Calculus exists in two forms:
• Tuple Relational Calculus (TRC): variables take on tuples as values
• Domain Relational Calculus (DRC): the variables range over field values
Tuple Relational Calculus (TRC):
A tuple variable is a variable that takes on tuples of a particular relation schema as values
i.e., every value assigned to a given tuple variable has the same number and type of fields.
In Tuple Calculus, a query is expressed as
{t| P(t)}
where t = resulting tuples,
P(t) = known as Predicate and these are the conditions that are used to fetch t
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
{T.name |T ∈ Student AND T.age > 17}

Example Queries
Say that we want to find the branch-name, loan-number, and amount for loans of over
$1200:
{t | t € loan 𝖠 t[amount] > 1200}

Suppose that we want only the loan-number attribute, rather than all attributes of the
loan relation. To write this query in the tuple relational calculus, we need to write an
expression for a relation on the schema (loan-number). We need those tuples on (loan-
number) such that there is a tuple in loan with the amount attribute > 1200. To express
this request, we need the construct “there exists” from mathematical logic.
The notation
€t € r (Q(t)) means “there exists a tuple t in relation r such that predicate Q(t) is true.”
Using this notation, we can write the query “Find the loan number for each loan of an
amount greater than $1200” as
{t | ∃s Є loan (t[loan-number] = s[loan-number] 𝖠 s[amount] > 1200)}

In English, we read the preceding expression as “The set of all tuples t such that there
exists a tuple s in relation loan for which the values of t and s for the loan-number
attribute are equal, and the value of s for the amount attribute is greater than$1200.”
Tuple variable t is defined on only the loan-number attribute, since that is the only
attribute havinga condition specified for t. Thus, the result is a relation on (loan number).
Consider the query “Find the names of all customers who have a loan from the Perryridge
branch.” This query is slightly more complex than the previous queries, since it involves
two relations: borrower and loan. As we shall see, however, all it requires is that we
have two “there exists” clauses in our tuple-relational-calculus expression, connected by
and (𝖠).We write the query as follows:
{t | ∃s Єborrower (t[customer-name] = s[customer-name]
𝖠 ∃ u Є loan (u[loan-number] = s[loan-number ]
𝖠 u[branch-name] = “Perryridge”))}

In English, this expression is “The set of all (customer-name) tuples for which the
customer has a loan that is at the Perryridge branch.” Tuple variable u ensures that the
customer is a borrower at the Perryridge branch. Tuple variable s is restricted to pertain
to the same loan number as s.

To find all customers who have a loan, an account, or both at the bank, we used the
union operation in the relational algebra. In the tuple relational calculus, we shall
need two “there exists” clauses,connected by or (∨):
{t | s Єborrower (t[customer-name] = s[customer-name])
∨ ∃ u Є depositor (t[customer-name] = u[customer-name])}

This expression gives us the set of all customer-name tuples for which at least one of the
followingholds:
• The customer-name appears in some tuple of the borrower relation as a borrower
from the bank.
• The customer-name appears in some tuple of the depositor relation as a depositor of
the bank.

If some customer has both a loan and an account at the bank, that customer appears only
once in the result, because the mathematical definition of a set does not allow duplicate
members.
If we now want only those customers who have both an account and a loan at the bank, all
we need to do is to change the or (∨) to and (𝖠) in the preceding expression.
{t | s Єborrower (t[customer-name] = s[customer-name])
𝖠 ∃ u Є depositor (t[customer-name] = u[customer-name])}
The result of this query appeared.

Now consider the query “Find all customers who have an account at the bank but do not
have a loanfrom the bank.” The tuple-relational-calculus expression for this query is
similar to the expressions that we have just seen, except for the use of the not
(¬) symbol:
{t | u Єdepositor (t[customer-name] = u[customer-name])
𝖠 ¬ s Є borrower (t[customer-name] = s[customer-name])}

7. Keywords

❖ Union
❖ Select
❖ Project
❖ Relational Tuple Calculus
❖ Intersection

8. Sample Questions

Remember:

1. Define relational algebra set operation


2. List the 3 relational algebra set operations
3. Define tuple relational calculus
4. List the 2 types of relational calculus

Understand:

1. Explain set operations in relational algebra


2. Compare relational algebra set operators with SQL queries with examples
3. Compare tuple relational calculus and relational algebra
4. Explain tuple relational calculus with example queries

Apply:

1. Sailors(sid,sname,rating,age)
Boats(bid,bname,color)Reserves(sid,bid,day)
Find the names of sailors who have reserved a red or a green boat
Find the names of sailors who have reserved a red and a but not green boat.
2. Find all professors who taught at least two different courses or two
different sections of the same course in the same classroom in the same
semester and the same year. Result contains both the faculty id, and the
semester/year information.
3. Find the equivalent view statement to this: SELECT DISTINCT F.Name, C.CrsCode
FROM FACULTY F, CLASS C WHERE F.Id = C.InstructorId AND C.Year = 2002.

4. Relational Scheme: student (rollNo, name, degree, year, sex, deptNo, advisor)
department (deptId, name, hod, phone) professor (empId, name, sex,
startYear, deptNo, phone) course (courseId, cname, credits, deptNo)
enrollment (rollNo, courseId, sem, year, grade) teaching (empId, courseId,
sem, year, classRoom) preRequisite (preReqCourse, courseID)
1. Determine the departments that do not have any girl students
2. Get the names of students who have scored „S‟ in all subjects they have
enrolled.Assume that every student is enrolled in at least one course.
5. Find the names, street address, and cities of residence of all employees who
work forFirst Bank Corporation and earn more than $10,000 per annum.
6. Find the names of all employees in this database who live in the same city as
the company for which they work. e. Find the names of all employees who
live in the samecity and on the same street as do their managers.

9. Stimulating Question (s)


“Find the names of all customers who have a loan at the bank.” Rewrite the query to
include not only the name, but also the city of residence for each customer. Observe that
now customer Jackson no longer appears in the result, even though Jackson does in fact
have a loan from the bank.
1. Explain why Jackson does not appear in the result.
2. Suppose that you want Jackson to appear in the result. How would you
modify thedatabase to achieve this effect?
3. Again, suppose that you want Jackson to appear in the result. Write a
query using anouter join that accomplishes this desire without your
having to modify the database.

10. Mind Map


11. Student Summary

At the end of this session, the facilitator (Teacher) shall randomly pick-up few students
to summarize the deliverables.

12. Reading Materials

1.Database Management Systems, Raghurama Krishnan, Johannes Gehrke, TATA


McGrawHill3rdEdition page no: 192-198
2.Database System Concepts, Silberschatz, Korth, McGraw hill, 5th Edition

13. Scope for Mini Project

-------------------------

You might also like