MATRUSRI ENGINEERING COLLEGE�DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SUBJECT NAME: DataBase Management Systems
FACULTY NAME: K Sunil Manohar Reddy
Insert Your Photo here🡪
MATRUSRI
ENGINEERING COLLEGE
DATABASE MANAGEMENT SYSTEMS
COURSE OBJECTIVES:
COURSE OUTCOMES:
MATRUSRI
ENGINEERING COLLEGE
INTRODUCTION: ��THIS UNIT DEALS WITH THE RELATIONAL MODEL AND STORING, ACCESSING AND MANAGING DATA USING VARIOUS FORMAL AND COMMERCIAL QUERY LANGUAGES.
UNIT-II
OUTCOMES:
Upon completion of this unit, student will be able to:
MATRUSRI
ENGINEERING COLLEGE
CONTENTS:�RELATIONAL MODEL: STRUCTURE OF RELATIONAL DATABASES, FUNDAMENTAL RELATIONAL-ALGEBRA OPERATIONS, ADDITIONAL RELATIONAL-ALGEBRA OPERATIONS, EXTENDED RELATIONAL-ALGEBRA OPERATIONS, NULL VALUES, MODIFICATION OF THE DATABASES ��
OUTCOMES:
Upon completion of this module, student will be able to:
MODULE-I
MATRUSRI
ENGINEERING COLLEGE
Relational Model
Relational Model represents the database as a collection of relations.
Attribute: It contains the name of a column in a particular table.
Domain: It contains a set of atomic values that an attribute can take.
Degree of a Relation: The number of the attributes in a given relation is called degree of the relation.
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.
MATRUSRI
ENGINEERING COLLEGE
Relational Schema
MATRUSRI
ENGINEERING COLLEGE
Structure of Relational Database
For basic structure, consider the deposit table of the following figure:
MATRUSRI
ENGINEERING COLLEGE
Keys in Relational Database
Note: Subset of Candidate keys is not a Primary Key.
MATRUSRI
ENGINEERING COLLEGE
Fundamental Relational-Algebra Operations
Relational Algebra is a procedural language consisting of a set of operations that take one or two relations as input and produce a new relation as their result.
Six basic operators
MATRUSRI
ENGINEERING COLLEGE
Select Operation
The select operation selects tuples that satisfy a given predicate.
Notation: σ p (r)
p is called the selection predicate
Example: select those tuples of the instructor relation where the instructor is in the “Physics” department.
Query: σ dept_name=“Physics” (instructor)
Result:
MATRUSRI
ENGINEERING COLLEGE
Select Operation
Example: Find the instructors in Physics with a salary greater $90,000, we write:
σ dept_name=“Physics” ∧ salary > 90,000 (instructor)
Example: Find all departments whose name is the same as their building name:
σ dept_name=building (department)
MATRUSRI
ENGINEERING COLLEGE
Project Operation
Notation:
∏ A1,A2,A3 ….Ak (r)
where A1, A2, …, Ak are attribute names and r is a relation name.
MATRUSRI
ENGINEERING COLLEGE
Cartesian-Product Operation
The Cartesian-product operation (denoted by X) allows us to combine information from any two relations.
Example: the Cartesian product of the relations instructor and teaches is written as:
instructor X teaches
We construct a tuple of the result out of each possible pair of tuples: one from the instructor relation and one from the teaches relation (see next slide)
Since the instructor ID appears in both relations we distinguish between these attribute by attaching to the attribute the name of the relation from which the attribute originally came.
instructor.ID
teaches.ID
MATRUSRI
ENGINEERING COLLEGE
The instructor X teaches table
MATRUSRI
ENGINEERING COLLEGE
Join Operation
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
σ instructor.id = teaches.id (instructor x teaches ))
We get only those tuples of “instructor X teaches” that pertain to instructors and the courses that they taught.
MATRUSRI
ENGINEERING COLLEGE
Join Operation
σ instructor.id = teaches.id (instructor x teaches))
�
MATRUSRI
ENGINEERING COLLEGE
Union Operation
The union operation allows us to combine two relations
Notation: r ∪ s
For r ∪ s to be valid:
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column of r deals with the same type of values as does the
2nd column of s)
�
MATRUSRI
ENGINEERING COLLEGE
Union Operation
Example: to find all courses taught in the Fall 2017 semester, or in the Spring 2018 semester, or in both.
Query:
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ∪ � ∏course_id (σ semester=“Spring” Λ year=2018 (section))
Result:
MATRUSRI
ENGINEERING COLLEGE
Set Difference Operation
The set-difference operation allows us to find tuples that are in one relation but are not in another.
Notation: r – s
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Example: To find all courses taught in the Fall 2017 semester, but not in the Spring 2018 semester.
Query: ∏course_id (σ semester=“Fall” Λ year=2017 (section)) − � ∏course_id (σ semester=“Spring” Λ year=2018 (section))
Result:
MATRUSRI
ENGINEERING COLLEGE
The Rename Operation
The results of relational-algebra expressions do not have a name that we can use to refer to them. The rename operator, ρ , is used for that purpose
The expression ρx (E) returns the result of expression E under the name x
Another form of the rename operation:
ρx(A1,A2, .. An) (E)
MATRUSRI
ENGINEERING COLLEGE
Additional Relational-Algebra Operations
MATRUSRI
ENGINEERING COLLEGE
Intersection Operation
Example: to find all courses taught in the Fall 2017 semester, or in the Spring 2018 semester, or in both.
Query:
∏course_id (σ semester=“Fall” Λ year=2017 (section)) ν � ∏course_id (σ semester=“Spring” Λ year=2018 (section))
Result:
MATRUSRI
ENGINEERING COLLEGE
NULL Values
It is possible for tuples to have a null value, denoted by null, for some of their attributes. null signifies an unknown value or that a value does not exist.
Arithmetic Operations and Comparisons with null values return the special truth value unknown or null.
For logical operators with an input as null:
OR: (unknown or true) = true, (unknown or false) = unknown, (unknown or unknown) = unknown
AND: (unknown and true) = unknown, (unknown and false) = false, (unknown and unknown) = unknown
NOT: (not unknown) = unknown
MATRUSRI
ENGINEERING COLLEGE
Modifying the Database
The content of the database may be modified using the following operations:
MATRUSRI
ENGINEERING COLLEGE