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

DBMS Unit 2

Uploaded by

vn2087
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

DBMS Unit 2

Uploaded by

vn2087
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

21CSC205P Database

Management Systems
Outline of the Presentation
• Conversion of ER model to Relational Table
• Case study: Apply conversion concept. Discussion of various design issues
• Pitfalls in Relational Database systems
• Understanding various Relational languages such as Tuple Relational calculus, Domain
relational calculus, Calculus Vs Algebra, Computational capabilities.
• Case Study: Applying Relational Algebra for all the queries of application Designed.
Conversion of ER to Relational Table
After completing the ER diagram
ER diagram is converted into the tables in relational model
Relational model can be easily implemented in ORACLE, MYSQL , etc.,
The below points to be considered for converting ER diagram into tables.
• Strong Entity Set With Only Simple Attributes
• Strong Entity Set With Composite Attributes
• Strong Entity Set With Multi Valued Attributes
• Translating Relationship Set into a Table
• Binary Relationships With Cardinality Ratios
• Binary Relationship With Both Cardinality Constraints and
Participation Constraints
• Binary Relationship With Weak Entity Set
Conversion of ER to Relational Table
Strong Entity Set With Only Simple Attributes
A strong entity set with only simple attributes will require only one
table in relational model.
Attributes of the table will be the attributes of the entity set.
The primary key of the table will be the key attribute of the entity set.

ER DIAGRAM Er Diagram to relational table RELATIONAL TABLE

REG_NO NAME REG_NO NAME DOB

STUDENT

DOB Schema : student (reg_no, name, dob)


Conversion of ER to Relational Table
Strong Entity Set With Composite Attributes
 A strong entity set with any number of composite attributes will require only one table
in relational model.
 While conversion, simple attributes of the composite attributes are taken into account
and not the composite attribute itself.
ER DIAGRAM Er Diagram to relational table RELATIONAL TABLE

F_NAME Reg_no F_name L_name dno street city


L_NAME

REG_NO NAME

Schema : student (reg_no, f_name, l_name, dno, street, city)


STUDENT
Street

Dno

address city
Conversion of ER to Relational Table
Strong Entity Set With Multi Valued Attributes
 A strong entity set with any number of multi valued attributes will require two tables in
relational model.
 One table will contain all the simple attributes with the primary key.
 Other table will contain the primary key and all the multi valued attributes.

ER DIAGRAM Er Diagram to relational table RELATIONAL TABLE

REG_NO NAME
REG_NO NAME REG_NO Phone_no

STUDENT

Phone_no Schema : student (reg_no, name) Schema : student (reg_no, phone_no)


Conversion of ER to Relational Table

Strong Entity Set With Multi Valued Attributes


Also the stronger entity set with any number of multi valued attributes
may be converted as shown below

ER DIAGRAM Er Diagram to relational table RELATIONAL TABLE

REG_NO NAME
reg_no name phone_no1 phone_no2 Phone_no3

STUDENT

Phone_no Schema : student (reg_no, name, phone_no1 ,phone_no2, phone_no3)


Conversion of ER to Relational Table
Translating Relationship Set into a Table
 A relationship set will require one table in the relational model. NOTE
If we consider the overall ER
 Attributes of the table are : diagram, three tables will be
• Primary key attributes of the participating entity sets required in relational model
1. Emp
• Its own descriptive attributes if any.
2. Dept
 Set of non-descriptive attributes will be the primary key. 3.Works_in

ER DIAGRAM Er Diagram to relational table RELATIONAL TABLE

Date_of
_join
empno ename Dept_id dname
empno dept_id date_of_join

emp Works_in dept

salary Schema : works_in (empno, dept_id, date_of_join)


Conversion of ER to Relational Table
Binary Relationships With Cardinality Ratios

Four types are possible

1. Binary relationship with cardinality ratio 1:1

2. Binary relationship with cardinality ratio 1:m

3. Binary relationship with cardinality ratio m:1

4. Binary relationship with cardinality ratio m:m


Conversion of ER to Relational Table
Mapping Cardinality
 The relationship set counselor, between the faculty and student entity sets may be one-
to-one, one-to-many, many-to-one, or many-to-many.
 To distinguish among these types, we draw either a directed line ( → ) or an undirected
line ( — ) between the relationship set and the entity.
Binary Relationships With Cardinality Ratios
 Binary relationship with cardinality ratio 1:1
Line from the relationship set counselor to both entity sets faculty and student as given
in the figure below. This indicates that a faculty may counsel at most one student, and
a student may have at most one counselor.

