0% found this document useful (0 votes)
5 views101 pages

3cs518ic24 Dbms Minor - Unit 2 (A)

This document provides an introduction to the relational model, covering the structure of relational databases, database schema, keys, and relational query languages. It explains key concepts such as tables, attributes, tuples, and various types of keys including primary, candidate, and foreign keys. Additionally, it discusses constraints that ensure data integrity within the database.

Uploaded by

prasham1380
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views101 pages

3cs518ic24 Dbms Minor - Unit 2 (A)

This document provides an introduction to the relational model, covering the structure of relational databases, database schema, keys, and relational query languages. It explains key concepts such as tables, attributes, tuples, and various types of keys including primary, candidate, and foreign keys. Additionally, it discusses constraints that ensure data integrity within the database.

Uploaded by

prasham1380
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 101

Introduction to Relational

Model
Unit-2

rces: Reference Book, NPTEL website, Internet


 Looping
Outline
• Structure of Relational Databases
• Database Schema
• Keys
• Schema Diagrams
• Relational Query Languages
• The Relational Algebra
Structure of Relational Databases
Columns Table (Relation): A database object
(5) that holds a collection of data for a
specific topic.
Stud Table consist of rows and columns.
Attributes:
ent
Roll Branc Semes SP
Title of Name Column (Attribute): The vertical
column No h ter I
component of a table. A column has a
101 Raju CE 3 8 name and a particular data type; e.g.
102 Mites CI 3 7 varchar, decimal, integer, datetime etc.

Cardinality = No of
Rows or h Record (Tuple): The horizontal
Tuples or component of a table, consisting of a
103 Mayu CE 3 6
Records sequence of values, one for each
r

tuples (7)
(7) column of the table. It is also known as
104 Niles EE 3 9 row.
h
A database consists of a collection of
105 Hites CI 3 7 tables (relations), each having a unique
h name.
Degree = No of
Domain 106is a setTarun ME
columns
of all 3 unique 8
(5)
possible values for
a specific107
column.Sures CE 3 9
Domain of Branch h attribute is (CE, CI, ME, EE)
(DBMS)  Unit 2 – Introduction to Relational Model 3
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
Relation=table
Tuple=row
Attribute=column

(DBMS)  Unit 2 – Introduction to Relational Model 4


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” or does not exist
 The null value causes complications in the definition of many operations
 For example: consider a relation student(rollno, name, phone) and for a particular
tuple(row) student does not carry phone.
 We shall see later that null values cause a number of difficulties when we access or
update the database.
 Relations are Unordered
 Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
 Example: instructor relation with unordered tuples

(DBMS)  Unit 2 – Introduction to Relational Model 5


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
Relational Database schema(related
relations)
• student (ID, name, dept name, tot cred)
• advisor (s id, i id)
• takes (ID, course id, sec id, semester,
year, grade)
• classroom (building, room number,
capacity)
• time slot (time slot id, day, start time,
end time)

(DBMS)  Unit 2 – Introduction to Relational Model 6


Keys
 We must have a way to specify how tuples within a given relation are
distinguished i.e. attribute values of a tuple must be such that they can
uniquely identify the tuple{no redundancy}

Different Types of Keys


 Super Key
 Candidate Key
 Primary Key
 Alternate Key
 Foreign Key

(DBMS)  Unit 2 – Introduction to Relational Model 7


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) {set of one or more attributes that, taken collectively, allow us to identify
uniquely a tuple in the relation}
 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

(DBMS)  Unit 2 – Introduction to Relational Model 8


Super Key
 A super key is a set of one or more attributes whose values uniquely
identifies each record within a relation (table).
Super Key Super Key
Super Key
(RollNo, Branch, (SPI, Name,
EnrollNo
Sem) BL)

Roll Branc Se SP B
EnrollNo Name
No h m I L
• Adding zero or more attributes
190540107 101 CE 3 8 Raju 0 to candidate key generates
001 super key.
190540107 102 CE 3 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 r
190540106 102 CI 3 7 Niles 1
002 h
180540107 101 CE 5 7 Niles 1
001 h
(DBMS)  Unit 2 – Introduction to Relational Model 9
Candidate Key
 A candidate key is a subset of a super key.
 The minimal set of attribute which can uniquely identify a tuple is known as candidate
key. E.g. EnrollNo
 The value of Candidate Key is unique and non-null for every tuple.
 There can be more than one candidate key in a relation
 Every candidate key is a super key but every super key is not a candidate key.
 The candidate keyCandidate Candidate
can be simple (having Key
only one attribute) or composite as well
Key (RollNo, Branch,
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS)  r Model
Unit 2 – Introduction to Relational 10
Primary Key
 A primary key is a candidate key that is chosen by database
designer to identify tuples uniquely in a relation (table).

Primary
Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS)  r Model
Unit 2 – Introduction to Relational 11
Alternate Key
 The candidate key other than the primary key is called an alternate
key.
 An alternate key is a candidate key that is not chosen by database
designer to identify tuples uniquely in a relation.
Primary Alternate
Key Key
Candidate Candidate Key
Key (RollNo, Branch,
EnrollNo Sem)

Roll Branc Se SP B
EnrollNo Name
No h m I L
190540107 101 CE 3 8 Raju 0
001
180540107 101 CE 5 7 Mites 1
002 h
190540106 101 CI 3 6 Mayu 2
001 (DBMS)  r Model
Unit 2 – Introduction to Relational 12
Primary Key rules
 A primary key may have one or more attributes.
 There is only one primary key in the relation (table).
 A primary key attribute value cannot be NULL.
 Generally, the value of a primary key attribute does not change.

(DBMS)  Unit 2 – Introduction to Relational Model 13


Foreign Key
 A foreign key is used to link two relations (tables).
 A foreign key is an attribute or collection of attributes in one table that
refers to the primary key in another table.
 A table containing the foreign key is called the child table, and the table
containing the primary key is called the parent table.
Parent Child
Table Table

Stud Proje
ent Nam Branc Se ct
EnrollNo Projec
e h m Title EnrollNo
tID
190540107 Raju CE 3 101 Bank 190540107
001 001
190540107 Mites CE 3 102 College 190540107
002 h 002
190540107 Niles CE 3 103 School 190540107
003 h (DBMS)  Unit 2 – Introduction to Relational Model003 14
Foreign Key: If an attribute can only take the values which are present as
values of some other attribute.
 The relation which is being referenced is called referenced relation and the
corresponding attribute is called referenced attribute and the relation
which refers to the referenced relation is called referencing relation and
the corresponding attribute is called referencing attribute.
 The referenced attribute of the referenced relation should be the primary
key for it. For Example, EnrollNO in Project is a foreign key of the
relation.
 It may be worth noting that unlike, Primary Key of any given relation,
