Unit 2 - Database Management System - WWW - Rgpvnotes.in
Unit 2 - Database Management System - WWW - Rgpvnotes.in
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.
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.
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.
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.
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).
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.
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
DEPT_ LOCATIONS
Dnumber DLocation
PROJECT
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.
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).
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.
“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
Fo e a ple −
∏the subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
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 −
“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.
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.
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“.
PURPOSE NOTATION
Selects all tuples that satisfy the selection condition from a σ<sele tio
SELECT
relation R. condition>(R)
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 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)
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.
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)
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.
Notatio − {T | Co ditio }
Fo e a ple −
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
Fo e a ple −
Notatio −
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
Fo e a ple −
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
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.
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.
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.
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