DATABASE
MANAGEMENT
SYSTEM
(DBMS)
UNIT-II (PART-I)
SYLLABUS: UNIT-II
Relational Model: Structure of relational Databases,
Relational Algebra and Relational Calculus, Operations on
Relational Algebra, Operations on Relational Calculus,
Tuple Relational Calculus, Domain Relational Calculus.
SQL and Integrity Constraints: Concept of DDL, DML,
DCL. Basic Structure, Set operations, Aggregate Functions,
Null Values, Domain Constraints, Referential Integrity
Constraints, assertions, Introduction to views, Querying,
Nested Sub queries, Database security application
development using SQL, Stored procedures and triggers.
2
RELATIONAL MODEL
Relational model represents how data is stored in
Relational Databases.
A relational database consists of a collection of tables,
each of which is assigned a unique name.
It was developed by E.F. Codd from IBM in the 1970s.
The relational database model allows any table to be
related to another table using a common attribute.
3
RELATIONAL MODEL: TERMINOLOGY
Attribute: Attributes are the properties that define an entity.
e.g.; ROLL_NO, NAME, ADDRESS
Relation Schema: A relation schema defines the structure of the
relation and represents the name of the relation with its attributes. e.g.;
STUDENT (ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the
relation schema for STUDENT.
Tuple: Each row in the relation is known as a tuple.
Relation Instance: The set of tuples of a relation at a particular instance
of time is called a relation instance..
Cardinality: The number of tuples in a relation is known as cardinality.
4
Degree: The number of attributes in the relation is known as the degree
of the relation.
RELATIONAL ALGEBRA
Relational algebra is a procedural query language.
It gives a step by step process to obtain the result of the
query.
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.
5
OPERATORS IN RELATIONAL
ALGEBRA
6
SELECTION(σ)
Selection(σ): It is used to select required tuples of the
relations.
The selection operator only selects the required tuples but
does not display them. To display, the data projection
operator is used.
Notation used: σp(r)
where
σ stands for the selection predicate
r stands for the relation
p refers to the prepositional logic formula that may use connectors
such as or, and, and not. 7
Relational operators can also be used such as − =, ≠, ≥, < , >, ≤
EXAMPLE
8
PROJECTION(Π)
It is used to project required column data from a relation.
Notation used: ∏ B1, B2, Bn (r)
where B1, B2 , Bn refer to the attribute names of the relation r.
By Default, projection removes duplicate data.
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.
Notation used: r∪s
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.
Notation used: rΧs
It is defined as the follows:
s Χ r = { t ∈ s and q t | q ∈ r}
where s and r refer to the relations.
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.
Notation used: R-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.
Derived operations include:
Join Operations
Intersection operations
Division operations.
18
INTERSECTION OPERATION (∩)
Intersection operation is same as the intersection operator
from set theory.
It selects all tuples which are present in both relations.
It eliminates duplicates.
Notation used: R∩S
Where R is the first relation and S is the second relation
E.g.
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
19
Retrieve the names which are present in STUDENT as well as in
EXAMPLE
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.
The division operator is used for queries which involve
the 'all'.
R1 ÷ R2 = tuples of R1 associated with all tuples of R2.
Some instances where division operator is used are:
Which person has account in all the banks of a particular city?
Which students have taken all the courses required to
21
graduate?
DIVISION OPERATION (÷)
Given two relations(tables): R(x,y) , S(y)
R and S : tables
x and y : column of R
y : column of S
R(x,y) div S(y): gives all distinct values of x from R that are associated
with all values of y in S.
Computation of Division : R(x,y) ÷ S(y)
Steps:
Find out all possible combinations of S(y) with R(x) by computing R(x) x(cross
join) S(y), say r1
Subtract actual R(x,y) from r1, say r2
22
x in r2 are those that are not associated with every value in S(y); therefore R(x)-
r2(x) gives us x that are associated with all values in S
JOIN OPERATION (⋈)
A Join operation combines related tuples from different
relations, if and only if a given join condition is satisfied.
It is denoted by ⋈.
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 ⋈.
SELECT * from employee NATURAL JOIN dept;
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;
SELECT employee.employee_name, departments.department_name FROM employees LEFT OUTER JOIN
departments ON employees.department_id = departments.department_id;
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;
SELECT Employees.employee_name, Departments.department_name
FROM Employees
RIGHT JOIN Departments ON Employees.department_id = Departments.department_id;
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;
SELECT Employees.employee_name, Departments.department_name
FROM Employees
FULL OUTER JOIN Departments ON Employees.department_id =
Departments.department_id;
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.
It is typically used when a table has a relationship with
itself, such as in a hierarchical structure or when you want
to compare records within the same table.
Syntax:
SELECT e.employee_name AS employee_name, m.employee_name AS
manager_name
FROM Employees e
LEFT JOIN Employees m ON e.manager_id = m.employee_id;
32
EXAMPLE
33
34
35