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

DBMS Module 2 - PPT

Uploaded by

revathi
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)
22 views

DBMS Module 2 - PPT

Uploaded by

revathi
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/ 101

Centre for Distance and Online

Education (CDOE)

Database Management System


…Databases and Database users
Module 2

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

Relational Relational Relational Update


Model Database Operations and
model Constraints Schemas Dealing with
Relational Constraint
Database Violations
Schemas
Centre for Distance and Online
Education (CDOE)

Relational Model Concepts


The relational Model of Data is based on the
concept of a Relation
The strength of the relational approach to data
management comes from the formal foundation
provided by the theory of relations
Centre for Distance and Online
Education (CDOE)

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.

• A relation typically contains a set of rows.

• The data elements in each row represent certain facts that


correspond to a real-world entity or relationship
• In the formal model, rows are called tuples
Centre for Distance and Online
Education (CDOE)

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)

• 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)

Introduction
Centre for Distance and Online
Education (CDOE)

• The earlier definition of a relation can be restated


more formally using set theory concepts as follows.
• A relation (or relation state) r(R) is a mathematical
relation of degree n on the domains dom(A1),
dom(A2), … , dom(An), which is a subset of the
Cartesian product (denoted by ×) of the domains
that define R:
r(R) Í (dom(A1) ´ dom(A2) ´ . . . ´ (dom(An))
Centre for Distance and Online
Education (CDOE)

Characteristics of the Database


Approach
The current relation state—reflects only the valid tuples
that represent a particular state of the real world.
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

Ordering of Values within a Tuple


Education (CDOE)

and an Alternative 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
important
Centre for Distance and Online
Education (CDOE)

• An alternative definition of a relation


can be given, making the ordering of
values in a tuple unnecessary.
Centre for Distance and Online
Education (CDOE)

• In this definition, a relation schema R = {A1,


A2, … , An} is a set of attributes (instead of
an ordered list of attributes), and a relation
state r(R) is a finite set of mappings r = {t1,
t2, … , tm}, where each tuple ti is a mapping
from R to D, and D is the union (denoted
by È) of the attribute domains.
Centre for Distance and Online
Education (CDOE)

According to this definition of tuple as a


mapping, a tuple can be considered as a set
of (<attribute>, <value>) pairs, where each
pair gives the value of the mapping from an
attribute Ai to a value vi from dom(Ai).
Centre for Distance and Online
Education (CDOE)

Characteristics of the Database


Approach

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 basic
relational model.
Centre for Distance and Online
Education (CDOE)

Characteristics of the Database


Approach

Interpretation (Meaning) of a Relation


The relation schema can be interpreted as a
declaration or a type of assertion.
Each tuple in the relation can then be interpreted as a fact or a
particular instance of the assertion.
Centre for Distance and Online
Education (CDOE)

Characteristics of the Database


Approach

some relations may represent facts about entities,


whereas other relations may represent facts about
relationships.
Centre for Distance and Online
Education (CDOE)

Entity Integrity, Referential


Integrity, and Foreign Keys
• The entity integrity constraint states that no
primary key value can be NULL.
• The referential integrity constraint is
specified between two relations and is used
to maintain the consistency among tuples in
the two relations.
Centre for Distance and Online
Education (CDOE)

Entity Integrity, Referential


Integrity, and Foreign Keys
• The entity integrity constraint states that no
primary key value can be NULL.
• The referential integrity constraint is
specified between two relations and is used
to maintain the consistency among tuples in
the two relations.
Centre for Distance and Online
Education (CDOE)

Entity Integrity, Referential


Integrity, and Foreign Keys
• The entity integrity constraint states that no
primary key value can be NULL.
• The referential integrity constraint is
specified between two relations and is used
to maintain the consistency among tuples in
the two relations.
Centre for Distance and Online
Education (CDOE)
Update Operations,
Transactions, and Dealing with
Constraint Violations
• The operations of the relational model can be
categorized into retrievals and updates. The
relational algebra operations, which can be
used to specify retrievals.
Centre for Distance and Online
Education (CDOE)

The Insert Operation


• The Insert operation provides a list of
attribute values for a new tuple t that is to
be inserted into a relation R.
• Insert can violate any of the four types of
constraints.
Centre for Distance and Online
Education (CDOE)

The Insert Operation


• Domain constraints can be violated if an
attribute value is given that does not
appear in the corresponding domain or is
not of the appropriate data type.
Centre for Distance and Online
Education (CDOE)

The Insert Operation


Operation:
• Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’,
‘6357 Windy Lane, Katy, TX’, F, 28000, NULL, 4>
into EMPLOYEE.
• Result: This insertion violates the entity integrity
constraint (NULL for the primary key Ssn), so it is
rejected.
Centre for Distance and Online
Education (CDOE)

The Delete Operation


• The Delete operation can violate only referential
integrity.
• This occurs if the tuple being deleted is
referenced by foreign keys from other tuples in
the database.
• To specify deletion, a condition on the attributes
of the relation selects the tuple (or tuples) to be
deleted.
Centre for Distance and Online
Education (CDOE)

The Delete Operation


Operation:
• Delete the WORKS_ON tuple with Essn =
‘999887777’ and Pno = 10.
• Result: This deletion is acceptable and deletes
exactly one tuple.
Centre for Distance and Online
Education (CDOE)

The Delete Operation


Operation:
• Delete the EMPLOYEE tuple with Ssn =
‘999887777’.
• Result: This deletion is not acceptable, because
there are tuples in WORKS_ON that refer to this
tuple. Hence, if the tuple in EMPLOYEE is deleted,
referential integrity violations will result.
Centre for Distance and Online
Education (CDOE)

The Update Operation


• The Update (or Modify) operation is used to
change the values of one or more attributes in a
tuple (or tuples) of some relation R.
• It is necessary to specify a condi- tion on the
attributes of the relation to select the tuple (or
tuples) to be modified.
Centre for Distance and Online
Education (CDOE)

The Update Operation


• Here are some examples.
– Operation:
• Update the salary of the EMPLOYEE tuple with
Ssn = ‘999887777’ to 28000.
• Result: Acceptable.
Centre for Distance and Online
Education (CDOE)

The Update Operation


Operation:
• Update the Dno of the EMPLOYEE tuple with Ssn
= ‘999887777’ to 7.
• Result: Unacceptable, because it violates
referential integrity.
Centre for Distance and Online
Education (CDOE)

The Transaction Concept


• A database application program running
against a relational database typically exe-
cutes one or more transactions.
• A transaction is an executing program that
includes some database operations, such as
reading from the database, or applying inser-
tions, deletions, or updates to the database.
Centre for Distance and Online
Education (CDOE)

The 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.
Centre for Distance and Online
Education (CDOE)

Types of Relational operation


Centre for Distance and Online
Education (CDOE)

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

Downtown L-17 1000


Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
Centre for Distance and Online
Education (CDOE)

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

Operation: (EMPLOYEE ⋈ SALARY)


Centre for Distance and Online
Education (CDOE)

Join Operations
Result:

EMP_CODE EMP_NAME SALARY


101 Stephan 50000
102 Jack 30000
103 Harry 25000

Operation: (EMPLOYEE ⋈ SALARY)


Centre for Distance and Online
Education (CDOE)

Types of Join operations:


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
• 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

EMP_NAME STREET CITY


Ram Civil line Mumbai
Shyam Park street Kolkata
Ravi M.G. Street Delhi
Hari Nehru nagar Hyderabad
Centre for Distance and Online
Education (CDOE)

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)

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
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)