FACULTY
Faculty_ID Student
Faculty_Name Student_RegNo
Faculty_Designation Student_Name
Faculty _Sal Counselor
Student_DOB
Faculty_DOB Student_Location
Faculty_MobileNo Dept_ID
Dept_ID
Conversion of ER to Relational Table
Binary Relationships With Cardinality Ratios
Binary relationship with cardinality ratio 1:m
A directed line from the relationship set counselor to the entity set faculty
and an undirected line to the entity set student as shown in the below figure,
indicates that a faculty may counsel many students, but a student may have
at most one counselor.

FACULTY
Faculty_ID Student
Faculty_Name Student_RegNo
Faculty_Designation Student_Name
Faculty _Sal Counselor
Student_DOB
Faculty_DOB Student_Location
Faculty_MobileNo Dept_ID
Dept_ID
Conversion of ER to Relational Table
Binary Relationships With Cardinality Ratios
Binary relationship with cardinality ratio m:1
An undirected line from the relationship set counselor to the entity set faculty
and a directed line to the entity set student as shown in the below figure,
indicates that a faculty may counsel at most one student, but a student may
have many counselors.

FACULTY
Faculty_ID Student
Faculty_Name Student_RegNo
Faculty_Designation Student_Name
Faculty _Sal Counselor
Student_DOB
Faculty_DOB Student_Location
Faculty_MobileNo Dept_ID
Dept_ID
Conversion of ER to Relational Table
Binary Relationships With Cardinality Ratios
Binary relationship with cardinality ratio m:m
We draw an undirected line from the relationship set counselor to both entity
sets faculty and student as shown in the below figure, indicates that a faculty
may counsel many students, and a student may have many counselor.

FACULTY
Faculty_ID Student
Faculty_Name Student_RegNo
Faculty_Designation Student_Name
Faculty _Sal Counselor
Student_DOB
Faculty_DOB Student_Location
Faculty_MobileNo Dept_ID
Dept_ID
Conversion of ER to Relational Table
Binary Relationship With Both Cardinality Constraints and Participation Constraints
 Because of the total participation constraint, foreign key acquires NOT NULL constraint
 Now foreign key can not be null.
 Option 1: For Binary Relationship With Cardinality Constraint and Total Participation
Constraint From One Side
 Because cardinality ratio = 1 : n , so we will combine the entity set B and
relationship set R.
 Then, two tables will be required-
• A ( a1 , a2 )
• BR ( a1 , b1 , b2 )
 Because of total participation, foreign key a1 has acquired NOT NULL constraint,
so it can’t be null now.

a1 a2 b1 b2

A 1 R m B
Conversion of ER to Relational Table
Binary Relationship With Both Cardinality Constraints and Participation
Constraints
Option 2: Binary Relationship With Cardinality Constraint and
Total Participation Constraint From Both Sides
If there is a key constraint from both the sides of an
entity set with total participation, then that binary
relationship is represented using only single table.
• ARB ( a1 , a2 , b1 , b2 )

a1 a2 b1 b2

m m
A R B
Conversion of ER to Relational Table
Binary Relationship With Weak Entity Set
Weak entity set always appears in association with identifying
relationship with total participation constraint.
Here, two tables will be required-
• A ( a1 , a2 )
• BR ( a1 , b1 , b2 )

a1 a2 b1 b2

m m
A R B
Pitfalls in Relational database

Pitfalls in Relational Database


Relational database design requires that we find a “good”
collection of relation schemas.
A bad design may lead to
• Repetition of information.
• Inability to represent certain information.
Design Goals:
• Avoid redundant data
• Ensure that relationships among attributes are
represented
• Facilitate the checking of updates for violation of database
integrity constraints
Pitfalls in Relational database

Example
Consider the relation schema:
Lending-schema = (branch_name, branch_city, assets,
customer_name, loan_number, amount)
Redundancy:
• Data for branch-name, branch-city, assets are repeated for each
loan that a branch makes
• Wastes space and complicates updating
 Null values
• Cannot store information about a branch if no loans exist
• Can use null values, but they are difficult to handle
Pitfalls in Relational database, Decomposing bad
schema
Decomposition

Repetition of information will lead to many issues, that type of


schema is a bad design. To avoid such issues the designed schema is
decomposed into two or more schemas.

Consider the schema : Lending_schema

Lending_schema is decomposed into two schemas as given below

• Branch-customer-schema = (branch_name, branch_city, assets,


customer_name)
• Customer-loan-schema = (customer_name, loan_number,
amount)
Pitfalls in Relational database, Decomposing bad
schema
Decomposition

