Untitled
Untitled
Untitled
Snapshot of Unit-1
DATABASE – set of interrelated data
DBMS – software or tool which provides an environment to store the
data set of programs to access those data in an efficient manner
ER Model
Entity
Entity Set
Attributes
Relationship
Relationship set
Keys
ER Symbols
ER Diagrams
Extended ER Features
Design Issues
University Questions
Explain the database system architecture with neat diagram?
Explain three basic notations in E-R models, with example?
Discuss about
Data models
Keys
Write about
Basic Concepts of ER model
ER diagram
Explain E-R diagram with example?
Explain the design of an E-R database schema?
Describe in detail about the Extended E-R Features.
Unit-2
Relational Model
Relational Model
Relational Model - Structure of Relational Databases
The set of allowed values for each attribute is called the domain of the
attribute
type/domain
integrity constraints
Relation Instance
The current values (relation instance) of a relation are specified by a
table
An element t of r is a tuple, represented by a row in a table
Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
attributes
(or columns)
customer_name customer_street customer_city
Jones Main Harrison
Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield
customer
Database
A database consists of multiple relations
Information about an enterprise is broken up into parts, with
each relation storing one part of the information
E.g.
depositor Relation
Why Split Information Across Relations?
Non-procedural, or declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
Pure languages form underlying basis of query languages that people
use.
Query Languages
It is a language in which a user requests information from the db.
Query Languages
23
Relational Algebra
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations as inputs and produce a
new relation as a result.
Relational Algebra
Six basic operators
select: - Selects a subset of rows from relation (horizontal).
project: - Retains only wanted columns from relation (vertical).
union: - Tuples in r1 and/or in r2.
set difference: – Tuples in r1, but not in r2.
Cartesian product: x - Allows us to combine two relations.
rename:
The operators take one or two relations as inputs and produce a new
relation as a result.
Basic operations
Select
It selects tuples that satisfy a given predicate,
To denote selection.
(Sigma) is used.
Syntax:
Sal>1000 (Employee)
27
Selection ()
(Table name)
Syntax: ∏
<Attribute list>
E.g… ∏ (employee)
eid,sal
Projection
Schema of result:
exactly the fields in the projection list, with the same
names that they had in the input relation.
E.G..
∏ ( Sal>1000
(employee)
)
eid,sal
- Selects tuples where sal>1000 & from them only eid and salary
attributes are selected.
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
rating 8(S2)
sname rating
yuppy 9
rusty 10
Union Operation:
R1 U R2 - implies that tuples either from R1 or tuples from
R2 or both R1 & R2.
E.G..
EMP1 EMP2
34
Union Operation:
∏ (EMP1)
eid U ∏ eid
(EMP2)
eid
1
2
3
4
5
6
35
Union
S1 S2
sid sname rating age sid sname rating age
22 dustin 7 45.0 28 yuppy 9 35.0
31 lubber 8 55.5
31 lubber 8 55.5 44 guppy 5 35.0
58 rusty 10 35.0 58 rusty 10 35.0
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
S1 S2
SET DIFFERENCE
R1 – R2 implies tuples present in R1 but not in R2.
∏ (EMP1)
- ∏ (EMP2)
eid eid
eid
2
3
37
Set Difference
sid sname rating age
22 dustin 7 45.0 sid sname rating age
31 lubber 8 55.5 22 dustin 7 45.0
58 rusty 10 35.0
S1 S2
S1
39
CARTESIAN PRODUCT
EMP1 EMP2
1 A 1 10 XXX
Emp1 Emp2 1 A 4 50 YYY
1 A 5 60 ZZZ
1 A 6 70 UUU
2 B 1 10 XXX
Cross Product Example
sid sname rating age
sid bid day
22 dustin 7 45.0
22 101 10/10/96 31 lubber 8 55.5
58 103 11/12/96 58 rusty 10 35.0
R1
S1
1 2
2 3
1 s
r
A B
r s: 1
2
1
3
Set Difference Operation – Example
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s:
A B
1
1
Cartesian-Product Operation – Example
Relations r, s:
A B C D E
1 10 a
10 a
2 20 b
r 10 b
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
Rename Operation
Used to name and to refer the results of relational-algebra expressions.
Allows us to refer to a relation by more than one name.
Example:
x (E)
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Banking Example
branch (branch_name, branch_city, assets)
Find the names of all customers who have a loan at the Perryridge branch.
customer_name (branch_name=“Perryridge”
customer_name(depositor)
Additional Operations
Additional Operations
Set intersection
Natural join
Aggregation
Outer Join
Division
Set-Intersection Operation – Example
Relation r, s:
A B A B
1 2
2 3
1
r s
rs
A B
2
Intersection
R1
U
R2 implies tuples present in both R1 & R2
∏ (EMP1)
U ∏ (EMP2)
eid eid
eid
1
4
52
Natural Join Operation – Example
Relations r, s:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
NATURAL JOIN OR EQUI JOIN
It requires that the two join attributes have the same name,
otherwise renaming operation is applied first and then join
operation is applied.
Symbol:
NATURAL JOIN OR EQUI JOIN
Syntax:
55
NATURAL JOIN OR EQUI JOIN
E.g..
EMPLOYEE DEPARTMENT
56
Employee Dept
or
Employee Dept
Employee.dno = department.dno
57
OUTER JOIN
EMPLOYEE DEPARTMENT
LEFT OUTER JOIN-
61
RIGHT OUTER JOIN-
It takes all tuples in the right relation that did not match with
any tuple in the left relation and pads the tuples with null values
for all other attributes and adds them to the result of the natural
join.
RIGHT OUTER JOIN-
∏ (EMPLOYEE DEPARTMENT)
Eid,dname,sal
Padding tuples from the left relation that didn’t match any from the right
relation, as well as tuples from the right relation that did not match any from
the left relation & adding them to the result of the join.
FULL OUTER JOIN
∏ Eid,dname,sal
(EMPLOYEE DEPARTMENT)
66
Division Operation
Depositor Branch
:
Customer- Acc-No. Branch- Branch-city assets
Name Name
67
Division Operation
To find all customers who have an account at all the branches located in
Adayar.
The query is,
∏ Customer-
(depositor account)
name, branch-
name
∏ branch-name
( Branch_city
= “Adayar” (branch)
)
Division Operation
Step 1:
List out all the branches located in Adayar
Step2: R1=
∏ branch-name
( = “Adayar” (branch)
Branch_city
)
Find all customer name, branch_name pairs for which the
customer has an account at a branch
R2=
∏ (depositor
Customer-
account)
name, branch-
name
Division Operation
Step3:
We need to find customers who appear in r2 with every branch name in R1.
∏ Customer-
(depositor account)
name, branch-
name
∏ (
branch-name )
= “Adayar” (branch)
Branch_city
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1 ,G2 ,,Gn
F ( A ),F ( A ,,F ( A ) (E )
1 1 2 2 n n
7
7
3
10
27
Insertion
Updating
Provide as a gift for all loan customers in the Perryridge branch, a 200 savings account. Let the loan
number serve as the account number for the new savings account.
r F1,F2 ,,Fl , (r )
Each Fi is either
the I th
attribute of r, if the I th
attribute is not updated, or,
if the attribute is to be updated Fi is an expression, involving
only constants and the attributes of r, which gives the new value
for the attribute
Update Examples
Make interest payments by increasing all balances by 5 percent.