Foreign Key can be NULL as well as may contain duplicate tuples i.e. it
need not follow uniqueness constraint.
 For Example, EnrollNO in Project relation is not unique. It has been
repeated for the first and fourth tuple. However, the EnrollNO in Student
relation is a primary key and it needs to be always unique and it cannot be
null.
(DBMS)  Unit 2 – Introduction to Relational Model 15
1. Candidate Key: are individual columns in a table that qualifies for uniqueness of all the rows.
Here in Employee table EmployeeID & SSN are Candidate keys.
2. Primary Key: is the columns you choose to maintain uniqueness in a table. Here in Employee
table you can choose either EmployeeID or SSN columns.
3. Super Key: If you add any other column/attribute to a Primary Key(candidate key) then it become
a super key, like EmployeeID + FullName is a Super Key.
4. Alternate Key: Candidate column other the Primary column, like if SSN is PK then EmployeeID
would be the Alternate key.

(DBMS)  Unit 2 – Introduction to Relational Model 16


Constraints
 Relational constraints are the restrictions imposed on the database
contents and operations.
 They ensure the correctness of data in the database.

(DBMS)  Unit 2 – Introduction to Relational Model 17


1. Domain Constraint-
 Domain constraint defines the domain or set of values for an attribute.
 It specifies that the value taken by the attribute must be the atomic value
from its domain

 Consider the following Student table-


 Here, value ‘A’ is not allowed since only integer values can be taken by
the age attribute. (DBMS)  Unit 2 – Introduction to Relational Model 18
2. Tuple Uniqueness Constraint-
 Tuple Uniqueness constraint specifies that all the tuples must be
necessarily unique in any relation.
 Consider the following table.

 This relation satisfies the tuple uniqueness constraint since here all the
tuples are unique.
 The second relation does not satisfy the tuple uniqueness constraint since
here all the tuples are not
(DBMS)unique.
 Unit 2 – Introduction to Relational Model 19
3. Key Constraint-
Key constraint specifies that in any relation-
 All the values of key must be unique.

Consider the table

This relation does not satisfy the key constraint as here all the values of primary key are
not unique.

(DBMS)  Unit 2 – Introduction to Relational Model 20


4. Entity Integrity Constraint-
 Entity integrity constraint ensures that the primary key attribute in a
relation, should not accept a null value.
 This is because the presence of null value in the primary key violates the
uniqueness property.
Consider the table

 This relation does not satisfy the entity integrity constraint as here the
primary key contains a NULL value.
(DBMS)  Unit 2 – Introduction to Relational Model 21
5. Referential Integrity Constraint-
 This constraint is enforced when a foreign key references the primary key of a
relation.
 It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.

The following two important results emerges out due to referential integrity
constraint-
 We can not insert a record into a referencing relation if the corresponding record
does not exist in the referenced relation.
 We can not delete or update a record of the referenced relation if the corresponding
record exists in the referencing relation.

(DBMS)  Unit 2 – Introduction to Relational Model 22


 Here,
• The relation ‘Student’ does not satisfy the referential integrity constraint.
• This is because in relation ‘Department’, no value of primary key specifies department
no. D14.
• Thus, referential integrity constraint
(DBMS)  Unitis
2 –violated.
Introduction to Relational Model 23
RIC violation
RIC violation can occur on deletion or modification of a referenced tuple. On
insertion there would be no violation. The Action to this can be specified for
every case by using the qualifiers ON DELETE and ON UPDATE.
 Actions to be taken are explained as follows :
 After any violation to Referential Integrity Constraint has occurred, there can be three possibilities which
can be used as an action : SET NULL, SET DEFAULT, CASCADE.

1. Referenced Relation : Table consists of Primary Key which is referenced by some


foreign key. Actions :

a. DELETION :
 ON DELETE NO ACTION : If referential integrity violation has occurred, the deletion would not
be allowed.
 ON DELETE CASCADE : In the case of referential integrity violation, the tuple would be deleted
from both the relation i.e. Referenced as well as Referencing Relation. Whenever Primary Key
is deleted, the corresponding tuple in the referencing relation is also deleted.
 ON DELETE SET NULL : In the case of violation, then the values of FOREIGN KEY are set to
NULL. If the FOREIGN KEY is a NOT NULL attribute , then ON DELETE SET NULL = ON DELETE
NO ACTION. (DBMS)  Unit 2 – Introduction to Relational Model 24
b. UPDATION :
 ON UPDATE NO ACTION : Updation would be restricted
 ON UPDATE CASCADE : Updation would be performed in both the referenced as well as
referencing relation.
 ON UPDATE SET NULL : Referencing attribute tried to set NULL, if its a success , updation of
referenced relation is allowed.

2. Referencing Relation :
 INSERTION : Violation might occur. If the violation occurs, then insertion gets
restricted.
 DELETION : No violation occurs.
 UPDATION : Referencing attribute may cause violation, in that case updation is not
allowed.

(DBMS)  Unit 2 – Introduction to Relational Model 25


SQL constraints
 Not null
 Create table emp(eid number(2) not null, ename varchar(10))
 Unique
 Create table emp(eid number(2) unique, ename varchar(10))
 Primary key{unique-null}
 Create table emp(eid number(2) primary key, ename varchar(10))
 Foreign key
 Create table person(pid int primary key, name varchar(10))
 Create table order(oid int primary key, orderno int, pid int references person(pid))
 Check
 Create table person(pid int check(pid>0), name varchar(10))
 Default
 Create table emp(eid number(2), ename varchar(10),comm int default 0)

(DBMS)  Unit 2 – Introduction to Relational Model 26


Relational Query Languages
 Relational query languages use relational algebra to break the user requests and instruct
the DBMS to execute the requests. It is the language by which user communicates with
the database. These relational query languages can be procedural or non-procedural.
 SQL has its own querying methods to interact with database. But how these queries work
in the database? These queries work similar to relational algebra that we have in
mathematics. In database we have tables participating in relational algebra.
 Procedural Query Language
 A procedural query language will have set of queries instructing the DBMS to
perform various transactions in the sequence to meet the user request. For example,
get_CGPA procedure will have various queries to get the marks of student in each
subject, calculate the total marks, and then decide the CGPA based on his total
marks. This procedural query language tells the database what is required from the
database and how to get them from the database. Relational algebra is a procedural
query language.
 Non-Procedural Query Language
 Non-procedural queries will have single query on one or more tables to get result
from the database. For example, get the name and address of the student with
particular ID will have single query on STUDENT table. Relational Calculus is a non
procedural language which informs what to do with the tables, but doesn’t 27inform
(DBMS)  Unit 2 – Introduction to Relational Model
Relational Algebra
 Relational algebra is a procedural query language. It takes one or more
