0% found this document useful (0 votes)
122 views

Unit 2 - Database Management System - WWW - Rgpvnotes.in

This document discusses relational algebra and relational databases. It defines key concepts like domains, attributes, tuples, relations, relation schemas, and integrity constraints. It also covers relational algebra operators and how they are used to perform queries on relations. Relational algebra provides a foundation for relational operations and query processing in relational database management systems.

Uploaded by

Gourav CHOUDHURY
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)
122 views

Unit 2 - Database Management System - WWW - Rgpvnotes.in

This document discusses relational algebra and relational databases. It defines key concepts like domains, attributes, tuples, relations, relation schemas, and integrity constraints. It also covers relational algebra operators and how they are used to perform queries on relations. Relational algebra provides a foundation for relational operations and query processing in relational database management systems.

Uploaded by

Gourav CHOUDHURY
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/ 16

Program : B.

Tech
Subject Name: Database Management System
Subject Code: IT-405
Semester: 4th
Downloaded from be.rgpvnotes.in

UNIT-II

Relational algebra is a procedural query language, which takes instances of relations as input and yields
instances of relations as output. It uses operators to perform queries. An operator can be either unary or
binary. They accept relations as their input and yield relations as their output. Relational algebra is
performed recursively on a relation, and intermediate results are also considered relations.

In the formal relational model terminology, a row is called a tuple, a column header is called an attribute,
and the table is called a relation. A domain of possible values represents the data type describing the types
of values that can appear in each column. We now define these terms—domain, tuple, attribute, and relation
formally.

Domains, Attributes, Tuples, and Relations


Deposit Relation Customer Relation
bname Account Ename Balance Ename Street City
Bhanwarkuwan SBI1200 Ram 5000 Ramesh MG road Indore
Tilak Nagar SBI1238 Amit 1000 Jhon RNT Marg Indore

 It has four attributes.


 For each attribute, there is a permitted set of values, called the domain of that attribute.
 E.g. the domain of name is the set of all branch names.
Let/denote the domain of name, and , and the remaining attributes' domains respectively.

A domain D is a set of atomic values. By atomic we mean that each value in the domain is indivisible as far
as the formal relational model is concerned. A common method of specifying a domain is to specify a data
type from which the data values are forming the domain are drawn. It is also useful to specify a name for
the domain, to help in interpreting its values. Some examples of domains follow:

● Usa_phone_numbers. The set of ten-digit phone numbers valid in the United States.
● Local_phone_numbers. The set of seven-digit phone numbers valid within an area code in the United
States. The use of local phone numbers is quickly becoming obsolete, being replaced by standard ten-
digit numbers.

A relation schema R, denoted by R (A 1, A 2, A n ), is


made up of a relation name R and a list of attributes, A 1, A 2,..., A n. Each attribute A i is the name of a
role played by some domain D in the relation schema R. D is called the domain of A i and is denoted by
dom (A i). A relation schema is used to describe a relation R is called the name of this relation. The degree
(or arity) of a relation is the number of attributes n of its relation schema.

Using the data type of each attribute, the definition is sometimes written as:

STUDENT (Name: string, Ssn: string, Homophone: string, Address: string, Office phone: string, Age: integer,
Gpa: real)

Characteristics of Relations
The Ordering of Tuples in a Relation. A relation is defined as a set of tuples. Mathematically, elements of a
set have no order among them hence, tuples in a relation do not have any order.

The ordering of Values within a Tuple and an Alternative Definition of a Relation. According to the other
definition of a relation, an n-tuple is an ordered list of n values, so the ordering of values in a tuple and hence
of attributes in a relation schema is essential.

Page no: 1 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Values and NULLs in the Tuples. Each value in a tuple is an atomic value that is; it is not divisible into
components within the framework of the underlying relational model. Hence, composite and multivalued
attributes are not allowed. An important concept is that of NULL values, which are used to represent the
values of attributes that may be unknown or may not apply to a tuple.

Relational Model Notation

A relation schema R of degree n is denoted by R (A 1, A 2, A n).

● The uppercase letters Q, R, S denote relation names.


● The lowercase letters q, r, s denotes relation states.
● The letters t, u, v denotes tuples.
● In general, the name of a relation schema such as STUDENT also indicates the current set of tuples in
that relation—the current relation state—whereas STUDENT (Name, Ssn,) refers only to the
relational schema.
● An attribute A can be qualified with the relation name R to which it belongs by using the dot notation
R.A—for example, STUDENT. Name or STUDENT. Age. This is because the same name may be used
for two attributes in different relations.

