Download presentation
Presentation is loading. Please wait.
Published byTerence Chapman Modified over 9 years ago
1
Chapters 2 & 6 The Relational Model
2
2 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 = A 1, A 2, …, A n, is an ordered set of values, i.e., t = where v 1 dom(A 1 ), v 2 dom(A 2 ), …, v n dom(A n ). Relation r(R): a set of tuples r(R) dom(A 1 ) dom(A 2 ) … dom(A n ) r is a relation instance
3
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 t 1, t 2 r(R), t 1 t 2, then t 1 [K] t 2 [K]
4
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
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
6 The Relational Model Fundamental relational operators: select ( ) project ( ) cartesian produce ( ) union ( ) difference ( ) rename ( ) Additional relational operators: intersection ( ) natural join ( ) division ( ) assignment ( )
7
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
8 Relational Algebra A = “a ¹ ” (r ¹ ) ABC a1a1 b1b1 c1c1 a1a1 b3b3 c1c1 A,C (r¹)A,C (r¹) AC a1a1 c1c1 a2a2 c2c2 r¹r¹ ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 Example.
9
9 The Relational Model Cartesian Product ( ) combines information from two relations r ¹ (A ¹, A 2, …, A n ) r 2 (B ¹, B 2, …, B m ) = r 3 (A ¹, …, A n, B ¹, …, B m ), and t r 3, t[A ¹, …, A n ] r 1, t[B ¹, …, B m ] r 2 if |r ¹ | = i and |r 2 | = j Union ( ) r 1 r 2 : combines tuples in r 1 with those tuples in r 2 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 |r 1 r 2 | = i j
10
10 Relational Algebra Example (continued). r¹r¹ ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 r2r2 ABC a1a1 b3b3 c1c1 a2a2 b1b1 c1c1 r¹ r2r¹ r2 r¹.Ar¹.Ar2.Ar2.Ar¹.Br¹.Br¹.Cr¹.Cr2.Br2.Br2.Cr2.C a¹a¹ a¹a¹ b¹b¹ c¹c¹ b3b3 c1c1 a¹a¹ a2a2 b¹b¹ c¹c¹ b1b1 c1c1 a2a2 a¹a¹ b2b2 c2c2 b3b3 c1c1 a2a2 a2a2 b2b2 c2c2 b1b1 c1c1 a¹a¹ a¹a¹ b3b3 c¹c¹ b3b3 c1c1 a¹a¹ a2a2 b3b3 c¹c¹ b1b1 c1c1 r ¹ r 2 ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 a2a2 b1b1 c1c1
11
11 The Relational Model Set Difference ( ) r 1 r 2 : contains tuples which occur in r 1 but not in r 2 the two involved relations must be union-compatible Rename ( ) s (r) returns relation r under the name s, or B A (r) returns attribute A under the attribute name B
12
12 Relational Algebra Example (continued). r¹r¹ ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 r2r2 ABC a1a1 b3b3 c1c1 a2a2 b1b1 c1c1 s(r¹)s(r¹) ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 r¹ r2r¹ r2 ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 =
13
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 ( ) r 1 r 2 contains common tuples in r 1 and r 2 the two involved relations must be union-compatible can be rewritten using set difference, i.e., r s = r (r s)
14
14 Relational Algebra Example (continued). r¹r¹ ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 r2r2 ABC a1a1 b3b3 c1c1 a2a2 b1b1 c1c1 r 1 r 2 ABC a1a1 b3b3 c1c1
15
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. A 1 = s. A 1 ... r.A n = s.A n (r s)) where R S = {A ¹, …, A n }
16
16 Relational Algebra r¹r¹ ABC a1a1 b1b1 c1c1 a2a2 b2b2 c2c2 a1a1 b3b3 c1c1 r3r3 BD b3b3 d4d4 b1b1 d5d5 b1b1 d3d3 Example (continued). ABC a1a1 b1b1 c1c1 D d3d3 a1a1 b1b1 c1c1 d5d5 a1a1 b3b3 c1c1 d4d4 r1 r3r1 r3
17
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
18 Relational Algebra StatusName Has_taken Core_course Has_taken Steve NameCourse# Math 112 Status Freshman Dan SteveCS 103 CS 142 Freshman Junior Dan Math 112 CS 143 Junior LindaCS 142Freshman Core_course Course# MATH 112 CS 142 Example. JuniorDan
19
19 Division Operation Example. Let r and s be the following r elations: r s A B 1212 AB 1231113461212311134612 r s
20
20 Division Operation r sr s ABC AB aaaaaaaaaaaaaaaa CD aabababbaabababb E 1111311111113111 D abab E 1111 r s Example. Let r and s be the following r elations: a a
21
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 = (A 1, …, A m, B 1, …, B n ) S = (B 1, …, B n ) The result of r s is a relation on schema R – S = (A 1, …, A m ) r s = { t | t R-S (r) u s ( tu r ) } r sr s
22
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
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
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
25 Outer Join Natural Join: loan Borrower Left Outer Join: loan Borrower loan-numberamount L-170 L-230 3000 4000 customer-name Jones Smith branch-name Downtown Redwood loan-numberamount L-170 L-230 L-260 3000 4000 1700 customer-name Jones Smith null branch-name Downtown Redwood Perryridge loan loan-numberamount L-170 L-230 L-260 3000 4000 1700 borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 branch-name Downtown Redwood Perryridge Example
26
26 Outer Join n Right Outer Join: loan borrower n Full Outer Join: loan borrower loan-numberamount L-170 L-230 L-155 3000 4000 null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-numberamount L-170 L-230 L-260 L-155 3000 4000 1700 null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null loan loan-numberamount L-170 L-230 L-260 3000 4000 1700 borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L-155 branch-name Downtown Redwood Perryridge Example
27
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
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 a value, 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
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
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
31 Deletion delete A delete 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
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 0 and amount 50 (loan) Delete all accounts at branches located in Needham. r 1 branch-city = “Needham” (account branch) r 2 account-number, branch-name, balance (r 1 ) account account – r 2 r 3 customer-name, account-number (r 2 depositor) depositor depositor – r 3 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
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
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. r 1 ( branch-name = “Perryridge” (borrower loan)) account account loan-number, branch-name, 200 (r 1 ) depositor depositor customer-name, loan-number (r 1 ) Example. account (account-number, branch-name, balance) depositor (customer-name, account-number) borrower (customer-name, loan-number) loan (loan-number, branch-name, amount)
35
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 F i (1 i n) is either the i th attribute of r, if the i th attribute is not updated, or a predicate involving the i th attribute, if it is the attribute to be updated. F i is an expression, involving only constants and the attributes of r, which gives the new value for the attribute.
36
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
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 =, a tuple to be inserted into relation r(A 1,…, A n ), a i dom(A i ), 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
38 The Relational Model Tuple Relational Calculus A nonprocedural (declarative) query language Formal DB language based on 1 st 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 r 1 s r 2 Formulas are made up of atoms
39
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 t 1 [X] t 2 [Y], where – t 1 and t 2 are (same) tuple variables – X, an attribute of t 1 and Y, an attribute of t 2 – {, } – 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
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 P 1 and P 2 are wffs, then P 1 P 2 is a wff P 1 P 2 is a wff P 1 P 2 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
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: A 1, …, A n (r) { t | u r (t[A 1 ] = u[A 1 ] ... t[A n ] = u[A n ])} Cartesian: r(R) s(S) { t | x r ( y s (t[A 1 ] = x[A 1 ] ... t[A n ] = x[A n ] t[B 1 ] = y[B 1 ] … t[B m ] = y[B m ])) } where R = {A 1, …, A n } and S = {B 1, …, B m } Union: r s { t | t r t s } Difference: r s { t | t r (t s) } Product
42
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
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
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
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
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
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 }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.