relations / tables and performs the operation and produce the result. This
result is also considered as a new table or relation.
 Relational algebra will have operators to indicate the operations. This
algebra can be applied on single relation – called unary or can be applied
on two tables – called binary.
 Relational Algebra works on the whole table at once, so we do not have to
use loops etc to iterate over all the rows(tuples) of data one by one.
 Relational Algebra is formal description of how relational database
operates.
 It is a procedural query language, i.e. user must define both “how” and
“what” to retrieve.
 All we have to do is specify the relation(table) name from which we need
the data, and in a single line of command, relational algebra will traverse
the entire given table to fetch data for you.
(DBMS)  Unit 2 – Introduction to Relational Model 28
Operators
1. Select: 
2. Project: 
3. Cartesian product: X
4. Inner Join:
a) Natural Join:
b) Theta join:
5. Outer join
a) Left outer join:
b) Right outer join:
c) Full outer join or outer join:
6. Additional operator:

a) Set union: 
b) Set intersection : ∩
c) Set difference: --
d) Division: /
e) rename: 
f) Assignment: 
(DBMS)  Unit 2 – Introduction to Relational Model 29
Relational Algebra Operations
Operator Description
Selection Display particular rows/records/tuples from a relation
Projection Display particular columns from a relation
Cross Product Multiply each tuples of both relations
Combine data or records from two or more tables
1. Natural Join / Inner Join
Joins 2. Outer Join
1. Left Outer Join 2. Right Outer Join 3. Full Outer Join

Combine the results of two queries into a single result.


Set Operators
1. Union 2. Intersection 3. Minus / Set-difference
Division Divides one relation by another
Rename Rename a column or a table

(DBMS)  Unit 2 – Introduction to Relational Model 30


1. Select Operator
 Symbol: σ (Sigma)
 Notation: σ (Relation)
condition
 Operation: Selects tuples from a relation that satisfy a given
condition.
 Operators: =, <>, <, >, <=, >=, Λ (AND), V (OR)
Exam σ
Display the detail of students belongs to Answ Branch=‘CE’
ple “CE” Branch. er (Student)
Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc SP
No e h I No e h I
101 Raju CE 8 101 Raju CE 8
102 Mites ME 9 104 Meet CE 9
h
103 Niles CI 9
h
104 Meet CE 9 (DBMS)  Unit 2 – Introduction to Relational Model 31
Selection Operator [σ condition (Relation)]

Exam Display the detail of students belongs to “CE” Branch and having
ple SPI more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h

Answ σBranch=‘CE’ Λ SPI>8


104 Meet CE 9

er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
104 Meet CE 9

(DBMS)  Unit 2 – Introduction to Relational Model 32


Selection Operator [σ condition (Relation)]
Exam Display the detail of students belongs to either “CI”
ple or “ME” Branch.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104 σBranch=‘CI’
Answ Meet CE 9
V Branch=‘ME’
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
102 Mites ME 9
h
103 Niles CI 9 (DBMS)  Unit 2 – Introduction to Relational Model 33
Selection Operator [σ condition (Relation)]
Exam Display the detail of students whose SPI
ple between 7 and 9.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104 σSPI>7CEΛ SPI<99
Answ Meet
er (Student)
Outp
ut
Roll Nam Branc SP
No e h I
101 Raju CE 8

(DBMS)  Unit 2 – Introduction to Relational Model 34


Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display the detail of students whose RollNo is less than
104. 101 Raj CE 6
 Display the detail of students having SPI more than 8. 102 Meet ME 8
 Display the detail of students belongs to “CE” Branch 103 Hars EE 7
having SPI less than 8. h
 Display the detail of students belongs to either “CE” or 104 Punit CE 9
“ME”down
 Write Branch. the relational algebra for the Emplo
 Display the detail of students whose SPI between 6 and yee
EmpI Nam Sala
employee table. Dept
9. D e ry
 Display the detail of all employee.
101 Niles Sales 1000
 Display the detail of employee whose Salary more than
h 0
10000.
 Display the detail of employee belongs to “HR” Dept 102 Mayu HR 2500
r 0
having Salary more than 20000.
 Display the detail of employee belongs to either “HR” 103 Hardi HR 1500
k 0
or “Admin” Dept.
 Display the detail of employee whose Salary between 104 Ajay Admi 2000
(DBMS)  Unit 2 – Introduction to Relational Model n 350
2. Project Operator
 Symbol: ∏ (Pi)
 Notation: ∏
attribute set (Relation)
 Operation: Selects specified attributes of a relation.
 It removes duplicate tuples (records) from the result.
Exam Display RollNo, Name and Branch of Answ ∏ RollNo, Name, Branch
ple all students. er (Student)

Stud Outp
ent
Roll Nam Branc SP ut
Roll Nam Branc
No e h I No e h
101 Raju CE 8 101 Raju CE
102 Mites ME 9 102 Mites ME
h h
103 Niles CI 9 103 Niles CI
h h
104 Meet CE 9 (DBMS)  Unit 1 – Overview and Architecture of104 Meet CE 36
Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display RollNo, Name and SPI of all students.
 101 Raj CE 6
Display Name and SPI of all students.
 Display the Name of all students. 102 Meet ME 8
 Display the Name of all branches. 103 Hars EE 7
h
104 Punit CE 9
 Write down the Emplo
relational algebra for the yee
employee table. EmpI Nam Sala
Dept
D e ry
 Display EmpID with Name of all employee.
101 Niles Sales 1000
 Display Name and Salary of all employee. h 0
 Display the Name of all employee. 102 Mayu HR 2500
 Display the Name of all departments. r 0
