21IT304 Notes-3
21IT304 Notes-3
REV.: 00
Rajam, AP
(An Autonomous Institution Affiliated to JNTUGV, AP)
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
5. Evocation
6. Deliverables
Lecture -13
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
• 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:
• 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)
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
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.
A B
Num Cube Square
2 8 4
3 18 9
5 75 -
Lecture-17
Lecture-18
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:
Understand:
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.
At the end of this session, the facilitator (Teacher) shall randomly pick-up few students
to summarize the deliverables.
-------------------------