All attributes of an original schema (R) must appear in the


decomposition (R1, R2):

R = R1 ∪ R

Lossless-join decomposition. For all possible relations r on


schema R.
r = ΠR1 (r) ΠR2 (r)
Pitfalls in Relational database, Decomposing bad
schema
Decomposition
Example :
Consider The relation : R = (A,B)
Decomposition of R = R1(A) and R2(B)
R
ΠA (R) ΠB(R) ΠA (R) ΠB(R)

A B A B A B
X 1 X 1 X 1
X 2 Y 2 X 2
Y 1 Y 1
Y 2
Relational Algebra – Fundamental Operators and syntax
Relational algebra is a procedural query language
An operator can be either unary or binary
Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
Fundamental operations of Relational Algebra
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
Relational Algebra – Fundamental Operators and syntax
Select Operation (σ)
It retrieves the records that satisfy the given predicate from a relation
Notation : σp(r)
Where
σ - Selection Predicate
r – Relation
p – Prepositional logic formula

Example 1: σJOB = “MANAGER"(EMP)


Output : Retrieve the records from emp where job is manager

Example 2 : σJOB = “MANAGER" and DEPTNO = 20(EMP)


Output : Retrieve the records from emp where job is manger and
deptno is 20.
Relational Algebra – Fundamental Operators and syntax

Project Operation (∏)


It projects column(s) that satisfy a given predicate.

Notation : ∏A1, A2,…, An (r)


Where
∏ - Predicate
A1,A2,…,An – Attribute names of a relation
r – Relation

Note : Because of relation is a set duplicate rows are eliminated


automatically.

Example : ∏ENAME, JOB (EMP)

Output : It selects and projects the column named as ename


and job from emp table
Relational Algebra – Fundamental Operators and syntax

Union Operation (∪)


 It performs binary union between two given relations.
 It is defined as
r ∪ s = { t | t ∈ r or t ∈ s}
Notation : r U s

Where
U is the union operator
R and S are relations

 The following conditions must hold from both relations to make the union
operation is true
• Relations R and S should have the same number of Attributes
• The Attributes domain values (data types) must be compatible
• Duplicate records will be eliminated automatically
Relational Algebra – Fundamental Operators and syntax
Union Operation (∪)
Consider the following table
F1 F2
30 C
Records in table a Records in table b 10 A
Sql > Select * from a Sql > Select * from b; Example : AUB 20 B
F1 F2 ID NAME 40 D
30 C 50 E
30 C
10 A
20 B 40 D
50 E
ID NAME
Example : BUA 10 A
20 B
30 C
40 D
50 E
Relational Algebra – Fundamental Operators and syntax
Set Difference (−)
The result of set difference operation is tuples, which are present in
one relation but are not in the second relation.
Notation : r − s
Example : Consider the following tables
F1 F2
Example : A-B 10 A
Records in table a Records in table b
20 B
Sql > Select * from a Sql > Select * from b;
F1 F2 ID NAME
30 C
30 C
10 A
20 B 40 D
ID NAME
50 E
Example : B-A 40 D
50 E
Relational Algebra – Fundamental Operators and
syntax
Cartesian Product (Χ)
Combines information of two different relations into one.
For each and every record in the first query , all the records will be
executed in the second query
Notation : r Χ s
Where r and s are relations and their output will be defined as :
r Χ s = { q t | q ∈ r and t ∈ s} F1 F2 ID NAME
10 A 30 C
Example : Consider the following tables 20 B 30 C
Records in table a Records in table b
30 C 30 C
Sql > Select * from a Sql > Select * from b;
10 A 40 D
F1 F2 ID NAME AΧB 20 B 40 D
30 C
30 C 30 C 40 D
10 A
10 A 50 E
20 B 40 D
20 B 50 E
50 E
30 C 50 E
Relational Algebra – Fundamental Operators and syntax

Rename Operation (ρ)


The rename operation allows us to rename the output relation.
Notation : ρ x (E)
Where the result of expression E is saved with name of x.
Relational algebra queries,
Tuple relational calculus
Relational algebra queries
Example Queries 1:
 Find the empno, ename, job, salary for employess whose salary is greater than
Rs.2000
{t | t ∈ emp ∧ t[sal] > 2000}
 Suppose that we want only the empno attribute, rather than all attributes of the emp
relation.
 To write this query in the tuple relational calculus, we need to write an expression for
a relation on the schema (empno). We need those tuples on (empno) , such that there
is a tuple in emp with the sal attribute > 2000.
 To express this request, we need the construct “there exists” from mathematical logic.
 The notation: ∃ t ∈ r (Q(t))