103 Hardi HR 1500
k 0
104 Ajay Admi 2000
(DBMS)  Unit 2 – Introduction to Relational Model n 0
37
2.5 Combined Projection & Selection
Operation
Exam Display RollNo, Name & Branch of “ME”
ple Branch students.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σ CE
Branch=‘ME’ 7 Answ ∏ RollNo, Name, Branch (σBranch=‘ME’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Roll Nam Branc
No e h I No e h
102 Mites ME 9 102 Mites ME
h h
(DBMS)  Unit 1 – Overview and Architecture of
38
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students whose SPI is
ple more than 8.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σSPI>8CE 7 Answ ∏ Name, Branch, SPI (σSPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
102 Mites ME 9 Mites ME 9
h h
103 Niles CI 9 (DBMS)  Unit 1 – OverviewNiles CI
and Architecture of 9 39
Combined Projection & Selection Operation
Exam Display Name, Branch and SPI of students who belongs to “CE” Branch and
ple SPI is more than 7.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘CE’
CE 7
Λ SPI>7 Answ ∏ Name, Branch, SPI (σBranch=‘CE’ Λ SPI>8
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
101 Raju CE 8 Raju CE 8

(DBMS)  Unit 2 – Introduction to Relational Model 40


Combined Projection & Selection Operation
Exam Display Name of students along with their Branch who belong to either “ME”
ple Branch or “CI” Branch.
Stud
ent
Roll Nam Branc SP
No e h I
101 Raju CE 8
102 Mites ME 9
h
103 Niles CI 9
h
104
Step- Meet σBranch=‘ME’
CE 7
V Branch=‘CI’ Answ ∏ Name, Branch (σBranch=‘ME’ V Branch=‘CI’
1 (Student) er (Student))
Outpu Outpu
t-1
Roll Nam Branc SP t-2
Nam Branc
SPI
No e h I e h
102 Mites ME 9 Mites ME 9
h h
103 Niles CI 9 Niles CI 9
(DBMS)  Unit 2 – Introduction to Relational Model 41
Exercise
 Write down the relational algebra for the student Stud
ent
table. Roll Nam Branc SP
No e h I
 Display Rollno, Name and SPI of all students belongs to
“CE” Branch. 101 Raj CE 6
 List the Name of students with their Branch whose SPI 102 Meet ME 8
is more than 8 and belongs to “CE” Branch.
103 Hars EE 7
 List the Name of students along with their Branch and h
SPI who belongs to either “CE” or “ME” Branch and
104 Punit CE 9
having SPI more than 8.
 Write Emplo
down the relational algebra for the
 Display the Name of students with their Branch name yee
employee
whose SPItable.
between 7 and 9.
EmpI Nam
Dept
Sala
 Display the Name of employee belong to “HR” Dept D e ry
and having salary more than 20000. 101 Niles Sales 1000
 Display the Name of all “Admin” and “HR” Dept’s h 0
employee. 102 Mayu HR 2500
 List the Name of employee with their Salary who r 0
belongs to “HR” or “Admin” Dept having salary more 103 Hardi HR 1500
than 15000. k 0
 Display the Name of employee along with their Dept 104 Ajay Admi 2000
name whose salary between
(DBMS) 15000 and 30000.
Unit 2 – Introduction to Relational Model n 0
42
3. Cartesian Product / Cross Product
 Symbol: X (Cross)
 Notation: Relation-1 (R1) X Relation-2 (R2) OR Algebra-1 X Algebra-2
 Operation: It will multiply each tuples of Relation-1 to each tuples of
Relation-2.
 Attributes of Resultant Relation = Attributes of R1 + Attributes of R2
 Tuples of Resultant Relation = Tuples of R1 * Tuples of R2
Exam Perform Cross Product between Student Answ (Student) X
ple and Result. er (Result)

Stud Resu Outp


ent
RN Nam Branc lt
RN SP ut
Student. Nam Branc Result. SP
o e h o I RNo e h RNo I
10 Raju CE 10 8 101 Raju CE 101 8
1 1
101 Raju CE 102 9
10 Mites ME 10 9
If both relations have some attribute with the same 102 Mites ME 101 8
2 h 2
name, it can be distinguished by combing relation- h
name.attribute-name.
102
(DBMS)  Unit 1 – Overview and Architecture of
Mites ME 102 9
43
Cartesian Product / Cross Product Example
Consider only selected
Exam Perform Cross Product between Student attributes
ple and Result. • Student – RNo, Name and
Stud Resu Branch
ent
RN Nam Branc Se lt
RN SP B Ran • Result – RNo, SPI and BL
o e h m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h
Answ ∏ RNo, Name, Branch (Student)
3 X ∏ RNo, SPI, BL
er (Result)
Outp
ut
Student. Nam Branc Result. SP B
RNo e h RNo I L
101 Raju CE 101 8 1
101 Raju CE 103 9 0
102 Mites ME 101 8 1
h
102 Mites ME 103 9 0
(DBMS)  Unit 2 – Introduction to Relational Model 44
Cartesian Product / Cross Product Example
Exam Perform Cross Product between Student Consider only selected tuples
• Student – Branch=‘CE’ and
ple and Result.
Stud Resu Sem=3
ent lt • Result – SPI>7 and BL<1
RN Nam Branc Se RN SP B Ran
o e h m o I L k
10 Raju CE 3 10 8 1 2
1 1
10 Mites ME 5 10 9 0 1
2 h 3
10 Om CE 3 10 7 2 3
Answ σBranch=‘CE’ Λ Sem=3 X σSPI>7 Λ BL<1
3 5
(Student)
10
er Dhar CE
(Result) 5
4
Outpa
ut
Student. Nam Branc Se Result. SP B Ran
RNo e h m RNo I L k
101 Raju CE 3 103 9 0 1
103 OM CE 3 103 9 0 1
(DBMS)  Unit 2 – Introduction to Relational Model 45
4. Natural Join / Inner Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 Operation: Natural join will retrieve consistent data from multiple
relations.
 It combines records from different relations that satisfy a given condition.

Steps performed in
Natural Description
Steps Join
Step – 1 It performs Cartesian Product
Step – 2 Then it deletes inconsistent tuples
Then it removes an attribute from duplicate
Step – 3
attributes

(DBMS)  Unit 1 – Overview and Architecture of


46
 Let r and s be relations on schemas R and S respectively.
 Then, r s is a relation on schema R  S obtained as follows:
 Consider each pair of tuples t from r and t from s.
r s
 If t and t have the same value on each of the attributes in R  S, add a tuple t to
r s
the result, where

t has the same value as tr on r

t has the same value as ts on s

 Example:
R = (A, B, C, D)
S = (E, B, D)
 Result schema = (A, B, C, D, E)
 r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B(DBMS)
 r.D =s.D (r1 –xOverview
Unit s)) and Architecture of
47
Relations r, s:

A B C D B D E

 1  a 1 a 
 2  a 3 a 
 4  b 1 a 
 1  a 2 b 
 2  b 3 b 
r s

r s
A B C D E
 1  a 
 1  a 
 1  a 
 1  a 
 2  b 

(DBMS)  Unit 1 – Overview and Architecture of


48
Natural Join / Inner Join Example
Exam Perform Natural Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
RN Nam Branc lt
RN SP ut
RN Nam Branc SP
o e h o I o e h I
10 Raju CE 10 8 10 Raju CE 8
1 1 To perform a Natural
1 Join there must be one
10 Mites ME 10 9 common attribute (column) between two
Steps
2 h performed in 3 relations.
Natural Join
Step:1 Perform Cross Step:2 Removes
Product
Student. Nam Branc Result. SP inconsistent
Student. Nam tuples
Branc Result. SP
RNo e h RNo I RNo e h RNo I
101 Raju CE 101 8 101 Raju CE 101 8
Step:3 Removes an attribute
101 Raju CE 103 9
from duplicate
RN Nam Branc SP
102 Mites ME 101 8 o e h I
h
10 Raju CE 8
102 Mites ME 103 9 1
h (DBMS)  Unit 2 – Introduction to Relational Model 49
Natural Join / Inner Join Example
Exam Perform Natural Join between Branch Answ (Branch)
ple and Faculty. er (Faculty)
Bran Facul Outp
ch
BI BNa ty
FI FNa BI ut
BI Bnam HO FI FNa
HOD
D me D me D D e D D me
1 CE Shah 10 Raj 1 1 CE Sha 10 Raj
1 h 1
2 ME Patel
10 Meet 2 2 ME Pate 10 Meet
3 l 2
To perform a Natural Join there must be one common
attribute (column) between two relations.

(DBMS)  Unit 2 – Introduction to Relational Model 50


Write down relational algebra for the following
tables/relations
 Relations
 Student (Rno, Sname, Address, City, Mobile)
 Department (Did, Dname)
 Academic (Rno, Did, SPI, CPI, Backlog)
 Guide (Rno, PName, Fid)
 Faculty (Fid, Fname, Subject, Did, Salary)

Exam List the name of students with their department name and SPI of all student
ple belong to “CE” department.
Answ ∏ Sname, Dname, (σDname=‘CE’ (Student (Department
er SPI Academic)))

Exam Display the name of students with their project name whose guide
ple is “Anil”.
Answ ∏ Sname, (σFname=‘Anil’ (Student (Guide
er Pname Faculty)))
(DBMS)  Unit 2 – Introduction to Relational Model 51
Exercise: Write down relational algebra for the following tables/relations

 Relations
 Student (Rno, Sname, Address, City, Mobile)
 Department (Did, Dname)
 Academic (Rno, Did, SPI, CPI, Backlog)
 Guide (Rno, PName, Fid)
 Faculty (Fid, Fname, Subject, Did, Salary)
 List the name of students with their department name having backlog 0.
 List the name of faculties with their department name and salary having salary
more than 25000 and belongs to “CE” department.
 List the name of all faculties of “CE” and “ME” department whose salary is more
than 50000.
 Display the students name with their project name of all “CE” department’s
students whose guide is “Z.Z. Patel”.
 Display the name of faculties with their department name who belongs to “CE”
department and tough “CPU” subject having salary more than 25000.
 List the name of students(DBMS)
with their department name doing project “Hackathon”
Unit 1 – Overview and Architecture of
52
4(a)Theta Join Operation
 The join operation allows us to combine a select operation and a
Cartesian-Product operation into a single operation.
 Consider relations r (R) and s (S)
 Let “theta” be a predicate on attributes in the schema R “union” S. The
join operation r s is defined as follows:

 Thus
 instructor.id = teaches.id (instructor x teaches ))
 Can equivalently be written as

instructor Instructor.id = teaches.id teaches.


(DBMS)  Unit 1 – Overview and Architecture of
53
5. Outer Join
 In natural join some records are missing, if we want that missing
records than we have to use outer join.

Three types of
Outer Join Sym
Sr. Outer Join
1 Left Outer Join bol
2 Right Outer Join
3 Full Outer Join

To perform a Outer Join there must be one common


attribute (column) between two relations.

(DBMS)  Unit 2 – Introduction to Relational Model 54


5(a) Left Outer Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 Operation:
 Display all the tuples of the left relation even though there is no matching tuple
in the right relation.
 For such kind of tuples having no matching, the attributes of right relation will be
padded with NULL in resultant relation.
Exam Perform Left Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc
SPI
No e h No I No e h
101 Raju CE 101 8 101 Raju CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result)
se (Student).
(DBMS)  Unit 1 – Overview and Architecture of
55
Left Outer Join Example
Exam Perform Left Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raju CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI
(Result))
Outp
ut
Roll Nam
SPI
No e
101 Raju 8
102 Meet NUL
L
(DBMS)  Unit 2 – Introduction to Relational Model 56
5(b) Right Outer Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-2
 Operation:
 Display all the tuples of right relation even through there is no matching tuple in