Relational Model Constraints and Relational Database Schemas

Constraints on databases can generally be divided into three main categories:


1. Constraints that are inherent in the data model. We call these inherent model-based constraints or implicit
constraints. Example: In the relational model, no two tuples in a relation can be duplicates. Why? Because
a relation is a set of tuples, as opposed to a bag/multiset or a sequence.
2. Constraints that can be directly expressed in schemas of the data model, typically by specifying them in
the DDL. We call these schema-based constraints or specific constraints.
3. Constraints that cannot be directly expressed in the schemas of the data model, and hence must be
expressed and enforced by the application programs. We call these application-based or semantic
constraints or business rules.

Types of Schema-based Constraints

Domain Constraints
Domain constraints specify that within each tuple, the value of each attribute A must be an atomic value
from the domain dom(A).

Key Constraints and Constraints on NULL Values


In the formal relational model, a relation is defined as a set of tuples. All elements of a set are distinct; hence,
all tuples in a relation must also be distinct.

A superkey SK specifies a uniqueness constraint that no two distinct tuples in any state r of R can have the
same value for SK. Every relation has at least one default super key—the set of all its attributes. A superkey
can have redundant attributes, however, so a more useful concept is that of a key, which has no redundancy.

A relation schema may have more than one key. In this case, each of the keys is called a candidate key. For
example, the CAR relation has (Licence_no Eng_sr_no Model Make_year model) two candidate keys:
License_number and Engine_serial_number. It is common to designate one of the candidate keys as the
primary key of the relation. This is the candidate key whose values are used to identify tuples in the relation.

Relational Databases and Relational Database Schemas

Page no: 2 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

The definitions and constraints we have discussed so far apply to single relations and their attributes. A
relational database usually contains many relations, with tuples in relations that are related in many ways.
In this section, we define a relational database and a relational database schema.
A relational database schema S is a set of relation schemas S = {R 1, R 2, ..., R m} and a set of integrity
constraints IC. A relational database state 10 DB of S is a set of relation states DB = {r 1, r 2, ..., r m} such that
each r i is a state of R i and such that the r i relation states satisfy the integrity constraints specified in IC.
Below Figure shows a relational database schema that we call COMPANY = {EMPLOYEE, DEPARTMENT,
DEPT_LOCATIONS, PROJECT, WORKS_ON, DEPENDENT}. The underlined attributes represent primary keys.

EMPLOYEEE

Fname Minit Lname Ssn Bdata Address Sex Salary Super_ssn Dno

DEPARTMENT

DNAME Dnumber Mgr_ssn Mgr_str_Date

DEPT_ LOCATIONS

Dnumber DLocation

PROJECT

Pname Pnumber Plocation Dnum

Integrity, Referential Integrity, and Foreign Keys

The entity integrity constraint states that no primary key value can be NULL. This is because the primary key
value is used to identify individual tuples in a relation. Having NULL values for the primary key implies that
we cannot identify some tuples. For example, if two or more tuples had NULL for their primary keys, we may
not be able to distinguish them if we try to reference them from other relations.

The referential integrity constraint is specified between two relations and is used to maintain the consistency
among tuples in the two relations. Informally, the referential integrity constraint states that a tuple in one
relation that refers to another relation must refer to an existing tuple in that relation.

Page no: 3 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

The relational algebra is essential for several reasons. First, it provides a formal foundation for relational
model operations. Second, and perhaps more important, it is used as a basis for implementing and
optimising queries in the query processing and optimisation modules that are integral parts of relational
database management systems (RDBMSs).

Difference Between Calculus & algebra


BASIS FOR
RELATIONAL ALGEBRA RELATIONAL CALCULUS
COMPARISON

Basic Relational Algebra is a Procedural Relational Calculus is Declarative


language. language.

States Relational Algebra states how to obtain Relational Calculus states what result
the result. we must obtain.

Order Relational Algebra describes the order in Relational Calculus does not specify
which operations must be performed. the order of operations.

Domain Relational Algebra is not domain Relation Calculus can be domain


dependent. dependent.

Related It is close to a programming language. It is close to the natural language.

The fu da e tal ope atio s of elatio al alge a a e as follo s −


● Select
● Project
● Union
● Set different
● Cartesian product
● Rename

We will discuss all these operations in the following sections.

Page no: 4 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

“elect Operatio σ
It selects tuples that satisfy the given predicate from a relation.

Notatio − σ(r)
Whe e σ sta ds fo sele tio p edi ate a d sta ds fo elatio . p is the propositional logic formula which
a use o e to s like a d, o , a d ot. These te s a use elatio al ope ato s like − =, ≠, , < , >, .

