DBMS2
DBMS2
customer
Relations are Unordered
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.
K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for Customer, since it is a
superkey (assuming no two customers can possibly have the same name),
and no subset of it is a superkey.
Determining Keys from E-R Sets
Strong entity set. The primary key of the entity set becomes the primary
key of the relation.
Weak entity set. The primary key of the relation consists of the union of the
primary key of the strong entity set and the discriminator of the weak entity
set.
Relationship set. The union of the primary keys of the related entity sets
becomes a super key of the relation.
For binary many-to-one relationship sets, the primary key of the “many”
entity set becomes the relation’s primary key.
For one-to-one relationship sets, the relation’s primary key can be that
of either entity set.
For many-to-many relationship sets, the union of the primary keys
becomes the relation’s primary key
Schema Diagram for the Banking Enterprise
Query Languages
Language in which user requests information from the
database.
Categories of languages
procedural
non-procedural
“Pure” languages:
Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
Pure languages form underlying basis of query
languages that people use.
Relational Algebra
Six basic operators
select
project
union
setdifference
Cartesian product
rename
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 (e.g., 2nd
column of r deals with the same type of values as does the
2nd column of s)
E.g. to find all customers with either an account or a loan
customer-name (depositor) customer-name (borrower)
Union Operation – Example
Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t r and t s}
Set differences must be taken between compatible
relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Set Difference Operation – Example
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.
Cartesian-Product Operation-
Example
Composition of Operations
Rename Operation
name.
Example:
x (E)
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
x (A1, A2, …, An) (E)
returns the result of expression E under the name X,
and with the attributes renamed to A1, A2, …., An.
Example Queries
Example: The table E (for EMPLOYEE)
nr name salary
1 John 100
5 Sarah 300
7 Tom 100
PROJECT salary(E) PROJECTnr, salary(E)
salary
100
300
NOT(Hobby=‘hiking’) (Person)
Hobby‘hiking’ (Person)
Project Operator
33
Produces table containing subset of columns of
argument table
attribute list(relation)
Example:
Person Name,Hobby(Person)
Note: r s = r - (r - s)
Set-Intersection Operation - Example
Natural-Join Operation
Notation: r s
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Natural Join Operation – Example
Division Operation
Division Operation – Example
Another Division Example
Another Division Example
Assignment Operation
The assignment operation () provides a convenient way to express complex queries.
Write query as a sequential program consisting of
a series of assignments
followed by an expression whose value is displayed as a result of the query.
Assignment must always be made to a temporary relation variable.
Example: Write r s as
temp1 R-S (r)
temp2 R-S ((temp1 x s) – R-S,S (r))
result = temp1 – temp2
The result to the right of the is assigned to the relation variable on the left of the .
May use variable in subsequent expressions.
Example Queries
Query 1
CN( BN=“Downtown”(depositor account))
Query 2
customer-name, branch-name (depositor account)
temp(branch-name) ({(“Downtown”), (“Uptown”)})
Example Queries
Extended Relational-Algebra-Operations
Generalized Projection
Outer Join
Aggregate Functions
Generalized Projection
Extends the projection operation by allowing
arithmetic functions to be used in the projection list.
SUM(A)
R= A B
COUNT(A)
1 3
MIN(B)
3 4
MAX(B)
3 2
AVG(B)
SUM(A) = 1 + 3 + 3 = 7
COUNT(A) = 3
MIN(B) = 2
MAX(B) = 4
AVG(B) = (3 + 4 + 2) / 3 = 3
Aggregate Operation – Example
Aggregate Operation – Example
Aggregate Functions (Cont.)
Outer Join
An extension of the join operation that avoids loss
of information.
Computes the join and then adds tuples form one
relation that do not match tuples in the other
relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does not exist
All comparisons involving null are (roughly speaking)
false by definition.
Outer Join – Example
Relation loan
loan-number branch-name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700
Relation borrower
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Outer Join – Example
Outer Join – Example
Null Values
It is possible for tuples to have a null value, denoted by null, for some of
their attributes
null signifies an unknown value or that a value does not exist.
The result of any arithmetic expression involving null is null.
Aggregate functions simply ignore null values
For duplicate elimination and grouping, null is treated like any other value,
and two nulls are assumed to be the same
Alternative: assume each null is different from each other
Null Values
Comparisons with null values return the special truth value unknown
Three-valued logic using the truth value unknown:
OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
NOT: (not unknown) = unknown
In SQL “P is unknown” evaluates to true if predicate P evaluates to
unknown
Result of select predicate is treated as false if it evaluates to unknown
Modification of the Database
The content of the database may be modified using
the following operations:
Deletion
Insertion
Updating