cis552relational model1 structure of relational database relational algebra extended...
TRANSCRIPT
CIS552 Relational Model 1
Relational Model
• Structure of Relational Database• Relational Algebra• Extended Relational-Algebra-Operations• Modification of the Database
CIS552 Relational Model 2
Basic Structure• Given sets A1, A2, …, An a relation r is a subset of
A1 A2 … An
Thus a relation is a set of n-tuples (a1, a2, …, an) where ai Ai
• Example: Ifcustomer-name = {Jones, Smith, Curry, Lindsay}customer-street = {Main, North, Park}customer-city = { Harrison, Rye, Pittsfield}
Then r = {(Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over customer-name customer-street customer-city
CIS552 Relational Model 3
Relation Schema
• A1, A2, …, An are attributes
• R = (A1, A2, …, An ) is a relation schema
Customer-schema = (customer-name, customer-street,
customer-city)• r(R) is a relation on the relation schema R
customer (Customer-schema)
CIS552 Relational Model 4
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.
customer-name customer-street customer-city
Jones Main HarrisonSmith North RyeCurry North RyeLindsay Park Pittsfield
customer
CIS552 Relational Model 5
Keys
• Let K R
• K is a superkey of R if values for K are sufficient to uniquely identify a 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 superkey 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
CIS552 Relational Model 6
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-many relationship sets, above super key is also the primary key.
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.
CIS552 Relational Model 7
Query Languages• Language in which user requests information form
the database.• Categories of languages:
– Procedural– Non-procedural
• Formal languages:– Relational Algebra– Tuple Relational Calculus– Domain Relational Calculus
• Formal languages form underlying basis of query languages that people use.
CIS552 Relational Model 8
Relational Algebra• Procedural language
• Six basic operators
– select
– project
– union
– set difference
– cartesian product
– rename
• The operators take two or more relations as inputs and give a new relation as a result.
CIS552 Relational Model 9
Select Operation• Notation p(r)• Defined as:
p(r) = {t | t r and p(t)}• where P is a formula in propositional calculus,
dealing with terms of the form: attribute attribute or constant
connected by: (and), (or), (not)
CIS552 Relational Model 10
Select Operation - Example
• Relation r:
A=B D > 5(r)
A B C D 1 7 8 6 6 3 2 8
A B C D 1 7 2 8
CIS552 Relational Model 11
Project Operation
• Notation:
A1, A2, …, Ak(r)where A1, A2 are attributes and r is a relation.
• The result is defined as the relation of k columns
obtained by erasing the columns that are not listed
• Duplicate rows are removed from result, since relations are sets
CIS552 Relational Model 12
Project Operation - Example
• Relation r:
A, C (r)
A B C 10 1 20 1 30 1 40 2
A C 1 1 1 2
A C 1 1 2
=
CIS552 Relational Model 13
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 (e.g. 2nd column of r deals with the same type of values as does the 2nd column of s)
CIS552 Relational Model 14
Union Operation - Example
• Relations r, s:
• r s
rA B 1 2 1
sA B 2 3
A B 1 2 1 3
CIS552 Relational Model 15
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
CIS552 Relational Model 16
Set Difference Operation - Example
rA B 1 2 1
sA B 2 3
r-sA B 1 1
CIS552 Relational Model 17
Cartesian-Product Operation
• Notation: r s• Defined as:
r s = { tq | 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.
CIS552 Relational Model 18
Cartesian-Product Operation - Example
rA B 1 2
r sA B C D E 1 10 + 1 10 + 1 20 - 1 10 - 2 10 + 2 10 + 2 20 - 2 10 -
sC D E 10 + 10 + 20 - 10 -
CIS552 Relational Model 19
Banking Tables
Name SSN City StreetJones 123-45-6789 New Bedford MainSmith 246-80-1357 Providence MapleJohnson 999-22-3456 Portsmouth Pine
… … … …
Customer
LNum Amount001 10000.00002 2500.00003 3000.00… …
LoanLNum SSN
001 123-45-6789002 999-22-3456003 123-45-6789
… …
Borrower
CIS552 Relational Model 20
Customer Borrower
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
CIS552 Relational Model 21
Customer Borrower
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
Name SSN City Street LNum SSNJones 123-45-6789 New Bedford Main 001 123-45-6789Jones 123-45-6789 New Bedford Main 002 999-22-3456Jones 123-45-6789 New Bedford Main 003 123-45-6789Smith 246-80-1357 Providence Maple 001 123-45-6789Smith 246-80-1357 Providence Maple 002 999-22-3456Smith 246-80-1357 Providence Maple 003 123-45-6789Johnson 999-22-3456 Portsmouth Pine 001 123-45-6789Johnson 999-22-3456 Portsmouth Pine 002 999-22-3456Johnson 999-22-3456 Portsmouth Pine 003 123-45-6789
… … … … … …
CIS552 Relational Model 22
Composition of Operations
• Can build expressions using multiple operations• Example: A= C (r s)• Natural Join
– Notation: r s
– Let r and s be relations on schemas R and S respectively. The result is a relation on schema R S which is obtained by considering each pair of tuples tr from r and ts from s.
– If tr and ts have the same value on each of the attributes in R S, a tuple t is added to the result, where
• t has the same values as tr on the attributes in R
• t has the same values as ts on the attributes in S
CIS552 Relational Model 23
Composition of Operations (Cont.)
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 s) )
CIS552 Relational Model 24
r sA B C D E 1 a 1 a 1 a 1 a 2 b
Natural Join Operation - Example
rA B C D 1 a 2 a 4 b 1 a 2 b
sB D E a a a b b
CIS552 Relational Model 25
Division Operation
r s• 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) }
CIS552 Relational Model 26
Division Operation (Cont.)
• PropertyLet q = r s Then q is the largest relation satisfying: q s r
• Definition in terms of the basic algebra operationLet 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)) gives those tuples t in
R-S(r) such that for some tuple u s, tu r.
CIS552 Relational Model 27
Division Operation - Example
A B 1 2 3 1 1 1 3 4 6 1 2
B
A=
CIS552 Relational Model 28
Another Division Example
A B C D E a a 1 a a 1 a b 1 a a 1 a b 3 a a 1 a b 1 a b 1
D Ea 1b 1
A B C a a =
CIS552 Relational Model 29
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 the 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 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
CIS552 Relational Model 30
Example Queries • Find those customers who have an account from at least the
“Downtown” and “Updown” branches.
• Query 1cn (bn = “Downtown” (depositor account )) cn (bn = “Uptown” (depositor account ))
where cn denotes customer-name and bn denotes branch-name
• Query 2cn, bn (depositor account )) temp(bn) ( {(“Downtown”), (“Uptown”)} )
CIS552 Relational Model 31
Example Queries (2)
• Find those customers who have accounts at all branches located in Brooklyn.
customer-name, branch-name (depositor account )) branch-name (branch-city = “Brooklyn”
(branch))
CIS552 Relational Model 32
Sample Problems
• Consider the following relational schema:Flights (flno, from , to, distance, departs)Aircraft (aid, aname, range)Certified (eid, aid)Employees (eid, ename, salary)
• By definition, pilots are those employees who are certified on at least one aircraft.
• An aircraft can be used for any flight provided it has sufficient range.
• Pilots can pilot any flight provided they are certified on an aircraft with sufficient range.
CIS552 Relational Model 33
Sample Problems (cont.)
1. Find eid’s of pilots who are certified on some Boeing.2. Find names of pilots who are certified on some Boeing.3. Find aid’s of aircraft that can fly non-stop from LA to NY.4. Find flno of flights that can be piloted by every pilot whose salary is
over $100,000.5. Solve problem 4 without using the division operator.6. Find names of pilots who can operate planes with a range greater than
3,000 miles, but are not certified on any Boeing.
7. Find eid of employee(s) with the highest salary.
8. Find eid of employee(s) with the second highest salary.
9. Find eid of employee(s) certified on the most aircraft.
10. Find eid’s of employees certified on exactly three aircraft.