0% found this document useful (0 votes)
6 views36 pages

Unit 4

Uploaded by

singhmalay12345
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)
6 views36 pages

Unit 4

Uploaded by

singhmalay12345
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/ 36

Faculty of Engineering and Technology

Parul Institute of Technology


Department of Computer Science & Engineering
Course: B.Tech – CSE
Subject: Database Management System
Unit 4
Relational Data Model
:Overview:
• What is Relational Data Model?
• Example of Some Relations
• Degree
• Cardinality
• Types of Cardinality
• What is Relational Algebra?
• Select Operation
• Project Operation
• Cartesian Product Operation
• Set Operations
• Assignment Operation
• Join Operations
What is Relational Data Model?
• The relational model is defined as a collection of tables to represent both data and the
relationships among those data.
• Tables are known as Relations. Each table has multiple rows and columns, and each column has a
unique name. Columns are known as Attributes. Rows are known as Tuples.
• In this Employee relation, there are four attributes named ID, Name, Department and Salary.
ID Name Department Salary
28283 A CSE 30000
32442 B EE 35000
43553 C MT 53000
21786 D ME 18000
45753 E ASH 45000
17843 F CE 63000
:Example of Some Relations:
:Degree:
• The degree is the number of entities involved in a particular relationship. It refers to the
number of different attributes or columns in a table.
• The degree of relationship can be categorized as Unary, Binary, Ternary & N-
ary relationship.
• In a unary relationship, only one entity is involved. Here, the degree of relationship is 1.
• In a binary relationship, there are two entities involved. The degree of relationship is 2.
• In a ternary relationship, there are three entities involved. The degree of relationship is
3.
• In an N-ary relationship, there is an n number of involved entities. The degree of
relationship is 'n'.
:Cardinality:
• The uniqueness of data present in a column is called “Cardinality”.
• A column with a high cardinality means that a significant
percentage of the values are unique.
• Low cardinality refers to a column with a large number of repeated
values.
• A greater cardinality indicates more distinct values, leading to
improved selectivity and faster query execution.
• A reduced cardinality, may suggest a limited number of distinct
values, allowing the database to use specialized optimization
techniques.
:Types of Cardinality:
• One-to-One cardinality (1:1)

• One-to-Many cardinality (1:m)


:Types of Cardinality:
• Many-to-One cardinality (m:1)

• Many-to-Many cardinality (m:m)


What is Relational Algebra?
• The Relational Algebra consists of a set of operations that take one or two relations as input and
produce a new relation as their result.
• Unary Operations: They operate on one relation.
1. Select Operation
2. Project Operation
• Binary Operations: They operate on more than one relations.
1. Cartesian Product Operation
2. Join Operations
3. Set Operations
4. Assignment Operation
:Select Operation:
• The select operation selects tuples that satisfy a given predicate.
• The lowercase Greek letter sigma (σ) is used to denote selection.
• The predicate appears as a subscript to σ.
• The argument relation is in parentheses after the σ.
• It is allowed to use =, ≠, <, ≤, >, ≥, and (∧), or (∨), and not (~) in the selection
predicate.
:Examples of Select Operation:
• To select tuples of the instructor relation where the instructor is in the “Physics” department, the
following statement is written:

σ dept name =“Physics” (instructor)

Output
:Examples of Select Operation:
• To select all instructors with salary greater than $90,000, the following statement is written:

σ salary >90000 (instructor)

Output
:Examples of Select Operation:
• To select instructors in Physics with salary greater than $90,000, the following statement is written:

σ dept name =“Physics” ^ salary >90000 (instructor)

Output
:Project Operation:
• The project operation is a unary operation that returns its argument relation,
with certain attributes left out.
• Projection is denoted by the uppercase Greek letter pi (Π).
• We list those attributes that we wish to appear in the result as a subscript to Π.
:Examples of Project Operation:
• To select specified attributes like ID, name and salary of the instructor relation, the following statement is
written:

Π ID, name, salary (instructor)

Output
:Examples of Project Operation:
• To get the monthly salary of each instructor, the following statement is written:

ΠID, name, salary/12 (instructor)

Output
:Select Operation + Product Operation:
• Consider the more complicated query - “Find the names of all instructors in the Physics department.” We
write:
Πname (σdept name =“Physics” (instructor))

Output
:Cartesian Product Operation:
• The Cartesian product operation is denoted by a cross (×), allows us to combine information from any two
relations.
• Consider two relations named drinks and meals.

• The Cartesian product of these relations is drinks × meals.


:Set Operations - Union:
• Consider a query to find the set of all courses taught in the Fall 2017 semester or the Spring 2018 semester.
The information is contained in the section relation.
• To find the set of all courses taught in the Fall 2017 semester, we write:
Πcourse id (σsemester =“Fall”∧ year=2017 (section))
• To find the set of all courses taught in the Spring 2018 semester, we write:
Πcourse id (σsemester =“Spring”∧ year=2018 (section))
• Now, we need the union of these two sets; that is, we need all course ids that appear in either or both of the
two relations.
Πcourse id (σsemester =“Fall”∧ year=2017 (section)) ∪ Πcourse id (σsemester =“Spring”∧ year=2018 (section))
:Set Operations - Intersection:
• Consider a query to find the set of all courses taught in the Fall 2017 semester and the Spring 2018 semester.
The information is contained in the section relation.
• To find the set of all courses taught in the Fall 2017 semester, we write:
Πcourse id (σsemester =“Fall”∧ year=2017 (section))
• To find the set of all courses taught in the Spring 2018 semester, we write:
Πcourse id (σsemester =“Spring”∧ year=2018 (section))
• Now, we need the intersection of these two sets; that is, we need all course ids that appear in either or both
of the two relations.
Πcourse id (σsemester =“Fall”∧ year=2017 (section)) ∩ Πcourse id (σsemester =“Spring”∧ year=2018 (section))
:Set Operations - Minus:
• We can find all the courses taught in the Fall 2017 semester but not in Spring 2018 semester by writing the
following set-difference operation:

Πcourse id (σsemester =“Fall”∧ year=2017 (section)) - Πcourse id (σsemester =“Spring”∧ year=2018 (section))
:Assignment Operation:
• It is convenient at times to write a relational-algebra expression by assigning parts of it to temporary relation
variables.
• The assignment operation, denoted by ←. It is a convenient way to express complex queries.

courses fall 2017 ← Πcourse id(σsemester =“Fall”∧ year=2017 (section))

courses spring 2018 ← Πcourse id(σsemester =“Spring” ∧ year=2018 (section))

courses fall 2017 ∩ courses spring 2018


:Join Operations:
• The join operation allows us to combine a selection and a
Cartesian product into a single operation. Join operation is
denoted by “⋈”.
• Consider relations r(R) and s(S), and let θ be a predicate on
attributes in the schema R ∪ S. The join operation is defined
as follows:
r ⋈ s = σ (r × s)
θ θ
:Types of Join Operations:
Join

Natural Join ⋈ Theta Join ⋈ θ

Inner Join ⋈ Outer Join

Left Join or Left Outer Join ⋈

Right Join or Right Outer Join ⋈

Full Join or Full Outer Join ⋈


ID NAME DEPARTMENT SALARY ID COURSE_ID SEMESTER
1 A CSE 30000 1 101 1
2 B CSE 35000 2 203 1
3 C EE 15000 6 201 2
6 D MT 50000 8 304 1
7 E ME 36000 9 495 1
9 F CSE 56000 10 320 2

INSTRUCTOR RELATION TEACHES RELATION


:Example of Inner Join Operation:
• If we want to join instructor and teaches relations, we have to find out
on the bases of which attribute we can join those relations. ID
attribute is common and using it we can join these two relations. The
following syntax can be used for inner join:

σinstructor.ID=teaches.ID(instructor × teaches)
or
(instructor) ⋈instructor.ID=teaches.ID (teaches)
• Output of Inner Join:

ID NAME DEPARTMENT SALARY COURSE_ID SEMESTER


1 A CSE 30000 101 1
2 B CSE 35000 203 1
6 D MT 50000 201 2
9 F CSE 56000 495 1
:Example of Left/Left Outer Join Operation:
• The following syntax can be used for Left/Left Outer Join:

σinstructor.ID=teaches.ID(instructor × teaches)
or
(instructor) ⋈instructor.ID=teaches.ID (teaches)
• Output of Left/Left Outer Join:

ID NAME DEPARTMENT SALARY COURSE_ID SEMESTER


1 A CSE 30000 101 1
2 B CSE 35000 203 1
3 C EE 15000 NULL NULL
6 D MT 50000 201 2
7 E ME 36000 NULL NULL
9 F CSE 56000 495 1
:Example of Right/Right Outer Join Operation:
• The following syntax can be used for Right/Right Outer Join:

σinstructor.ID=teaches.ID(instructor × teaches)
or
(instructor) ⋈ instructor.ID=teaches.ID (teaches)
• Output of Right/Right Outer Join:

ID NAME DEPARTMENT SALARY COURSE_ID SEMESTER


1 A CSE 30000 101 1
2 B CSE 35000 203 1
6 D MT 50000 201 2
8 NULL NULL NULL 304 1
9 F CSE 56000 495 1
10 NULL NULL NULL 320 2
:Example of Full/Full Outer Join Operation:
• The following syntax can be used for Full/Full Outer Join:

σinstructor.ID=teaches.ID(instructor × teaches)
or
(instructor) ⋈ instructor.ID=teaches.ID (teaches)
• Output of Full/Full Outer Join:

ID NAME DEPARTMENT SALARY COURSE_ID SEMESTER


1 A CSE 50000 101 1
2 B CSE 10000 201 1
3 C EE 15000 303 2
6 D MT 20000 507 1
7 E ME 25000 NULL NULL
8 NULL NULL 35000 706 1
9 F CSE NULL 907 2
10 NULL NULL NULL NULL NULL
:Short Questions:
1. Define Relational Data Model or Relational Model.
2. Explain Relational Model with a suitable example.
3. Define Degree and explain types of degree.
4. Define Cardinality and explain types of cardinality.
5. Define Relational Algebra.
6. Why Select Operation is used? Which symbol is used as a select
operation notation? Explain with a suitable example.
7. Why Project Operation is used? Which symbol is used as a select
operation notation? Explain with a suitable example.
:Short Questions:
8. Explain Cartesian Product Operation with a suitable example.
9. Why Assignment Operator is used?
10. Enlist Set Operations and explain with suitable example.
11. Give classification of Join Operations.
12. Explain Inner Join with a suitable example.
13. Explain Left Join with a suitable example.
14. Explain Right Join with a suitable example.
15. Explain Full Join with a suitable example.

You might also like