DBMS Module 2 - PPT
DBMS Module 2 - PPT
Education (CDOE)
Dr.Soumyasri S.M
Associate Professor, Dept. of MCA,
NIE, Mysore.
Centre for Distance and Online
Education (CDOE)
Introduction
From this module you will be able to learn
Introduction
• A Relation is a mathematical concept based on
the ideas of sets
• The model was first proposed by Dr. E.F. Codd
of IBM Research in 1970
Centre for Distance and Online
Education (CDOE)
Introduction
• Informally, a relation looks like a table of values.
Introduction
• Each column has a column header that gives an
indication of the meaning of the data items in that
column
• In the formal model, the column header is called an
attribute name (or just attribute)
Centre for Distance and Online
Education (CDOE)
Definition
• A relation schema2 R, denoted by R(A1, A2, … , An), is
made up of a relation name R and a list of attributes, A1,
A2, … , An.
• Each attribute Ai is the name of a role played by some
domain D in the relation schema R. D is called the
domain of Ai and is denoted by dom(Ai).
Centre for Distance and Online
Education (CDOE)
Definition
• A relation of degree seven, which stores
information about university students, would
contain seven attributes describing each
student as follows:
• STUDENT(Name, Ssn, Home_phone,
Address, Office_phone, Age, Gpa)
Centre for Distance and Online
Education (CDOE)
Definition
• Using the data type of each attribute, the
definition is sometimes written as:
• STUDENT(Name: string, Ssn: string,
Home_phone: string, Address: string,
Office_phone: string, Age: integer, Gpa: real)
Centre for Distance and Online
Education (CDOE)
Definition
• A relation (or relation state)4 r of the
relation schema R(A1, A2, … , An), also
denoted by r(R), is a set of n-tuples r = {t1, t2,
… , tm}.
• Each n-tuple t is an ordered list of n values t
=<v1, v2, … , vn>, where each value vi, 1 £ i £
n, is an element of dom (Ai) or is a special
NULL value.
Centre for Distance and Online
Education (CDOE)
Definition
• The terms relation intension for the schema R
and relation extension for a relation state r(R)
are also commonly used
Centre for Distance and Online
Education (CDOE)
Introduction
Centre for Distance and Online
Education (CDOE)
Characteristics of Relations
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 particular order.
Centre for Distance and Online
Select Operation
• The select operation selects tuples that
satisfy a given predicate.
• It is denoted by sigma (σ).
Notation: σ p(r)
Centre for Distance and Online
Education (CDOE)
Select Operation
• Where:
• σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which
may use connectors like: AND OR and NOT.
These relational can use as relational operators
like =, ≠, ≥, <, >, ≤.
Centre for Distance and Online
Education (CDOE)
Select Operation
• For example: LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT
Select Operation
• Input:
• σ BRANCH_NAME="perryride" (LOAN)
• Output:
• BRANCH_NAME LOAN_NO AMOUNT
Perryride L-15 1500
Perryride L-16 1300
Centre for Distance and Online
Education (CDOE)
Project Operation
• This operation shows the list of those
attributes that we wish to appear in the
result. Rest of the attributes are
eliminated from the table.
• It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Centre for Distance and Online
Education (CDOE)
Select Operation
• Where:
• A1, A2, A3 is used as an attribute name of
relation r.
• Example: CUSTOMER RELATION
Centre for Distance and Online
Education (CDOE)
Select Operation
NAME STREET CITY
Jones Main Harrison
Smith North Rye
Hays Main Harrison
Curry North Rye
Johnson Alma Brooklyn
Brooks Senator Brooklyn
Centre for Distance and Online
Education (CDOE)
Select Operation
• Input:
• ∏ NAME, CITY (CUSTOMER) NAME CITY
• output Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
Centre for Distance and Online
Education (CDOE)
Union Operation
• Suppose there are two tuples R and S. The
union operation contains all the tuples that are
either in R or S or both in R & S.
• It eliminates the duplicate tuples. It is denoted
by ∪.
Notation: R ∪ S
Centre for Distance and Online
Education (CDOE)
Union Operation
• A union operation must hold the following
condition:
• R and S must have the attribute of the same
number.
• Duplicate tuples are eliminated automatically.
• Example:
• DEPOSITOR RELATION
Centre for Distance and Online
Education (CDOE)
Union Operation
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
Centre for Distance and Online
Education (CDOE)
Union Operation
• BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Centre for Distance and Online
Education (CDOE)
Union Operation
Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOME
R_NAME (DEPOSITOR)
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
Centre for Distance and Online
Education (CDOE)
Union Operation
• Output: CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
Centre for Distance and Online
Education (CDOE)
Set Intersection
• Suppose there are two tuples R and S. The
set intersection operation contains all tuples
that are in both R & S.
• It is denoted by intersection ∩.
Notation: R ∩ S
Centre for Distance and Online
Education (CDOE)
Set Intersection
• Example: Using the above DEPOSITOR table
and BORROW table
• Input:
• ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUS
TOMER_NAME (DEPOSITOR)
Centre for Distance and Online
Education (CDOE)
Set Intersection
• Output:
CUSTOMER_NAME
Smith
Jones
Centre for Distance and Online
Education (CDOE)
Set Difference
• Suppose there are two tuples R and S. The set
intersection operation contains all tuples that are
in R but not in S.
• It is denoted by intersection minus (-).
Notation: R - S
• Example: Using the above DEPOSITOR table
and BORROW table
Centre for Distance and Online
Education (CDOE)
Set Difference
• Example
• ∏ CUSTOMER_NAME (BORROW) -
∏ CUSTOMER_NAME (DEPOSITOR)
Centre for Distance and Online
Education (CDOE)
Set Difference
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
Centre for Distance and Online
Education (CDOE)
Cartesian product
• The Cartesian product is used to combine
each row in one table with each row in the
other table. It is also known as a cross
product.
• It is denoted by X.
• Notation: E X D
Centre for Distance and Online
Education (CDOE)
Cartesian product
• EMPLOYEE
EMP_ID EMP_NAME EMP_DEPT
1 Smith A
2 Harry C
3 John B
Centre for Distance and Online
Education (CDOE)
Cartesian product
• DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Centre for Distance and Online
Education (CDOE)
Cartesian product
• Input:
• EMPLOYEE X DEPARTMENT
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
Centre for Distance and Online
Education (CDOE)
Cartesian product
• Output: EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
Centre for Distance and Online
Education (CDOE)
Join Operations
• A Join operation combines related tuples
from different relations, if and only if a
given join condition is satisfied. It is
denoted by ⋈.
• Example:EMPLOYEE
Centre for Distance and Online
Education (CDOE)
Join Operations
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
Centre for Distance and Online
Education (CDOE)
Join Operations
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
Join Operations
Result:
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 ⋈.
• Example: Let's use the above EMPLOYEE table
and SALARY table:
• Input:
• ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Centre for Distance and Online
Education (CDOE)
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 ⋈.
• Example: Let's use the above EMPLOYEE table
and SALARY table:
• Input:
• ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Centre for Distance and Online
Education (CDOE)
Natural Join
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
Centre for Distance and Online
Education (CDOE)
Outer Join
• The outer join operation is an extension of the
join operation. It is used to deal with missing
information.
• Example:
• EMPLOYEE
Centre for Distance and Online
Education (CDOE)
Outer Join
Outer Join
FACT_WORKERS
EMP_NAME BRANCH SALARY
Ram Infosys 10000
Shyam Wipro 20000
Kuber HCL 30000
Hari TCS 50000
Centre for Distance and Online
Education (CDOE)
Outer Join
Input:(EMPLOYEE ⋈ FACT_WORKERS)
Outer Join
Output:
EMP_NAME STREET CITY BRANCH SALARY
Ram Civil line Mumbai Infosys 10000
Shyam Park street Kolkata Wipro 20000
Hari Nehru nagar Hyderabad TCS 50000
Centre for Distance and Online
Education (CDOE)
Outer Join
• An outer join is basically of three types:
• Left outer join
• Right outer join
• Full outer join
Centre for Distance and Online
Education (CDOE)
Equi join
• It is also known as an inner join. It is the
most common join. It is based on matched
data as per the equality condition. The
equi join uses the comparison operator(=).
Centre for Distance and Online
Education (CDOE)
Equi join
• Example:
• CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
Centre for Distance and Online
Education (CDOE)
Equi join
• PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Centre for Distance and Online
Education (CDOE)
Equi join
• Input:
• CUSTOMER ⋈ PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Centre for Distance and Online
Education (CDOE)
• Output:
CLASS_I NAME PRODUC CITY
D T_ID
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida
Centre for Distance and Online
Education (CDOE)
Division operation
• The division operator is used for queries which
involve the 'all'.
• R1 ÷ R2 = tuples of R1 associated with all tuples of
R2.
Centre for Distance and Online
Education (CDOE)
Division operation
• Example
• Retrieve the name of the subject that is
taught in all courses.
Centre for Distance and Online
Education (CDOE)
Division operation
Name Course
System Btech
Database Mtech
Database Btech
Algebra Btech
Centre for Distance and Online
Education (CDOE)
Division operation
• ÷
Course
Btech
Mtech
Centre for Distance and Online
Education (CDOE)
Division operation
• =
Name
database
Centre for Distance and Online
Education (CDOE)
Division operation
• The resulting operation must have all
combinations of tuples of relation S that
are present in the first relation or R.
Centre for Distance and Online
Education (CDOE)
Generalized projection
• The generalized projection operation extends the
projection operation by allowing functions of
attributes to be included in the projection list. The
generalized form can be expressed as:
• pF1, F2, ..., Fn (R)
• where F1, F2, … , Fn are functions over the
attributes in relation R and may involve arithmetic
operations and constant values.
Centre for Distance and Online
Education (CDOE)
Algorithm
• Step 1: Mapping of Regular Entity Types
• Step 2: Mapping of Weak Entity Types.
• Step 3: Mapping of Binary 1:1 Relationship
Types
• Step 4: Mapping of Binary 1:N
Relationship Types.
Centre for Distance and Online
Education (CDOE)
Algorithm
• Step 5: Mapping of Binary M:N
Relationship Types.
• Step 6: Mapping of Multivalued Attributes
• Step 7: Mapping of N-ary Relationship
Types.
Centre for Distance and Online
Education (CDOE)
Module 2-summary
From this module you learned