OUTER UNION operation


• The OUTER UNION operation was developed to
take the union of tuples from two relations that have
some common attributes, but are not union (type)
compatible.
• This operation will take the UNION of tuples in two
relations R(X, Y) and S(X, Z) that are partially
compatible, meaning that only some of their
attributes, say X, are union compatible.
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)

Additional Relational Operations


• Generalized Projection
• Aggregate functions grouping
• Recursive closure operations
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)

Aggregate Functions and Grouping


• Request that cannot be expressed in the basic
relational algebra is to specify mathematical
aggregate functions on collections of values from
the database.
• Examples of such functions include retrieving the
average or total salary of all employees or the total
number of employee tuples.
Centre for Distance and Online
Education (CDOE)

Aggregate Functions and Grouping


• These functions are used in simple statistical
queries that summarize information from the
database tuples.
Centre for Distance and Online
Education (CDOE)

Recursive Closure Operations


• This operation is applied to a recursive
relationship between tuples of the same type, such
as the relationship between an employee and a
supervisor.
• This relationship is described by the foreign key
Super_ssn of the EMPLOYEE relation
Centre for Distance and Online
Education (CDOE)

• Relational Database Design


Using ER-to-Relational
Mapping
Centre for Distance and Online
Education (CDOE)
Centre for Distance and Online
Education (CDOE)
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

Relational Relational Relational Update


Model Database Operations and
model Constraints Schemas Dealing with
Relational Constraint
Database Violations
Schemas

You might also like