Fo e a ple −
σsu je t = "data ase"(Books)
Output − “ele ts tuples f o ooks he e the subject is 'database'.
σsu je t = "data ase" a d p i e=" " Books
Output − “ele ts tuples f o ooks he e the subject is 'database', and 'price' is 450.
σsu je t = "data ase" a d p i e=" " o ear > "2010"(Books)
Output − “ele ts tuples f o ooks he e the subject is 'database', and 'price' is 450 or those books
published after 2010.

Project Operatio ∏
It projects column(s) that satisfy a given predicate.

Notatio − ∏A , A , A

Where A1, A2, An are attribute names of relation r.


Duplicate rows are automatically eliminated, a the relation is a set.

Fo e a ple −
∏the subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.

Union Operation (∪)


It performs a binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notatio − U s

Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the follo i g o ditio s ust hold −

r and s must have the same number of attributes.


Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
∏ autho Books ∪ ∏ autho A ti les
Output − P oje ts the a es of the autho s ho have either written a book or an article or both.

“et Differe ce −
The result of set difference operation is tuples, which are present in one relation but are not in the second
relation.

Notatio − − s
Finds all the tuples that are present in r but not in s.

∏ autho Books − ∏ autho A ti les


Output − P o ides the a e of autho s ho ha e itte ooks ut ot a ti les.

Page no: 5 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Cartesia Product Χ
Combines information of two different relations into one.
Notatio − Χ s
Where r and s are relations and thei output ill e defi ed as −

Χ s = { t | ∈ r and t ∈ s}
σautho = 'tt' Books Χ A ti les
Output − Yields a elatio , hi h sho s all the ooks a d a ti les itte tt.

Re a e Operatio
The results of relational algebra are also relations but without any name. The rename operation allows us
to e a e the output elatio . ' e a e' ope atio is de oted ith s all G eek lette ho .

Notatio − E
Where the result of expression E is saved with the name of x.
Additional operations are −
● Set intersection
● Assignment
● Natural join
We can define the three operations UNION, INTERSECTION, and SET DIFFERENCE
on two union-compatible relations R and S as follows:
■ UNION: The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are either
in R or in S or in both R and S. Duplicate tuples are eliminated.
■ INTERSECTION: The result of this operation, denoted by ‘ ∩ “, is a relation that includes all tuples that are
in both R and S.
■ SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a relation that includes all
tuples that are in R but not in S.
The set operations UNION, INTERSECTION, and MINUS. (a) Two union-compatible relations.
(b) STUDENT ∪ INSTRUCTOR. “TUDENT ∩ IN“T‘UCTO‘. d “TUDENT − IN“T‘UCTO‘.
e IN“T‘UCTO‘ − “TUDENT.

CARTESIAN PRODUCT operation—also known as CROSS PRODUCT or CROSS JOIN—which is denoted by ×.


This is also a binary set operation, but the relations on which it is applied do not have to be union-compatible.
The JOIN operation, denoted by, is used to o i e elated tuples f o t o elatio s i to si gle lo ge
tuples. This operation is very important for any relational database with more than a single relation because
it allows us to process relationships among relations.

DEPT_MG‘← DEPA‘TMENT ⨝ Mgr_ssn=Snn EMPLOYEE


‘E“ULT ← ∏Dame, Lname (DEPT_MGR)

Page no: 6 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Variations of JOIN: The EQUIJOIN and NATURAL JOIN


The most common use of JOIN involves join conditions with equality comparisons only. Such a JOIN, where
the only comparison operator used is =, is called an EQUIJOIN.

The standard definition of NATURAL JOIN requires that the two join attributes (or each pair of join attributes)
have the same name in both relations. If this is not the case, a renaming operation is applied first.
P‘OJ_DEP DEPT_LOC“ ← DEPA‘TMENT * DEPT_LOCATION“.

The DIVISION Operation


The DIVISION operation, denoted by ÷, is useful for a special kind of query that sometimes occurs in database
appli atio s. A e a ple is ‘et ie e the a es of e plo ees ho o k o all the p oje ts that Joh “ ith
works on.

“MITH← σ Fname ='Jhon' AND Lname = 'SMITH' (EMPLOYEE)


“MITH_PNO“ ← pno (WORKS_ON ⨝ Essn = Ssn SMITH)

PURPOSE NOTATION

Selects all tuples that satisfy the selection condition from a σ<sele tio
SELECT
relation R. condition>(R)

PROJECT Produces a new relation with only some of the attributes of R


