DBMS Unit 2
DBMS Unit 2
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.
STUDENT
REG_NO NAME
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.
REG_NO NAME
REG_NO NAME REG_NO Phone_no
STUDENT
REG_NO NAME
reg_no name phone_no1 phone_no2 Phone_no3
STUDENT
Date_of
_join
empno ename Dept_id dname
empno dept_id date_of_join
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
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
R = R1 ∪ 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
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
Using the above notation, we can write the query “Find the empno
for each employee with a salary greater than Rs.2,000” as:
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
{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
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:
If P1 and P2 are formulae, then so are P1 ∨ P2, P1 ∧ P2, and P1 ⇒ P2.
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
Banking Examples
• Find the names of all customers having a loan, an account, or both at the bank
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 ]))}
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
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 )}