the left relation.
 For such kind of tuples having no matching, the attributes of left relation will be
padded with NULL in resultant relation.
Exam Perform Right Outer Join between Student Answ (Student)
ple and Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc SP
No e h No I No e h I
101 Raju CE 101 8 101 Raju CE 8
102 Meet ME 103 9 103 NUL NULL 9
L
Exerci What is the output of (Result)
se (Student).
(DBMS)  Unit 1 – Overview and Architecture of
57
Right Outer Join Example
Exam Perform Right Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raju CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI (Result))
Outp
ut
Roll Nam SP
No e I
101 Raju 8
103 NUL 9
L
(DBMS)  Unit 2 – Introduction to Relational Model 58
5(c) Full Outer Join
 Symbol:
 Notation: Relation-1 (R1) Relation-2 (R2) OR Algebra-1 Algebra-
2
 Operation:
 Display all the tuples of both of the relations. It also pads null values whenever
required. (Left outer join + Right outer join)
 For such kind of tuples having no matching, it will be padded with NULL in
Exam Perform Full Outer Join between Student Answ (Student)
pleresultant
and relation.
Result. er (Result)
Stud Resu Outp
ent
Roll Nam Branc lt
Roll SP ut
Roll Nam Branc
SPI
No e h No I No e h
101 Raju CE 101 8 101 Raju CE 8
102 Meet ME 103 9 102 Meet ME NUL
L
Exerci What is the output of (Result) 103 NUL NULL 9
se (Student). L
(DBMS)  Unit 1 – Overview and Architecture of
59
Full Outer Join Example
Exam Perform Full Outer Join between Student and Result. (Display
ple RollNo, Name and SPI)
Stud Resu
ent
Roll Nam Branc lt
Roll SP B
No e h No I L
101 Raju CE 101 8 1
102 Meet ME 103 9 0

Answ ∏ RollNo, Name, ((Student)


er SPI (Result))
Outp
ut
Roll Nam
SPI
No e
101 Raju 8
102 Meet NUL
L
103 NUL 9 (DBMS)  Unit 2 – Introduction to Relational Model 60
6. Set Operators
 Set operators combine the results of two or more queries into a
single result.

Three types of Set


