DBMS Unit-2 (I)
DBMS Unit-2 (I)
DBMS Unit-2 (I)
MANAGEMENT
SYSTEM
(DBMS)
UNIT-II (PART-I)
SYLLABUS: UNIT-II
2
RELATIONAL MODEL
Relational model represents how data is stored in
Relational Databases.
3
RELATIONAL MODEL: TERMINOLOGY
Attribute: Attributes are the properties that define an entity.
e.g.; ROLL_NO, NAME, ADDRESS
6
SELECTION(σ)
Selection(σ): It is used to select required tuples of the
relations.
8
PROJECTION(Π)
It is used to project required column data from a relation.
9
EXAMPLE
10
RENAME(ρ)
Rename is a unary operation used for renaming attributes
of a relation.
Notation(s) used:
ρ x (E)
result of the E expression is saved with the name of x
ρ(a/b)R
rename the attribute 'b' of the relation R by 'a'.
11
UNION (∪)
Performs binary union between two relations.
It is defined as follows:
r ∪ s = { t | t ∈ r or t ∈ s}
where r and s either refer to DB relations or the relation result set (or
temporary relation).
The given conditions must hold if we want any union operation to be valid:
s, and r must contain a similar number of attributes.
The domains of an attribute must be compatible.
The duplicate tuples are eliminated automatically.
E.g.
∏ writer (Novels) ∪ ∏ writer (Articles)
12
Projecting the names of those writers who might have written either an
article or a novel or both.
EXAMPLE
13
CARTESIAN/ CROSS PRODUCT (Χ)
It helps in combining data and info of two differing
relations into a single one.
E.g.
σwriter = ‘mahesh'(Novels Χ Articles)
14
Yielding a relation that shows all the articles and novels
written by Mahesh.
EXAMPLE
15
SET DIFFERENCE/ MINUS (−)
If there are two tuples R and S, the set intersection
operation contains all tuples that are in R but not in S.
E.g.
∏ writer (Novels) − ∏ writer (Articles)
Providing the writer names who might have written novels but
have not written articles.
16
EXAMPLE
17
DERIVED OPERATIONS
Derived operations can be derived from basic
operations.
Division operations.
18
INTERSECTION OPERATION (∩)
Intersection operation is same as the intersection operator
from set theory.
It eliminates duplicates.
E.g.
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
19
20
DIVISION OPERATION ()
Division is used when you have to find out entities that
are interacting with all entities of a set of different type
entities.
R(x,y) div S(y): gives all distinct values of x from R that are associated
with all values of y in S.
Steps:
Find out all possible combinations of S(y) with R(x) by computing R(x) x(cross
join) S(y), say r1
23
EXAMPLE
24
TYPES OF JOINS
25
NATURAL JOIN
A natural join is the set of tuples of all combinations in R
and S that are equal on their common attribute names.
It is denoted by ⋈.
26
SAMPLE DATABASE
27
LEFT JOIN (LEFT OUTER JOIN)
A left join returns all rows from the left table and the matching rows from the right table.
If there is no match in the right table, NULL values are returned for the right table's
columns.
Syntax:
SELECT column_name(s) FROM table1 LEFT JOIN Table2 ON Table1.Column_Name=table2.column_name;
28
RIGHT JOIN (RIGHT OUTER JOIN)
A right join is similar to a left join but returns all rows from the right table and
the matching rows from the left table.
If there is no match in the left table, NULL values are returned for the left
table's columns.
Syntax:
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name =
table2.column_name;
29
FULL OUTER JOIN
A full outer join returns all rows from both tables and includes
NULL values where there is no match in either table.
Syntax:
SELECT column_name FROM table1 FULL OUTER JOIN table2 ON
table1.columnName = table2.columnName WHERE condition;
30
EQUI JOIN
An equi join combines rows from two or more tables based on the equality
of values in specified columns (or attributes).
It is a common type of join used to retrieve related data from different
tables.
Syntax:
SELECT column_list FROM table1, table2.... WHERE table1.column_name =
table2.column_name;
SELECT Employees.employee_name, Departments.department_name
FROM Employees
JOIN Departments ON Employees.department_id = Departments.department_id;
31
SELF JOIN
Self Join is a type of Inner Join.
A self join is used to combine rows from a single table.
32
EXAMPLE
33
34
35