and removes duplicate tuples. <att i ute
list>(R)

Page no: 7 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

THETA JOIN Produces all combinations of tuples from R and R1 2 that satisfy
the join condition. R1 <join
condition> R2
Produces all the combinations of tuples from R1 and R2 that
EQUIJOIN satisfy a join condition with only equality comparisons.
R1 <join
condition> R2, OR
Same as EQUIJOIN except that the join attributes of R2 are not R1*<join
NATURAL included in the resulting relation; if the join attributes have the condition> R2, OR
JOIN same names, they do not have to be specified at all. R1* (<join
attributes 1>),
Produces a relation that includes all the tuples in R1 or R2 or
UNION both R1 and R2; R1 and R2 must be union-compatible.
R1 ∪ R2

Produces a relation that includes all the tuples in both R1 and R2;
INTERSECTI R1 and R2 must be union-compatible.
ON ‘ ∩‘

Produces a relation that includes all the tuples in R1 that are not
DIFFERENCE in R2; R1 and R2 must be union-compatible.
R1 – R2

Produces a relation that has the attributes of R1 and R2 and


CARTESIAN includes as tuples all possible combinations of tuples from R1
PRODUCT and R2. R1 × R2

Produces a relation R(X) that includes all tuples t[X] in R1(Z) that
DIVISION appear in R1 in combination with every tuple from R(Y), where
Z = X ∪ Y. R1(Z) ÷ R2(Y)

Example on Relational Algebra

Page no: 8 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Query 1. Retrieve the name and address of all e plo ees ho o k fo the ‘esea h depa t e t.
‘E“EA‘CH_DEPT ← σ Dname = 'Research'(DEPARTMENT)
‘E“EA‘CH_EMP“ ← ‘E“EA‘CH_DEPT ⨝ Dnumber = Dno EMPLOYEE)
‘E“ULT← Fname, Lname, Address (RESEARCH_EMPS)

Que . Fo e e p oje t lo ated i “taffo d , list the p oje t u e , the o t olli g depa t e t u e,
a d the depa t e t a age s last name, address, and birth date.

“TAFFO‘D_P‘OJs ← σ Plo atio = 'staffo d'(PROJECT)


CONTR_DEPTS← STAFFORD_PROJs⨝ Dnum=Dnumber DEPARTMENT)
P‘OJ_DEPT_MG‘“ ← CONT‘_DEPT“ ⨝ Mgr_ssn = Ssn EMPLOYEE)
‘E“ULT ← Pnumber, Dnum, Lname, Address,Bdate(PROJ_DEPT_MGRS)
Query 3. Find the names of employees who work on all the projects controlled by department number 5.

DEPT _P‘OJ“ ← P o P u e σD u =5(PROJECT)))


EMP_P‘OJ ← “s , P o Ess , P o WO‘K“_ON
‘E“ULT_EMP_““N“ ← EMP_P‘OJ ÷ DEPT _P‘OJ“
‘E“ULT ← L a e, Fname (RESULT_EMP_SSNS * EMPLOYEE)

Query 4. List the names of all employees with two or more dependents.

Strictly speaking, this query cannot be done in the primary (original) relational algebra. We must use the
AGGREGATE FUNCTION operation with the COUNT aggregate function. We assume that dependents of the
same employee have distinct Dependent_name values.
ƺ
T1(Ssn, No_of_Depe de ts ← Essn COUNT Dependent_name(DEPENDENT)
T ← σ No_of Depe de t (T1)
‘E“ULT ← L a e, Fname (T2* EMPLOYEE)

ID Name Dept_name Salary


10101 Shrinivasan Comp.sci 65000

Page no: 9 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

12121 Wu Finance 90000


15151 Mozart Music 40000
22222 Einstin Physics 95000

Instructor Relation
i structor relatio where the i structor is i the Physics
department, we write:
dept name = Ph sics i structor

Relational Calculus

In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that is, it tells
what to do but never explains how to do it.

‘elatio al al ulus e ists i t o fo s−

Tuple Relational Calculus (TRC) Filtering variable ranges over tuples

Notatio − {T | Co ditio }

Returns all tuples T that satisfies a condition.

Fo e a ple −

{ T.name | Author(T) AND T.article = 'database' }


Output − ‘etu s tuples ith ' a e' f o Autho ho has itte article on 'database'.

TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).

Fo e a ple −

{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}


Output − The a o e ue ill ield the sa e esult as the p e ious o e.

Domain Relational Calculus (DRC)


In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as done in TRC,
mentioned above).

Notatio −