Operators Sym
Sr. Set Operator
1 Union bol
U
Intersect /
2 ∩
Intersection
Minus / Set
3 −
difference
Conditi Set operators will take two or more queries as input, which must be
• Bothunion-compatible.
ons relation should have same (equal) number of
columns
• Corresponding attributes should have the same
data type or domain
(DBMS)  Unit 2 – Introduction to Relational Model 61
Conditions to perform Set Operators
Conditio Both relation should have same (equal) number
ns-1 of columns.
Stud Facul Stud Facul
ent
RN Nam Dep SP ty
FI Nam Dep ent
RN Nam Dep SP ty
FI Nam Dep Ex
o e t I d e t o e t I d e t p
10 Raj CE 8 10 Patel CE 10 Raj CE 8 10 Patel CE 5
1 1 1 1
10 Meet ME 9 10 Shah ME 10 Meet ME 9 10 Shah ME 3
2 2 2 2
10 Jay
Conditio CE 9 10 Dav
Corresponding ME should have
attributes 10theJay
sameCE 9 10 Dav ME 4
3
ns-2 data type.3 e 3 3 e
Stud Facul Stud Facul
ent
RN Nam Dep SP ty
FI Nam Dep ent
RN Nam Dep SP ty
FI Nam Dep Ex
Sub
o e t I d e t o e t I d e t p
10 Raj CE 8 10 Patel CE DS 10 Raj CE 8 10 Patel CE 5
1 1 1 1
10 Meet ME 9 10 Shah ME DBM 10 Meet ME 9 10 Shah ME 3
2 2 S 2 2
(DBMS)  Unit 1 – Overview and Architecture of
10 Jay CE 9 10 Dav ME DF 10 Jay CE 9 10 Dav ME 62 4
Set Operators [Exercise]

Exerci Check whether following tables are


se compatible or not:
• A: (First_name(char), Last_name(char), Date_of_Birth(date))
• B: (FName(char), LName(char), PhoneNumber(number))
Χ (Not compatible) Both tables have 3 attributes but third attributes datatype is
different.

• A: (First_name(char), Last_name(char), Date_of_Birth(date))


• B: (FName(char), LName(char), DOB(date))
 (Compatible) Both tables have 3 attributes and of same data type.

• Person (PersonID, Name, Address, Hobby)


• Professor (ProfessorID, Name, OfficeAddress, Salary)
• (Not compatible) Both tables have 4 attributes but forth attributes datatype is
∏ Name, Address
different.
& ∏ Name, OfficeAddress
(Person) (Professor)

• (Compatible) Both tables(DBMS)


have 2Unit
attributes and of same data type.
1 – Overview and Architecture of
63
6(a) Union Operator
 Symbol: U
 Notation: Relation-1 (R1) U Relation-2 (R2) OR Algebra-1 U Algebra-2
 Operation:
 It displays all the tuples/records belonging to the first relation (left relation) or the
second relation (right relation) or both.
 It also eliminates duplicate tuples (tuples present in both relations appear once).
Exam Perform Union between Customer and Answ (Customer) U
ple Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Manoj
Suresh Suresh Meet
Meet Manoj Raju
Suresh
Exerci Is there any difference in the output if we swap the tables in Union operator.
se (Employee) U (Customer).
(DBMS)  Unit 1 – Overview and Architecture of
64
6(b) Intersect/ Intersection Operator
 Symbol: ∩
 Notation: Relation-1 (R1) ∩ Relation-2 (R2) OR Algebra-1 ∩ Algebra-2
 Operation:
 It displays all the tuples/records belonging to both relations. OR
 It displays all the tuples/records which are common from both relations.

Exam Perform Intersection between Customer and Answ (Customer) ∩


ple Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Meet
Suresh Suresh Suresh
Meet Manoj

Exerci Is there any difference in the output if we swap the tables in Intersection.
se (Employee) ∩ (Customer).
(DBMS)  Unit 1 – Overview and Architecture of
65
6(c) Minus/ Set difference Operator
 Symbol: −
 Notation: Relation-1 (R1) − Relation-2 (R2) OR Algebra-1 − Algebra-2
 Operation:
 It displays all the tuples/records belonging to the first relation (left relation) but not in
the second relation (right relation).

Exam Perform Set difference between Customer and Answ (Customer) −


ple Employee. er (Employee)
Custo Emplo Outp
mer yee ut
Name Name Name
Raju Meet Raju
Suresh Suresh
Meet Manoj

Exerci Is there any difference in the output if we swap the tables in Set difference.
se (Employee) − (Customer).
(DBMS)  Unit 1 – Overview and Architecture of
66
Union Operators Example

Exam Display Name of person who are either employee or


ple customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Answ ∏ Name (Customer) U ∏ Name
er (Employee)
Outp
ut
Name
Manoj
Raju
Suresh

(DBMS)  Unit 2 – Introduction to Relational Model 67


Intersect/ Intersection Operators Example

Exam Display Name of person who are employee as well


ple as customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Answ ∏ Name (Customer) ∩ ∏ Name
er (Employee)
Outp
ut
Name
Suresh

(DBMS)  Unit 2 – Introduction to Relational Model 68


Minus/ Set difference Operators Example

Exam Display Name of person who are employee but


ple not customer.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Answ ∏ Name (Employee) − ∏ Name
er (Customer)
Outp
ut
Name
Manoj

(DBMS)  Unit 2 – Introduction to Relational Model 69


Minus/ Set difference Operators Example

Exam Display Name of person who are customer but


ple not employee.
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Answ ∏ Name (Customer) − ∏ Name
er (Employee)
Outp
ut
Name
Raju

(DBMS)  Unit 2 – Introduction to Relational Model 70


Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Algebr ∏ ID, Name(Customer) U ∏ ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) U ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ ID, Name(Customer) ∩ ∏ ID, Name
a-3 (Employee)
Algebr ∏ ID, Name, Balance (Customer) ∩ ∏ ID, Name, Salary
a-4 (Employee)

(DBMS)  Unit 2 – Introduction to Relational Model 71


Set Operators [Exercise]
Exerci What is the output of following relational algebra for the below
se mentioned tables:
Custo Emplo
mer
I Nam Balan yee
I Nam De Sala
D e ce D e pt ry
1 Raju 10000 2 Sures CE 8000
h
2 Sures 20000
h 3 Mano ME 9000
j
Algebr ∏ ID, Name(Customer) − ∏ ID, Name
a-1 (Employee)
Algebr ∏ ID, Name, Balance (Customer) − ∏ ID, Name, Salary
a-2 (Employee)
Algebr ∏ ID, Name(Employee) − ∏ ID, Name
a-3 (Customer)
Algebr ∏ ID, Name, salary(Employee) − ∏ ID, Name, balance
a-4 (Customer)

(DBMS)  Unit 2 – Introduction to Relational Model 72


6(d) Division Operator
 Symbol: ÷ (Division)
 Notation: Relation1 (R1) ÷ Relation2 (R2) OR Algebra1 ÷ Algebra2
 Condition:
 Attributes of relation2/algebra2 must be a proper subset of attributes of
relation1/algebra1.
 Operation:
 The output of the division operator will have attributes =
All attributes of relation1 – All attributes of relation2
 The output of the division operator will have tuples =
Tuples in relation1, which are associated with the all tuples of relation2.

