Chapters 2 & 6 The Relational Model. 2 A tabular data structure Tables (relations) with unique...
-
Upload
terence-chapman -
Category
Documents
-
view
224 -
download
0
Transcript of Chapters 2 & 6 The Relational Model. 2 A tabular data structure Tables (relations) with unique...
Chapters 2 & 6
The Relational Model
2
The Relational Model A tabular data structure
Tables (relations) with unique names rows (tuples/entities/records) columns (attributes/fields)
Domain (or dom) of an attribute: a set of permitted atomic (non-decomposable) values
Tuples t r(R), where R = A1, A2, …, An, is an ordered set of values, i.e.,
t = < v1, v2, …, vn > where v1 dom(A1), v2 dom(A2), …, vn dom(An).
Relation r(R): a set of tuples r(R) dom(A1) dom(A2) … dom(An)
r is a relation instance
3
The Relational Model Database Schema
a relational DB schema consists of a set of relation schemas a relation schema has a (i) name and (ii) a finite set of attributes, where
each one is associated with a domain r(R) denotes a relation (instance) on the relation schema R
Definition. A candidate key K is a non-empty set of attributes which has the following properties:
a) uniqueness: K uniquely identifies a tuple in a relation, and
b) minimality: no proper subset of K satisfies the uniqueness constraint
• a primary key is a chosen candidate key
• a superkey satisfies (a) but may not satisfy (b)
• a relation may have more than one candidate key
• a foreign key in a relation schema is the primary key of another relation schema
Keys in the relational model K is a superkey for R if t1, t2 r(R), t1 t2, then t1[K] t2[K]
4
The Relational Model Relational Algebra
Operators used for manipulating relations, i.e., specifying retrieval/update requests (queries)
Relations in a relational DBMS are closed under the relational algebraic operations, since a
query result is itself is a relation
A procedural query language
5
The Relational Model Unary relational operators:
select () project () rename () assignment ()
Binary relational operators: cartesian produce () union () difference () intersection () natural join () division ()
Set Operations
6
The Relational Model Fundamental relational operators:
select () project () cartesian produce () union () difference () rename ()
Additional relational operators: intersection () natural join () division () assignment ()
7
The Relational Model Select ()
Selects the tuples (rows) from a relation r satisfying a certain selection condition C, i.e., c(r)
C is a Boolean expression on attributes of r
Resulting relation has the same attributes as r
e.g., (std_major = “CS” std_major = “Math”) std_gpa 3.5 (Student)
Project () Keeps only certain attributes specified in an attribute list L
from a relation r
Resultant relation has only those attributes of r specified in L
e.g., std_name, std_phone# (Student)
8
Relational Algebra
A = “a¹” (r¹) A B Ca1 b1 c1
a1 b3 c1
A,C (r¹)A Ca1 c1
a2 c2
r¹A B Ca1 b1 c1
a2 b2 c2
a1 b3 c1
Example.
9
The Relational Model Cartesian Product ()
combines information from two relations
r¹(A¹, A2, …, An) r2 (B¹, B2, …, Bm) = r3(A¹, …, An, B¹, …, Bm), and t r3, t[A¹, …, An] r1, t[B¹, …, Bm] r2
if |r¹| = i and |r2| = j
Union ()
r1 r2: combines tuples in r1 with those tuples in r2
duplicate tuples are eliminated
the two involved relations must be union-compatible
union-compatible:
1) same arity (degree), i.e., same number of attributes
2) attributes in the corresponding columns must have the same domain
, then |r1 r2| = i j
10
Relational AlgebraExample (continued).
r¹
A B C
a1 b1 c1
a2 b2 c2
a1 b3 c1
r2
A B C
a1 b3 c1
a2 b1 c1
r¹ r2
r¹.A r2.Ar¹.B r¹.C r2.B r2.C
a¹ a¹b¹ c¹ b3 c1
a¹ a2b¹ c¹ b1 c1
a2 a¹b2 c2 b3 c1
a2 a2b2 c2 b1 c1
a¹ a¹b3 c¹ b3 c1
a¹ a2b3 c¹ b1 c1
r¹ r2
A B C
a1 b1 c1
a2 b2 c2
a1 b3 c1
a2 b1 c1
11
The Relational Model
Set Difference ()
r1 r2 : contains tuples which occur in r1 but not in r2
the two involved relations must be union-compatible
Rename ()
s(r) returns relation r under the name s, or
BA(r) returns attribute A under the attribute name B
12
Relational Algebra
Example (continued).
r¹
A B C
a1 b1 c1
a2 b2 c2
a1 b3 c1
r2
A B C
a1 b3 c1
a2 b1 c1
s(r¹)
A B C
a1 b1 c1
a2 b2 c2
a1 b3 c1
r¹ r2
A B C
a1 b1 c1
a2 b2 c2
=
13
The Relational Model Additional Relational Operations
simplify queries constructed by using the fundamental operators
more convenient since less operators involved
do not add expressive power to the relational model
Additional Operations: Set Intersection ()
• r1 r2 contains common tuples in r1 and r2
• the two involved relations must be union-compatible
• can be rewritten using set difference, i.e.,
r s = r (r s)
14
Relational Algebra
Example (continued).
r¹
A B C
a1 b1 c1
a2 b2 c2
a1 b3 c1
r2
A B C
a1 b3 c1
a2 b1 c1
r1 r2
A B C
a1 b3 c1
15
The Relational Model – Additional Operators
Natural Join (): a combination of , , in that order
: merge two relations into one
: select equal values on common attributes
: remove duplicated columns
A “” becomes “” if the two operand relations have no common attributes, e.g.,
r(A, B) s(C, D) = r(A, B) s(C, D)
Rewrite using , and :
r(R) s(S) = R S (r.A1 = s.A1 ... r.An = s.An (r s))
where R S = {A¹, …, An}
16
Relational Algebra
r¹
A B Ca1 b1 c1
a2 b2 c2
a1 b3 c1
r3
B D
b3 d4
b1 d5
b1 d3
Example (continued).
A B Ca1 b1 c1
Dd3
a1 b1 c1 d5
a1 b3 c1 d4
r1 r3
17
Assignment ()
assigns a relation with a relation variable, a convenient way to express complex queries, e.g.,
V R S(r)
Division ()
retrieve each (resultant) tuple in the dividend relation that “contain” all the tuple values of the divisor relation
attribute(divisor) attribute(dividend)
resultant schema has attribute(dividend) – attribute(divisor),
e.g., Has_taken(Name, Course#, Status) Core_course(Course#)
The Relational Model – Additional Operators
18
Relational Algebra
StatusName Has_taken Core_course
Has_taken
Steve
Name Course#Math 112
StatusFreshman
DanSteve CS 103
CS 142Freshman
Junior
DanDan Math 112
CS 143JuniorJunior
Linda CS 142 Freshman
Core_courseCourse#
MATH 112CS 142
Example.
JuniorDan
19
Division Operation
Example. Let r and s be the following relations:
r s
A
B
1
2
A B
12311134612
r s
20
Division Operation
r s
A B CA B
aaaaaaaa
C D
aabababb
E
11113111
D
ab
E
11
r s
Example. Let r and s be the following relations:
a a
21
Division Operation
Suited to queries that include the phrase “for all”.
Let r and s be relations on schemas R and S, respectively where
R = (A1, …, Am, B1, …, Bn)
S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
r s = { t | t R-S(r) u s ( tu r ) }
r s
22
Division Operation Property
Let q = r s
Then q is the largest relation satisfying q × s r
Definition in terms of the basic algebra operation. Let r(R) and s(S) be relations, and let S R
r s = R-S (r) – R-S ((R-S (r) × s) – R-S,S(r))
To see why
R-S,S(r) simply reorders attributes of r
R-S(R-S (r) × s) – R-S,S(r)), i.e., (iii), gives those tuples t in
R-S (r) such that for some tuple u s, tu r.
(i)
(ii)
(iii)
23
Sample Query: “Find all customers who have an account at all branches located in Brooklyn city.”
branch (branch-name, branch-city, assets)
depositor (customer-name, account-number)
account (account-number, branch-name, balance)
Division Operation
(branch-city = “Brooklyn” (branch))branch-name
(depositor account)customer-name, branch-name
24
Outer Join An extension of the join operation that avoids loss of
information.
Computes the (natural) join and then adds tuples from one relation that does 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.
• Will study precise meaning of comparisons with nulls later
25
Outer Join
Natural Join: loan Borrower
Left Outer Join: loan Borrower
loan-number amount
L-170L-230
30004000
customer-name
JonesSmith
branch-name
DowntownRedwood
loan-number amount
L-170L-230L-260
300040001700
customer-name
JonesSmithnull
branch-name
DowntownRedwoodPerryridge
loanloan-number amount
L-170L-230L-260
300040001700
borrower
customer-name loan-number
JonesSmithHayes
L-170L-230L-155
branch-name
DowntownRedwoodPerryridge
Example
26
Outer Join
Right Outer Join: loan borrower
Full Outer Join: loan borrower
loan-number amount
L-170L-230L-155
30004000null
customer-name
JonesSmithHayes
branch-name
DowntownRedwoodnull
loan-number amount
L-170L-230L-260L-155
300040001700null
customer-name
JonesSmithnullHayes
branch-name
DowntownRedwoodPerryridgenull
loanloan-number amount
L-170L-230L-260
300040001700
borrower
customer-name loan-number
JonesSmithHayes
L-170L-230L-155
branch-name
DowntownRedwoodPerryridge
Example
27
Left outer join ( ) defined by other algebraic operators
r s = (r s)
where (null, …, null) is on the schema S - R
Right outer join ( ) defined by other algebraic operators
r s = (r s) ( (s - S(s r)) { (null, …, null) } )
where (null, …, null) is on the schema R - S
Full outer join ( ) defined by left and right outer joins
r s = (r s) (r s)
Outer Join
( (r - R(r s)) { (null, …, null) } )
28
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.
For duplicate elimination and grouping, null is treated like avalue, and two nulls are assumed to be the same.
Alternative: assume each null is different from each other (adopted)
Both are arbitrary decisions
Relational (comparison) operations (such as >, <, =, etc.) involving null values return the special truth value unknown, which is null.
29
Null Values Three-valued logic using the truth value unknown:
AND: (true and unknown) = unknown
(false and unknown)= false
(unknown and unknown)= unknown
OR: (unknown or true) = true (unknown or false) = unknown (unknown or unknown) = unknown
NOT: (not unknown) = unknown
30
Null Values Selection: Comparisons with null values return the special
truth value unknown, i.e., not selected
Natural Join: joining tuples on null values cause mismatch
Projection: treating nulls like any other values when eliminating duplicates; our assumption is: difference
Union, Intersect, Difference: treating nulls just as the projection does, i.e., null values in corresponding columns in two tuples are treated as different
Outer join: behaving just like join operations, except on tuples that do not occur in the join results, which may be added
to the result, depending on the operation.
31
Deletion
A deletedelete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database.
Can delete only whole tuples; cannot delete values on only particular attributes
A deletion is expressed in relational algebra by:
r r – E
where r is a relation and E is a relational algebra query.
32
Deletion
Delete all account records in the Perryridge branch.
account account – branch-name = “Perryridge” (account)
Delete all loan records with amount in the range of 0 to 50.
loan loan – amount 0and amount 50 (loan)
Delete all accounts at branches located in Needham.
r1 branch-city = “Needham” (account branch)
r2 account-number, branch-name, balance (r1)
account account – r2
r3 customer-name, account-number (r2 depositor)
depositor depositor – r3
Example. branch (branch-name, branch-city, assets) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number)
33
Insertion To insert data into a relation, we either:
specify a tuple to be inserted, or
write a query whose result is a set of tuples to be inserted
In relational algebra, an insertion is expressed by:
r r E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.
34
Insertion
Insert information in the database specifying that “Smith” has $1200 in account A-973 at the “Perryridge” branch.
account account { (“A-973”, “Perryridge”, 1200) }
depositor depositor { (“Smith”, “A-973”) }
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.
r1 (branch-name = “Perryridge” (borrower loan))
account account loan-number, branch-name, 200 (r1)
depositor depositor customer-name, loan-number(r1)
Example. account (account-number, branch-name, balance) depositor (customer-name, account-number)
borrower (customer-name, loan-number) loan (loan-number, branch-name, amount)
35
Modification A mechanism to change a value in a tuple without changing
all the values in the tuple.
Use the generalized projection operator to do this task.
r F1, F2, …, Fn, (r)
Each Fi (1 i n) is either the ith attribute of r, if the ith attribute is not updated, or a predicate involving the ith
attribute, if it is the attribute to be updated.
Fi is an expression, involving only constants and the attributes of r, which gives the new value for the attribute.
36
Modification
Make interest payments by increasing all balances by 5%
account account-number, branch-name, balance * 1.05 (account)
Pay all accounts with balances over $10,000 6% interest and pay all others 5%
account account-number, branch-name, balance*1.06 (balance 10000 (account)) account-number, branch-name, balance*1.05 (balance 10000 (account))
Example. account (account-number, branch-name, balance)
37
The Relational Model Database Updates
A DB application provides a mechanism for updating data Method for updating data: use an interactive query/update language Updating means inserting, deleting, and modifying db data
Delete• Removes selected tuples from db tables
• Relational algebra expression: r r E, where r is a relation & E is a relational algebra query
Insertion• Add tuples into db tables
t = <a1, a2,…, an>, a tuple to be inserted into relation r(A1,…, An), ai dom(Ai), 1 i n
• Relational algebra expression: r r E
Modification ()• Changes a value in a tuple
• Relational algebra expression: A E(r), where A is an attr of relation r, & E is an arithmetic expression which includes constants & attrs of r.
38
The Relational Model Tuple Relational Calculus
A nonprocedural (declarative) query language
Formal DB language based on 1st order predicate calculus
Tuple variables range over tuples of a relation
Attribute values: t[A], value of attribute A in tuple variable t
Tuple calculus expressions:
{ t | P(t) }
i.e., t: predicate (formula) P is true for t
• Quantifier: (universal/for all), (existential/there exists)
• Free/bound tuple variable: a variable not quantified/quantified by a quantifier
e.g., t r1 s r2
• Formulas are made up of atoms
39
The Relational Model Tuple Relational Calculus
Atoms are of the following types:
• t r, where t is a tuple variable and r is a relation
• t1[X] t2[Y], where
– t1 and t2 are (same) tuple variables
– X, an attribute of t1 and Y, an attribute of t2
– { <, , , , >, }
– dom(X) and dom(Y) are -compatible
• t[X] C, where
– t is a tuple variable
– X, an attribute of t
– C, a constant in dom(X)
– , same as above, a comparison operator
40
The Relational Model Tuple Calculus Expressions
Well-formed formula (wff):
1. an atom is wff
2. if P is a wff, then
P is a wff
(P) is a wff
3. if P1 and P2 are wffs, then
P1 P2 is a wff
P1 P2 is a wff
P1 P2 is a wff
4. if P(t) is a wff, where t is free, then
t r (P(t)) is a wff
t r (P(t)) is a wff
41
The Relational Model Relational Algebra Expressions vs. Tuple Calculus Expressions
Relational Alg. Expression Tuple Calculus Expression
Select: C(r) { t | t r C’ }, where C’ is the condition C with each appearance of attribute A of r
replaced by t[A]
Project: A1, …, An(r) { t | u r (t[A1] = u[A1] ... t[An] = u[An])}
Cartesian: r(R) s(S) { t | x r (y s
(t[A1] = x[A1] ... t[An] = x[An] t[B1] = y[B1] … t[Bm] = y[Bm])) }
where R = {A1, …, An} and S = {B1, …, Bm}
Union: r s { t | t r t s }
Difference: r s { t | t r (t s) }
Product
42
Sample Tuple Calculus Queries
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 | n loan (t[loan-number] = n[loan-number] n[amount] 1200) }
Example. loan (loan-number, branch-name, amount)
43
Find the names of all customers having a loan, an account, or both at the bank.
{ t | b borrower (t [customer-name] = b[customer-name]) d depositor (t [customer-name] = d[customer-name])}
Find the names of all customers who have a loan and an account at the bank.
{ t | b borrower (t [customer-name] = b[customer-name]) d depositor (t [customer-name] = d[customer-name]) }
Example. borrower (customer-name, loan-number) depositor (customer-name, account-number)
Sample Tuple Calculus Queries
44
Find the names of all customers having a loan at the Perryridge branch.
{ t | b borrower (t [customer-name] = b[customer-name] n loan (b[loan-number] = n[loan-number]
n[branch-name] = “Perryridge”)) }
Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank.
{ t | b borrower (t [customer-name] = b[customer-name] n loan (b[loan-number] = n[loan-number]
n[branch-name] = “Perryridge”)) d depositor (d[customer-name] = t [customer-name])}
Example. borrower (customer-name, loan-number) loan (loan-number, branch-name, amount) depositor (customer-name, account-number)
/
Sample Tuple Calculus Queries
45
Find the names of all customers having a loan from the Perryridge branch, and the cities they live in.
{ t | n loan (n[branch-name] = “Perryridge” b borrower (b[loan-number] = n[loan-number] t [customer-name] = b[customer-name] c customer (c[customer-name] = b[customer-name]
t [customer-city] = c[customer-city]))) }
Example.
loan (loan-number, branch-name, amount) borrower (customer-name, loan-number)
customer (customer-name, customer-street, customer-city)
Sample Tuple Calculus Queries
46
Find the names and cities of all customers who have an account at all branches located in Brooklyn.
{ t | c customer (t [customer-name] = c[customer-name] t [customer-city] = c[customer-city] b branch (b[branch-city] = “Brooklyn” a account (a[branch-name] = b[branch-name] d depositor (d[account-number] = a[account-number] d[customer-name] = c[customer-name]))))}
Example.
customer (customer-name, customer-street, customer-city) depositor (customer-name, account-number) account (account-number, branch-name, balance)
branch (branch-name, branch-city, assets)
Sample Tuple Calculus Queries
47
The Relational Model Safe Tuple Calculus Expressions
Expressions generate infinite relations, e.g., { t | (t r) }, is not safe
• An expression E: { t | P(t) } is safe if each value in E dom(P)
• dom(P) = { Constants in P } { Attribute values of relations in P }