Documente Academic
Documente Profesional
Documente Cultură
Introduction
A relational database consists of tables each of which
is assigned a unique name.
Row in a table represents a relationship among a set of
values.
In relational model the term relation is used to refer to
a table.
The term tuple is used to refer to a row.
The term attribute refers to a column.
Relation instance refers to a specific instance of a
relation.
The order in which the tuples appear in the relation is
irrelevant, since a relation is set of tuples.
Introduction
For each attribute of a relation there is set of permitted
values called the domain of that attribute.
E.g. domain of salary attribute is set of all possible
salary values.
For all possible relations r, the domain of all attribute be
atomic.
A domain is atomic if elements of the domain are
considered to be indivisible unit.
E.g. the value of an attribute can be an account number,
but cannot be a set of account numbers
Example of a Relation
Schema
Schema: the logical design of a
database.
Instance: snapshot of the data in the
database at a given instant in time.
Relation Schema
relation r is a subset of
D 1 x D2 x x Dn
Thus, a relation is a set of n-tuples (a1,
a2, , an) where each ai Di
Schema of a relation consists of
attribute definitions
name
type/domain
integrity constraints
Relation Instance
Main
North
North
Park
customer
Harrison
Rye
Rye
Pittsfield
tuples
(or rows)
Database
Keys
Let K R
K is a superkey of R if values for K are sufficient to
identify a unique tuple of each possible relation r(R)
by possible r we mean a relation r that could exist in
the enterprise we are modeling.
Example: {customer_name, customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can
possibly have the same name
In real life, an attribute such as customer_id would be used
instead of customer_name to uniquely identify customers, but
we omit it to keep our examples small, and instead assume
customer names are unique.
Keys (Cont.)
K is a candidate key if K is minimal
Example: {customer_name} is a candidate key
for Customer, since it is a superkey and no
subset of it is a superkey.
Primary key: a candidate key chosen as the
principal means of identifying tuples within a
relation
Should choose an attribute whose value never, or very
rarely, changes.
E.g. email address is unique, but may change
Foreign Keys
Schema Diagram
FIGURE
7.1
The ER
conceptual
schema
diagram for
the
COMPANY
database.
FIGURE 7.2
Result of
mapping the
COMPANY ER
schema into
a relational
schema.
FIGURE 4.11
Ternary relationship types. (a) The SUPPLY
relationship.
FIGURE 7.3
Mapping the n-ary relationship type SUPPLY
from Figure 4.11a.
FIGURE 4.4
EER diagram
notation for
an attributedefined
specializatio
n on
JobType.
FIGURE 7.4
Options for mapping specialization or generalization.
(a) Mapping the EER schema in Figure 4.4 using option 8A.
FIGURE 4.3
Generalization. (b) Generalizing CAR and TRUCK into
the superclass VEHICLE.
FIGURE 7.4
Options for mapping specialization or generalization.
(b) Mapping the EER schema in Figure 4.3b using option 8B.
FIGURE 4.4
EER diagram
notation for
an attributedefined
specializatio
n on
JobType.
FIGURE 7.4
Options for mapping specialization or generalization.
(c) Mapping the EER schema in Figure 4.4 using option 8C.
FIGURE 4.5
EER diagram notation for an overlapping
(nondisjoint) specialization.
FIGURE 7.4
Options for mapping specialization or generalization.
(d) Mapping Figure 4.5 using option 8D with Boolean type fields Mflag and
Pflag.
FIGURE 4.7
A specialization
lattice with
multiple
inheritance for a
UNIVERSITY
database.
FIGURE 7.5
Mapping the EER specialization lattice in Figure
4.6 using multiple options.
FIGURE 4.8
Two categories (union
types): OWNER and
REGISTERED_VEHICLE.
FIGURE 7.6
Mapping the EER
categories
(union types) in
Figure 4.7 to
relations.
Mapping Exercise
Query Languages
Procedural
Non-procedural, or declarative
In a procedural language the user instructs
the system to perform a sequence of
operations on the database to compute the
desired result.
In nonprocedural the user describes the
desired information without giving a specific
procedure.
Relational
Algebra
Procedural language
Six basic operators
select:
project:
union:
set difference:
Cartesian product: x
rename:
Select Operation
Notation: p(r)
p is called the selection predicate
Defined as:
Example of selection:
branch_name=Perryridge
(account)
12
23 10
23 10
Project
Operation
Notation:
A1 , A2 ,, Ak
(r )
Relation r:
A,C (r)
10
20
30
40
Union
Operation
Notation: r s
Defined as:
r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd
column
of r deals with the same type of values as does the 2nd
column of s)
Example: to find all customers with either an account or a loan
customer_name (depositor) customer_name (borrower)
Relations r, s:
r s:
Set
Difference
Operation
Notation r s
Defined as:
r s = {t | t r and t s}
Relations r, s:
r
r s:
Cartesian-Product Operation
Example
Relations r, s:
10
10
20
10
a
a
b
b
r x s:
A
1
1
1
1
2
2
2
2
10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
Cartesian-Product
Operation
Notation r x s
Defined as:
r x s = {t q | t r and q s}
Assume that attributes of r(R) and s(S) are disjoint. (That is, R
S = ).
If attributes of r(R) and s(S) are not disjoint, then renaming
must be used.
Composition of Operations
A=C(r x s)
1
1
1
1
2
2
2
2
10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
1
2
2
10 a
10 a
20 b
Rename
Operation
x (E)
x ( A ,A
1
2 ,..., An
(E )
Banking Example
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)
Example Queries
Find all loans of over $1200
greater than
$1200
loan_number (amount
> 1200
(loan))
Find the names of all customers who have a loan at the Perryridge
branch.
customer_name (branch_name=Perryridge
(borrower.loan_number = loan.loan_number(borrower x loan)))
Example
Queries
customer_name (branch_name=Perryridge
(borrower.loan_number = loan.loan_number(borrower x
loan)))
Example
Queries
Find the names of all customers who have a loan at the Perryridge
branch.
Query 1
Query 2
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = Perryridge (loan)) x
borrower))
Example Queries
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
Formal
Definition
Additional Operations
Set intersection
Natural join
Division
Assignment
Set-Intersection
Operation
Notation: r s
Defined as:
r s = { t | t r and t s }
Assume:
r, s have the same arity
attributes of r and s are compatible
Note: r s = r (r s)
Relation r, s:
1
2
1
r
rs
2
3
s
Natural-Join Operation
Notation: r
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
1
2
4
1
2
a
a
b
a
b
1
3
1
2
3
a
a
a
b
b
r
r
s
A
1
1
1
1
2
a
a
a
a
b
Assignment Operation
account at bank.
Find the name of all customers who have a loan at the bank
and the loan amount
loan)
account ))
account)
temp(branch_name) ({(Downtown ),
(Uptown )})
Note that Query 2 uses a constant relation.
Bank
Example
Queries
account)
Extended Relational-AlgebraOperations
Generalized Projection
Aggregate Functions
Outer Join
Generalized
Projection
F1 ,F2 ,..., Fn (E )
F ( A ),F ( A ,,F ( A ) (E )
1
Aggregate Operation
Relation r:
Example
g sum(c) (r)
10
sum(c )
27
Aggregate Operation
Relation account grouped by branch-name:
Example
branch_nameaccount_number
Perryridge
Perryridge
Brighton
Brighton
Redwood
branch_name
balance
A-102
A-201
A-217
A-215
A-222
sum(balance)
400
900
750
750
700
(account)
branch_name sum(balance)
Perryridge
Brighton
Redwood
1300
1500
700
Aggregate
Functions
(Cont.)
branch_name
sum(balance) as sum_balance
(account)
Outer
Join
Outer
Join
Example
Relation loan
Downtown
Redwood
Perryridge
3000
4000
1700
Relation borrower
customer_nameloan_number
Jones
Smith
Hayes
L-170
L-230
L-155
Outer
Join
Example
Join
loan
borrower
Downtown
Redwood
3000
4000
Jones
Smith
loan
borrower
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
null
loan
borrower
Downtown
Redwood
null
3000
4000
null
Jones
Smith
Hayes
loan
borrower
Downtown
Redwood
Perryridge
null
3000
4000
1700
null
Jones
Smith
null
Hayes
Null
Values
It is possible for tuples to have a null value, denoted by null,
for some of their attributes
Modification of the
The content of the database may be modified using the
Database
following operations:
Deletion
Insertion
Updating
All these operations are expressed using the assignment
operator.
Deletion
Deletion
Examples
account account
loan loan
(r1)
Insertion
Insertion
Examples
loan))
Updating
r F ,F ,,F , (r )
Each Fi is either
Update Examples
interest
and pay all others 5 percent
account account_number, branch_name, balance * 1.06 ( BAL 10000
(account ))
account_number, branch_name, balance * 1.05 (BAL 10000
(account))