(DBMS)  Unit 1 – Overview and Architecture of


73
Division Operator Example
Exam Perform Division operation between Student Answ (Student) ÷
ple and Subject. er (Subject)
Stude Subje Outp
nt ct ut
Nam Subje Subjec Name
e ct t Rohit
Raj DBMS DBMS
Suresh
Raj DS DS
Meet DS DF
Meet DF
Rohit DBMS
Rohit DS
Rohit DF
Sures DBMS
h
Sures DF
h
Sures DS (DBMS)  Unit 2 – Introduction to Relational Model 74
Division Operator Example

A B B B B
Sn 1 2 3 4
PNo PNo PNo PNo PNo
o
P2 P2 P1 P2
S1 P1
P4 P2 P5
S1 P2
P4
S1 P3
Alge (A) ÷ Alge (A) ÷ Alge (A) ÷ Alge (A) ÷
S1 P4
bra (B1) bra (B2) bra (B3) bra (B4)
S2 P1 Outp Outp Outp Outp
ut ut ut ut
S2 P2 SNo SNo SNo SNo
S3 P2 S1 S1 S1
S4 P2 S2 S4
S4 P4 S3
S5 P4 S4

(DBMS)  Unit 2 – Introduction to Relational Model 75


Division Operator Example
Exam List the name of students doing a project in all
ple technologies.
Stude Proje
nt
RN Nam Technol ct
TI Technol
o e ogy D ogy
10 Raj .NET 1 .NET
1
2 PHP
10 Raj PHP
1 3 Android
10 Meet .NET 4 iPhone
2
Answ ∏ Name, Technology (Student) ÷ ∏ Technology
10 Meet PHP er (Project)
2 Outp
10 Meet iPhone ut
Name
2
Meet
10 Meet Android
2
10 Rohit Android
3 (DBMS)  Unit 2 – Introduction to Relational Model 76
Rename Operator
 Symbol: ρ (Rho)
 Notation: ρ
A (X1,X2….Xn) (Relation)

 Operation:
 The rename operation is used to rename the output relation.
 The result of rename operator are also relations with new name.
 The original relation name can not be changed when we perform rename
operation on any relation.
 How to use:
 ρ x (E)
Returns a relation E under a new name X.
 ρ A1, A2. …,An (E)
Returns a relation E with the attributes renamed to A1, A2, …., An.
 ρ x(A1, A2. …,An) (E)
Returns a relation E under a new name X with the attributes renamed to A1, A2,
…., An.
(DBMS)  Unit 1 – Overview and Architecture of
77
Rename Operator Example
Exam Rename Exam Rename
ple table ple attributes
Stud Stud
ent ent
Rn Na CP
RN Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 AlgeJay ρ(RollNo,
10 7 StudentName, SPI)
3
bra (Student) 3
bra (Student)
Perso Stud
n ent
Roll StudentN SP
RN Na CP
o me I No ame I
10 Raj 8 101 Raj 8
1
102 Meet 9
10 Meet 9
2 103 Jay 7
10 Jay 7 (DBMS)  Unit 2 – Introduction to Relational Model 78
Rename Operator Example
Exam Rename table and Exam Rename particular
ple attributes both ple attributes
Stud Stud
ent
Rn Na CP ent
Rn Na CP
o me I o me I
10 Raj 8 10 Raj 8
1 1
10 Meet 9 10 Meet 9
2 2
AlgeJay ρPerson
10 7 (RollNo, StudentName) (∏ RNo, Name AlgeJay ρStudentName
10 7 / Name
3
bra (Student)) 3
bra (Student)
Perso Stud
n
Roll StudentN ent
Rn StudentN CP
No ame o ame I
101 Raj 10 Raj 8
102 Meet 1
10 Meet 9
103 Jay 2
(DBMS)  Unit 2 – Introduction to Relational Model 79
Rename Operator Example
Exam Find out maximum CPI from Step- ρA (Student) X ρB
ple student table. 1 (Student)
Stud Outpu
ent
Rn Na CP t-1
A.Rn A.Na A.C B.Rn B.Na B.C
o me I o me PI o me PI
10 Raj 8 101 Raj 8 101 Raj 8
1
101 Raj 8 102 Meet 9
10 Meet 9
2 101 Raj 8 103 Jay 7
10 Jay
Step- 7σ
A.CPI<B.CPI (ρA (Student) X ρB 102 Meet 9 101 Raj 8
3
2 (Student)) 102 Meet 9 102 Meet 9
Outpu
t-2 102 Meet 9 103 Jay 7
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI 103 Jay 7 101 Raj 8
101 Raj 8 102 Meet 9 103 Jay 7 102 Meet 9
103 Jay 7 101 Raj 8 103 Jay 7 103 Jay 7
103 Jay 7 102 Meet 9
(DBMS)  Unit 2 – Introduction to Relational Model 80
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay
Step- 7σ
A.CPI<B.CPI (ρA (Student) X ρB
3
2 (Student))
Outpu
t-2
A.Rn A.Na A.C B.Rn B.Na B.C
o me PI o me PI
101 Raj 8 102 Meet 9
103 Jay 7 101 Raj 8
103 Jay 7 102 Meet 9
(DBMS)  Unit 1 – Overview and Architecture of
81
Rename Operator Example
Exam Find out maximum CPI from
ple student table.
Stud
Step- ∏ A.CPI (σA.CPI<B.CPI (ρA (Student) X ρB
ent
Rn Na CP 3 (Student)))
o me I Outpu
10 Raj 8 t-3
A.CPI
1
8
10 Meet 9
2 7
10 Jay 7
Step-
3 ∏ CPI (Student) − ∏ A.CPI (σA.CPI<B.CPI (ρ A (Student) X ρB
4
(Student)))
Stud Outpu Outpu
ent t-3 t
CPI A.CPI CPI
8 − 8 = 9
9 7
7
(DBMS)  Unit 1 – Overview and Architecture of
82
Aggregate Functions
 Symbol: g or G
 Notation: g (Relation)
function-name(column), function-name(column), …, function-name(column)

 Operation:
 It takes a more than one value as input and returns a single value as output
(result).
 Aggregate functions are:
 Sum (It returns the sum (addition) of the values of a column.)
 Max (It returns the maximum value for a column.)
 Min (It returns the minimum value for a column.)
 Avg (It returns the average of the values for a column.)
 Count (It returns total number of values in a given column.)

(DBMS)  Unit 1 – Overview and Architecture of