means “there exists a tuple t in relation r such that predicate Q(t) is true.”
Relational algebra queries,
Tuple relational calculus

Using the above notation, we can write the query “Find the empno
for each employee with a salary greater than Rs.2,000” as:

{t | ∃ s ∈ emp (t[empno] = s[empno] ∧ s[sal] > 2000)}

In English, the above expression is read as


“The set of all tuples t such that there exists a tuple s in relation
emp for which the values of t and s for the empno attribute are
equal, and the value of s for the sal attribute is greater than
Rs.2,000.”

Tuple variable t is defined on only the empno attribute, since that


is the only attribute having a condition specified for t. Thus, the
result is a relation on (empno).
Relational algebra queries,
Tuple relational calculus
Relational algebra queries
Example Queries 2:
 Consider the query “Find the names of all employees whose department is in the location
“CHICAGO.” This query is slightly more complex than the previous queries, since it
involves two relations: emp and dept.

 It requires is that we have two “there exists” clauses in our tuple-relational-calculus


expression, connected by and ( ∧).

 We write the query as follows:


{t | ∃ s ∈ emp (t[ename] = s[ename]
∧ ∃ u ∈ dept (u[dname] = s[dname]
∧ u[building] = “CHICAGO”))}

 Tuple variable u is restricted to departments that are located in the chicago, while tuple
variable s is restricted to emp whose dept name matches that of tuple variable u.
Relational algebra queries,
Tuple relational calculus
Tuple Relational Calculus

The tuple relational calculus is a nonprocedural query language.

It describes the desired information without giving a specific


procedure for obtaining that information.

A query in the tuple relational calculus is expressed as:

{t | P(t)}

That is, it is the set of all tuples t such that predicate P is true for t.

we use t[A] to denote the value of tuple t on attribute A, and we use
t ∈ r to denote that tuple t is in relation r.
Relational algebra queries,
Tuple relational calculus

Tuple Relational Calculus


Formal definition
A tuple-relational-calculus expression is of the form:
{t|P(t)}
where P is a formula.

Several tuple variables may appear in a formula. A tuple


variable is said to be a free variable unless it is quantified by a ∃ or ∀.

Thus, in:
t ∈ emp ∧ ∃ s ∈ dept(t[dname] = s[dname])
t is a free variable.
Tuple variable s is said to be a bound variable.
Relational algebra queries,
Tuple relational calculus
A tuple-relational-calculus formula is built up out of atoms. An
atom has one of the following forms:

s ∈ r, where s is a tuple variable and r is a relation (we do not allow


use of the ∈/ operator).

s[x] u[y], where s and u are tuple variables, x is an attribute on


which s is defined, y is an attribute on which u is defined, and is a
comparison operator (<, ≤, =, =, >, ≥); we require that attributes x
and y have domains whose members can be compared by (-)

s[x] c, where s is a tuple variable, x is an attribute on which s is


defined, is a comparison operator, and c is a constant in the
domain of attribute x.
Relational algebra queries,
Tuple relational calculus

We build up formulae from atoms by using the following rules:

An atom is a formula.

If P1 is a formula, then so are ¬P1 and (P1).

If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.

If P1(s) is a formula containing a free tuple variable s, and r is a relation,


then
∃ s ∈ r (P1(s)) and ∀ s ∈ r (P1(s))
are also formulae.
Relational algebra queries,
Tuple relational calculus
• branch (branch_name, branch_city, assets )
• customer (customer_name, customer_street, customer_city )
• account (account_number, branch_name, balance )
• loan (loan_number, branch_name, amount )
• depositor (customer_name, account_number )
• borrower (customer_name, loan_number )
Relational algebra queries,
Tuple relational calculus

Banking Examples
Find the loan_number, branch_name, and amount for loans of over $1200
{t | t  loan  t [amount ]  1200}
Find the loan number for each loan of an amount greater than $1200

{t |  s loan (t [loan_number ] = s [loan_number ]  s [amount ]  1200)}

Notice that a relation on schema [loan_number ] is implicitly defined by


the query
Relational algebra queries,
Tuple relational calculus

Banking Examples
• Find the names of all customers having a loan, an account, or both at the bank