{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

Where a1, a2 are attributes and P stands for formulae built by inner attributes.

Fo e a ple −

{< article, page, subject > | ∈ TP ∧ subject = 'database'}


Output − Yields A ti le, Page, a d “u je t f o the elatio TP he e subject is database.

Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also involves
relational operators.
The expression power of Tuple Relation Calculus and Domain Relational Calculus is equivalent to Relational

Page no: 10 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Algebra.

E-R Diagram
An ER schema diagram for the COMPANY database
Entities and Attributes
Entities and Their Attributes. The basic object that the ER model represents is an entity, which is a thing in
the real world with an independent existence. An entity may be an object with a physical existence (for
example, a person, car, house, or employee) or it may be an object with a conceptual existence (for
instance, a company, a job, or a university course).

Composite versus Simple (Atomic) Attributes. Composite attributes can be divided into smaller subparts,
which represent more basic attributes with independent meanings — for example, the Address attribute of
the EMPLOYEE entity.

Single-Valued versus Multivalued Attributes. Most attributes have a single value for a particular entity;
such attributes are called single-valued. For example, Age.
A multivalued attribute may have lower and upper bounds to constrain the number of values allowed for
each entity. For example, the Colors attribute of a car.
Stored versus Derived Attributes. In some cases, two (or more) attribute values are related—for example,
the Age and Birth_date attributes of a person.

Initial Conceptual Design of the COMPANY Database covers


Relationship & type
Cardinality

Page no: 11 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Week Entity
Participation Constraints: - The participation constraint specifies whether the existence of an entity
depends on its being related to another entity via the relationship type.

Company schema, with structural constraints specified using (min, max) notation and role names.

Enhanced Entity-Relationship (EER) Model


Semantic data modelling concepts that were incorporated into conceptual data models such as the ER
Model. ER model can be enhanced to include these concepts, leading to the Enhanced ER (EER) model.
Subclasses: - An entity type is used to represent both a type of entity and the entity set or collection of
entities of that type that exist in the database. For example, the entity type EMPLOYEE describes the type
(that is, the attributes and relationships) of each employee entity, and refers to the current set of EMPLOYEE
entities in the COMPANY database.
Superclasses: - We call each of these subgroupings a subclass or subtype of the EMPLOYEE entity type, and
the EMPLOYEE entity type is called the superclass or supertype for each of these subclasses.

Extended E-R Model


Specialisation
Specialisation is the process of defining a set of subclasses of an entity type this entity type is called the
superclass of the specialisation. The set of subclasses that forms a specialisation is defined by some
distinguishing characteristic of the entities in the superclass. For example, the set of subclasses {SECRETARY,
ENGINEER, TECHNICIAN} is a specialisation of the superclass EMPLOYEE that distinguishes
among employee entities based on the job type of each employee entity.

 Define a set of subclasses of an entity type.


Steps for Specialization

 Establish additional specific attributes with each subclass.


 Establish additional specific relationship types between each subclass and other entity types or other
subclasses.
EER diagram notation to represent subclasses and specialisation. Three specialisations of EMPLOYEE:

Page no: 12 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

{SECRETARY, TECHNICIAN, ENGINEER}


{MANAGER}
{HOURLY_EMPLOYEE, SALARIED_EMPLOYEE}

Generalisation

Generalisation
We can think of a reverse process of abstraction in which we suppress the differences among several entity
types, identify their standard features, and generalise them into a single superclass of which the original
entity types are subclasses. For example, consider the entity types CAR and TRUCK.
Generalisation. (a) Two entity types, CAR and TRUCK. (b)Generalising CAR and TRUCK into the superclass
VEHICLE.

Specialization

Inheritance
We use all the above features of ER-Model to create classes of objects in object-oriented programming. The
details of entities are generally hidden from the user; this process known as abstraction.
Inheritance is an essential feature of Generalization and Specialization. It allows lower-level entities to inherit
the attributes of higher-level entities.

Page no: 13 Follow us on facebook to get real-time updates from RGPV


Downloaded from be.rgpvnotes.in

Inheritance

For example, the attributes of a Person class such as name, age, and gender can be inherited by lower-level
entities such as Student or Teacher.

Aggregation
Aggregation is a process when the relation between two entities is treated as a single entity. Here the
relation between Center and Course is acting as an Entity in relation with Visitor.

Aggregation

Page no: 14 Follow us on facebook to get real-time updates from RGPV


We hope you find these notes useful.
You can get previous year question papers at
https://qp.rgpvnotes.in .

If you have any queries or you want to submit your


study notes please write us at
[email protected]

You might also like