83
Aggregate Functions Example
Stud
ent Exam Find out sum of CPI of all Outpu
Rn Bran Semes CP ple students. t
Name sum
o ch ter I
Answ g sum(CPI)
10 Rames CE 3 9 73
er (Student)
1 h
10 Mahes EC 3 8 Exam Find out maximum & Outpu
2 h ple minimum CPI. tma mi
10 Suresh ME 4 7 Answ g max(CPI), min(CPI) x n
3 er (Student) 9 7
10 Amit EE 4 8 Exam Count the number of Outpu
4 ple students. t
count
10 Anita CE 4 8 Answ g count(Rno)
5 9
er (Student)
10 Reeta ME 3 7
Exam Find out average of CPI of all Outpu
6
ple students. t
avg
10
7
Rohit EE 4 9
Answ g avg(CPI) (Student)
8.11
er
10 Chetan CE 3 8
8 (DBMS)  Unit 2 – Introduction to Relational Model 84
Assignment Operator
 It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables.
 The assignment operation is denoted by  and works like assignment in
a programming language.
 Example: Find all instructor in the “Physics” and Music department.

Physics   dept_name=“Physics” (instructor)


Music   dept_name=“Music” (instructor)
Physics  Music
 With the assignment operation, a query can be written as a sequential
program consisting of a series of assignments followed by an expression
whose value is displayed as the result of the query.

(DBMS)  Unit 1 – Overview and Architecture of


85
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.

(DBMS)  Unit 2 – Introduction to Relational Model 86


Example: Find information about courses taught by instructors in the
Physics department
 Query 1
dept_name=“Physics” (instructor instructor.ID = teaches.ID teaches)

 Query 2
(dept_name=“Physics” (instructor)) instructor.ID = teaches.ID teaches

 The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.

(DBMS)  Unit 2 – Introduction to Relational Model 87


Exercise
1. Consider the relations r1(P, Q, R) and r2(R, S, T) with primary keys P and R
respectively. The relation r1 contains 2000 tuples and r2 contains 2500
tuples. The maximum size of the join r1⋈ r2 is.
(A) 2000
(B) 2500
(C) 4500
(D) 5000

2. Consider a database table T containing two columns X and Y each of type


integer. After the creation of the table, one record (X=1, Y=1) is inserted in
the table.
Let MX and MY denote the respective maximum values of X and Y among all records in the table at
any point in time. Using MX and MY, new records are inserted in the table 128 times with X and Y
values being MX+1, 2*MY+1 respectively. It may be noted that each time after the insertion, values
of MX and MY change. What will be the output of the following SQL query after the steps mentioned
above are carried out?
SELECT Y FROM T(DBMS)
WHERE X=7;
 Unit 2 – Introduction to Relational Model 88
Exercise 1
employee (name, street, city)
works (name, cname, salary)
company (cname, city)

1.Find the names of all employees who live in city “Miami”.

2.Find the names of all employees whose salary is greater than $100,000.

3.Find the names of all employees who live in “Miami” and whose salary is greater than
$100,000.

(DBMS)  Unit 2 – Introduction to Relational Model 89


Exercise 2

(DBMS)  Unit 2 – Introduction to Relational Model 90


(DBMS)  Unit 2 – Introduction to Relational Model 91
Example 3
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

• Find the names of all branches located in “Chicago”.

• Find the names of all borrowers who have a loan in branch “Downtown”.

(DBMS)  Unit 2 – Introduction to Relational Model 92


branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

 Find all loans of over $1200 amount > 1200 (loan)

 Find the loan number for each loan of an amount loan-number (amount > 1200 (loan))
greater than $1200

 Find the names of all customers who have a loan, an account, or both,
from the bank
customer-name (borrower)  customer-name (depositor)
(DBMS)  Unit 2 – Introduction to Relational Model 93
branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-only)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

 Find the names of all customers who have a loan and an account at bank .

customer-name (borrower)  customer-name (depositor)

 Find the names of all customers who have a loan at the Perryridge branch.

customer-name (branch-name=“Perryridge” (borrower.loan-number = loan.loan-number (borrower x loan)))

(DBMS)  Unit 2 – Introduction to Relational Model 94


 Find the largest account balance( Rename account relation as d )

balance(account) - account.balance (account.balance < d.balance (account x rd (account)))

1 A 50 1 A 50
0 0
2 B 30 2 B 30
0 0
3 account
C 70 3d(account)
C 70
0 0 1 A 50 1 A 50
1 A 50 1 A 50
0 0 0 0
1 A 50 2 B 30 1 A 50 2 B 30
0 0 0 0
1 A 50 3 C 70 1 A 50 3 C 70
0 0 0 0
account x d
2 B 30 1 A 50 2 B 30 1 A 50
0 0 0 0
2 B 30 2 B 30 2 B 30 2 B 30
0 0 0 0
2 B 30 3 C 70 2 B 30 3 C 70
0 0 0 0
3 C 70 1 A 50 3 C 70 1 A 50
0 (DBMS)
0  Unit 2 – Introduction to Relational Model 0 0 95
Example 4
Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost)

(DBMS)  Unit 2 – Introduction to Relational Model 96


Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost)

(DBMS)  Unit 2 – Introduction to Relational Model 97


Suppliers(sid, sname, address)
Parts(pid, pname, color)
Catalog(sid, pid, cost)

(DBMS)  Unit 2 – Introduction to Relational Model 98


Relational Algebra [Exercise-1]
 Write down relational algebras for the following table:
 Employee (person-name, street, city)
 Works (person-name, company-name, salary)
 Company (company-name, city)
 Managers (person-name, manager-name)
 Find the names of all employees who work for “TCS”.
 Find the names and cities of residence of all employees who work for “Infosys”.
 Find the names, street and city of residence of all employees who work for “ITC” and
earn more than $10,000 per annum.
 Find the names of all employees in this database who live in the same city as the
company for which they work.
 Find the names of all employees working in “TCS” who earn more than 25000 and
less than 40000.
 Find the name of employee whose manager is “Ajay Patel” and salary is more than
50000.
 Display the name of employee with street, city, company name, salary and manager
name staying in “Rajkot” and working in “Ahmedabad”.
 Find maximum, minimum and average salary of all employee.
 Find out the total number
(DBMS)  Unit 1 – Overview and Architecture of
of employee. 99
Relational Algebra [Exercise-2]
Reserves(sid, bid, day)
Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

1. Find names of sailors who have reserved boat #103


2. Find names of sailors who have reserved a red boat
3. Find the colors of boats reserved by Lubber
4. Find the names of sailors who have reserved at least one boat
5. Find the names of sailors who have reserved a red or a green boat
6. Find the names of sailors who have reserved a red and a green boat
7. Find the sids of sailors with age over 20 who have not reserved a red boat

(DBMS)  Unit 2 – Introduction to Relational Model 100


Relational Algebra [Exercise-3]
Reserves(sid, bid, day)
Sailors(sid, sname, rating, age)
Boats(bid, bname, color)

(DBMS)  Unit 2 – Introduction to Relational Model 101

You might also like