{t | s  borrower ( t [customer_name ] = s [customer_name ])


 u  depositor ( t [customer_name ] = u [customer_name ])
Find the names of all customers who have a loan and an account
at the bank
{t |  s loan (t [loan_number ] = s [loan_number ]  s [amount ]  1200)}

{t | s  borrower ( t [customer_name ] = s [customer_name ])


 u  depositor ( t [customer_name ] = u [customer_name] )
Relational algebra queries,
Tuple relational calculus

Banking Examples
• Find the names of all customers having a loan at the Perryridge branch
{t | s  borrower (t [customer_name ] = s [customer_name ]
 u  loan (u [branch_name ] = “Perryridge”
 u [loan_number ] = s [loan_number ]))}

• Find the names of all customers who have a loan at the


Perryridge branch, but no account at any branch of the bank

{t | s  borrower (t [customer_name ] = s [customer_name ]


 u  loan (u [branch_name ] = “Perryridge”
 u [loan_number ] = s [loan_number ]))
 not v  depositor (v [customer_name ] =
t [customer_name ])}
Relational algebra queries,
Tuple relational calculus

Safety of Expressions
• It is possible to write tuple calculus expressions that generate infinite relations.
• For example, { t |  t r } results in an infinite relation if the domain of any
attribute of relation r is infinite
• To guard against the problem, we restrict the set of allowable expressions to
safe expressions.
• An expression {t | P (t )} in the tuple relational calculus is safe if every
component of t appears in one of the relations, tuples, or constants that appear
in P
• NOTE: this is more than just a syntax condition.
• E.g. { t | t [A] = 5  true } is not safe --- it defines an infinite set with
attribute values that do not appear in any relation or tuples or constants
in P.
Relational algebra queries,
Tuple relational calculus
Domain Relational Calculus

A second form of relational calculus, called domain relational calculus,


uses domain variables that take on values from an attributes domain,
rather than values for an entire tuple. The domain relational calculus,

However, is closely related to the tuple relational calculus.

Domain relational calculus serves as the theoretical basis of the widely


used QBE language, just as relational algebra serves as the basis for
the SQL language.
Relational algebra queries,
Tuple relational calculus
Domain Relational Calculus

An expression in the domain relational calculus is of the form

{< x1, x2,..., xn > | P(x1, x2,..., xn)}

where x1, x2,..., xn represent domain variables. P represents a


formula composed of atoms, as was the case in the tuple relational
calculus.
Relational algebra queries,
Tuple relational calculus
Domain Relational Calculus

An atom in the domain relational calculus has one of the following
forms:
< x1, x2,..., xn > ∈ r, where r is a relation on n attributes and x1,
x2,..., xn are domain variables or domain constants.
x y, where x and y are domain variables and is a comparison
operator (<, ≤, =, =, >, ≥). We require that attributes x and y have
domains that can be compared by (-).
x c, where x is a domain variable, is a comparison operator, and c
is a constant in the domain of the attribute for which x is a domain
variable.
Relational algebra queries,
Tuple relational calculus
Domain Relational Calculus
Example Queries
• Find the loan_number, branch_name, and amount for loans of
over $1200
{ l, b, a  |  l, b, a   loan  a > 1200}
Find the names of all customers who have a loan of over $1200
{ c  |  l, b, a ( c, l   borrower   l, b, a   loan  a > 1200)}

• Find the names of all customers who have a loan from the
Perryridge branch and the loan amount:
 { c, a  |  l ( c, l   borrower  b ( l, b, a   loan 
b = “Perryridge”))}
 { c, a  |  l ( c, l   borrower   l, “ Perryridge”, a   loan)}
Relational algebra queries,
Tuple relational calculus
Domain Relational Calculus
Example Queries
• Find the names of all customers having a loan, an account, or both
at the Perryridge branch:
{ c  |  l (  c, l   borrower
  b,a ( l, b, a   loan  b = “Perryridge”))
  a ( c, a   depositor
  b,n ( a, b, n   account  b = “Perryridge”))}
• Find the names of all customers who have an account at all
branches located in Brooklyn:
{ c  |  s,n ( c, s, n   customer) 
 x,y,z ( x, y, z   branch  y = “Brooklyn”) 
 a,b ( x, y, z   account   c,a   depositor)}
Relational algebra queries,
Tuple relational calculus

Safety of Expressions

The expression:
{  x1, x2, …, xn  | P (x1, x2, …, xn )}

is safe if all of the following hold:


1.All values that appear in tuples of the expression are values from
dom (P ) (that is, the values appear either in P or in a tuple of a
relation mentioned in P ).
2.For every “there exists” subformula of the form  x (P1(x )), the
subformula is true if and only if there is a value of x in dom (P1)
such that P1(x ) is true.
3.For every “for all” subformula of the form x (P1 (x )), the subformula
is true if and only if P1(x ) is true for all values x from dom (P1).
THANK YOU

You might also like