Relational ModelRelational Model Database System Concepts, 5th Ed.y p , Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Chapter 2: Relational ModelChapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-OperationsFundamental Relational Algebra Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations Modification of the Database Silberschatz, Korth and Sudarshan2.2Database System Concepts - 5th Edition, Oct 5, 2006 Example of a RelationExample of a Relation Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5th Edition, Oct 5, 2006 Basic StructureBasic Structure Formally, given sets D1, D2, .... Dn a relation r is a subset of D1 x D2 x ... x Dn Thus, a relation is a set of n-tuples (a1, a2, ..., an) where each ai Di Example: If customer_name = {Jones, Smith, Curry, Lindsay, ...} /* Set of all customer names */ customer_street = {Main, North, Park, ...} /* set of all street names*/ customer_city = {Harrison, Rye, Pittsfield, ...} /* set of all city names */ Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (C N th R )(Curry, North, Rye), (Lindsay, Park, Pittsfield) } is a relation over Silberschatz, Korth and Sudarshan2.4Database System Concepts - 5th Edition, Oct 5, 2006 customer_name x customer_street x customer_city Attribute TypesAttribute Types Each attribute of a relation has a name The set of allowed values for each attribute is called the domain of theThe set of allowed values for each attribute is called the domain of the attribute Attribute values are (normally) required to be atomic; that is, indivisible E h l f ib b b E.g. the value of an attribute can be an account number, but cannot be a set of account numbers Domain is said to be atomic if all its members are atomic The special value null is a member of every domain The null value causes complications in the definition of many operations We shall ignore the effect of null values in our main presentation and consider their effect later Silberschatz, Korth and Sudarshan2.5Database System Concepts - 5th Edition, Oct 5, 2006 Relation SchemaRelation Schema A1, A2, ..., An are attributes R = (A1, A2, ..., An ) is a relation schema Ordering of attributes is important! Example: Customer_schema = (customer_name, customer_street, customer_city) r(R) denotes a relation r on the relation schema R Example: customer (Customer_schema) Silberschatz, Korth and Sudarshan2.6Database System Concepts - 5th Edition, Oct 5, 2006 Relation InstanceRelation 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 attributes (or columns) Jones customer_name Main customer_street Harrison customer_city t lSmith Curry Lindsay North North Park Rye Rye Pittsfield tuples (or rows) customer Silberschatz, Korth and Sudarshan2.7Database System Concepts - 5th Edition, Oct 5, 2006 Relations are UnorderedRelations are Unordered Order of tuples is irrelevant (tuples may be stored in an arbitrary order) Example: account relation with unordered tuplesExample: account relation with unordered tuples Silberschatz, Korth and Sudarshan2.8Database System Concepts - 5th Edition, Oct 5, 2006 DatabaseDatabase A database consists of multiple relations Information about an enterprise is broken up into parts, with each relationp p p , storing one part of the information account : stores information about accounts depositor : stores information about which customer owns which accountdepositor : stores information about which customer owns which account customer : stores information about customers Storing all information as a single relation such as b k( t b b l t )bank(account_number, balance, customer_name, ..) results in repetition of informationp e.g., if two customers own an account (What gets repeated?) the need for null values e.g., to represent a customer without an account Normalization theory (Chapter 7: Relational Database Design) deals with how to design relational schemas Silberschatz, Korth and Sudarshan2.9Database System Concepts - 5th Edition, Oct 5, 2006 to design relational schemas TheThe customercustomer RelationRelation Silberschatz, Korth and Sudarshan2.10Database System Concepts - 5th Edition, Oct 5, 2006 TheThe depositordepositor RelationRelation Silberschatz, Korth and Sudarshan2.11Database System Concepts - 5th Edition, Oct 5, 2006 KeysKeys Let K R K is a superkey of R if values for K are sufficient to identify a unique tuple ofp y y q p each possible relation r(R) by "possible r " we mean a relation r that could exist in the enterprise we are modelingare 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 l ll d i t d t iour examples small, and instead assume customer names are unique. Silberschatz, Korth and Sudarshan2.12Database System Concepts - 5th Edition, Oct 5, 2006 Keys (Cont.)Keys (Cont.) K is a candidate key if K is minimal Example: {customer name} is a candidate key for Customer, since itExample: {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, hchanges. E.g. email address is unique, but may change Silberschatz, Korth and Sudarshan2.13Database System Concepts - 5th Edition, Oct 5, 2006 Query LanguagesQuery Languages Language in which user requests information from the database. Categories of languagesCategories of languages Procedural Non-procedural, or declarative "Pure" languages: Relational algebra T l l ti l l l Tuple relational calculus Domain relational calculus Pure languages form underlying basis of query languages that people Pure languages form underlying basis of query languages that people use. Silberschatz, Korth and Sudarshan2.15Database System Concepts - 5th Edition, Oct 5, 2006 Relational AlgebraRelational Algebra Procedural language Six basic operatorsSix basic operators select: project: project: union: set difference: ­ Cartesian product: × rename: The operators take one or two relations as inputs and produce a new relation as a result. Silberschatz, Korth and Sudarshan2.16Database System Concepts - 5th Edition, Oct 5, 2006 Select OperationSelect Operation ­­ ExampleExample Relation r A B C D 1 5 7 7 12 23 3 10 (r) A=B D > 5 (r) A B C D 1 7 1 23 7 10 Silberschatz, Korth and Sudarshan2.17Database System Concepts - 5th Edition, Oct 5, 2006 Select OperationSelect Operation Notation: p(r) p is called the selection predicate Defined as: p(r) = {t | t r and p(t)} where p is a formula in propositional calculus: formula := term term term ( term )( term ) term := expr expr expr ( expr ) tt ib texpr := attribute constant is one of: (and), (or), (not) is one of: =, , >, , <, Example of selection: branch_name=`Perryridge' (account) Silberschatz, Korth and Sudarshan2.18Database System Concepts - 5th Edition, Oct 5, 2006 Project OperationProject Operation ­­ ExampleExample Relation r: A B C 10 1 10 20 30 1 1 1 30 40 1 2 A C A CA,C (r) 1 1 = 1 1 1 2 2 Silberschatz, Korth and Sudarshan2.19Database System Concepts - 5th Edition, Oct 5, 2006 Project OperationProject Operation Notation: )(21 rkAAA where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing )(,,, 21 kAAA y g the columns that are not listed Duplicate rows removed from result, since relations are sets Example: To eliminate the branch name attribute of account Example: To eliminate the branch_name attribute of account account_number, balance (account) Silberschatz, Korth and Sudarshan2.20Database System Concepts - 5th Edition, Oct 5, 2006 Union OperationUnion Operation ­­ ExampleExample Relations r, s: A B A Br s 1 2 2 3 1 A B r s: 1 2 1 1 3 Silberschatz, Korth and Sudarshan2.21Database System Concepts - 5th Edition, Oct 5, 2006 Union OperationUnion Operation Notation: r s Defined as: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 2ndof r deals with the same type of values as does the 2 column of s) Example: to find all customers with either an account or a loan customer_name (depositor) customer_name (borrower) Silberschatz, Korth and Sudarshan2.22Database System Concepts - 5th Edition, Oct 5, 2006 Set Difference OperationSet Difference Operation ­­ ExampleExample Relations r, s: A B A B 1 2 2 3 1 r s r ­ s: A B 1 1 1 Silberschatz, Korth and Sudarshan2.23Database System Concepts - 5th Edition, Oct 5, 2006 Set Difference OperationSet Difference Operation Notation r ­ s Defined as: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 r and s must have the same arity attribute domains of r and s must be compatible Silberschatz, Korth and Sudarshan2.24Database System Concepts - 5th Edition, Oct 5, 2006 CartesianCartesian--Product OperationProduct Operation ­­ ExampleExample Relations r, s: A B C D Er s 1 2 10 10 20 a a b r ×s 20 10 b b A B 1 1 C D 10 10 E a 1 1 1 2 10 20 10 10 a b b a 2 2 2 2 10 10 20 10 a a b b Silberschatz, Korth and Sudarshan2.25Database System Concepts - 5th Edition, Oct 5, 2006 2 10 b CartesianCartesian--Product OperationProduct Operation Notation r ×s Defined as:Defined as: r ×s = { tq | t r and q s} where tq means the concatenation of tuples t and q to produce a singlewhere tq means the concatenation of tuples t and q to produce a single tuple. 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. Silberschatz, Korth and Sudarshan2.26Database System Concepts - 5th Edition, Oct 5, 2006 Composition of OperationsComposition of Operations Can build expressions using multiple operations Example: A=C(r ×s) r ×s A B C D Er s r ×s A B 1 1 C D 10 10 E a a 1 2 10 10 20 a a b 1 1 1 2 10 20 10 10 a b b a 20 10 b b 2 2 2 2 10 10 20 10 a a b b A=C(r ×s) 2 10 b A B C D EA B C D E 1 2 10 10 a a Silberschatz, Korth and Sudarshan2.27Database System Concepts - 5th Edition, Oct 5, 2006 2 2 20 b Rename OperationRename Operation Allows us to name, and therefore to refer to, the results of relationalalgebra expressions. Allows us to refer to a relation by more than one name. Example of naming a relation: x (E) returns the expression E under the name Xp Example of naming a relation and its attributes: If a relational-algebra expression E has arity n, then t th lt f i E d th X d ith th )(),...,,( 21 EnAAAx returns the result of expression E under the name X, and with the attributes renamed to A1 , A2 , ...., An . Silberschatz, Korth and Sudarshan2.28Database System Concepts - 5th Edition, Oct 5, 2006 Banking ExampleBanking Example branch (branch_name, branch_city, assets) customer (customer name customer street customer city)customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer name account number)depositor (customer_name, account_number) borrower (customer_name, loan_number) Silberschatz, Korth and Sudarshan2.29Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer name loan number) Find all loans of over $1200 (loan) borrower (customer_name, loan_number) Find the loan number for each loan of an amount greater than $1200 amount > 1200 (loan) $1200 loan_number (amount > 1200 (loan)) Find the names of all customers who have a loan, an account, or both, from the bank customer_name (borrower) customer_name (depositor) Silberschatz, Korth and Sudarshan2.30Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer name loan number) Find the names of all customers who have a loan at the Perryridge branch borrower (customer_name, loan_number) branch. customer_name (branch_name="Perryridge" (borrower loan number = loan loan number (borrower × loan))) Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank ( borrower.loan_number loan.loan_number branch but do not have an account at any branch of the bank. customer_name (branch_name = "Perryridge" ( (borrower × loan)))(borrower.loan_number = loan.loan_number(borrower × loan))) ­ customer_name(depositor) Silberschatz, Korth and Sudarshan2.31Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries Find the names of all customers who have a loan at the Perryridge branch. Query 1 ( (customer_name (branch_name = "Perryridge" ( borrower.loan_number = loan.loan_number (borrower × loan))) Query 2 customer_name(loan.loan_number = borrower.loan_number ( (branch name = "Perryridge" (loan)) × borrower))(branch_name = Perryridge (loan)) borrower)) Silberschatz, Korth and Sudarshan2.32Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries Find the largest account balance account (account_number, branch_name, balance) Find the largest account balance Strategy: Find those balances that are not the largest ­ Rename account relation as d so that we can compare each account balance with all others Use set difference to find those account balances that were not found in the earlier step. The query is: balance(account) - account.balance (account balance < d balance (account x d (account)))( account.balance < d.balance ( d ( ))) Silberschatz, Korth and Sudarshan2.33Database System Concepts - 5th Edition, Oct 5, 2006 Formal DefinitionFormal Definition A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions: E1 E2 E1 ­ E2 E1 × E2 (E ) P is a predicate on attributes in E p (E1), P is a predicate on attributes in E1 s(E1), S is a list consisting of some of the attributes in E1 Silberschatz, Korth and Sudarshan2.34Database System Concepts - 5th Edition, Oct 5, 2006 x (E1), x is the new name for the result of E1 Additional OperationsAdditional Operations We define additional operations that do not add any power to the relational algebra, but that simplify common queries.relational algebra, but that simplify common queries. Set intersection N t l j i Natural join Division Assignment Silberschatz, Korth and Sudarshan2.35Database System Concepts - 5th Edition, Oct 5, 2006 SetSet--Intersection OperationIntersection Operation Notation: r s Defined as:Defined as: r s = { t | t r and t s } Assume: r, s have the same arity attributes of r and s are compatible N t ( ) Note: r s = r ­ (r ­ s) Silberschatz, Korth and Sudarshan2.36Database System Concepts - 5th Edition, Oct 5, 2006 SetSet--Intersection OperationIntersection Operation ­­ ExampleExample Relation r, s: A B A B 1 2 1 2 3 r s r s A B 2 Silberschatz, Korth and Sudarshan2.37Database System Concepts - 5th Edition, Oct 5, 2006 NaturalNatural--Join OperationJoin Operation Notation: r s Let r and s be relations on schemas R and S respectively Let r and s be relations on schemas R and S respectively. Then, r s is a relation on schema R S obtained as follows: Consider 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, add a tuple t to the result, where t has the same value as t on r t has the same value as tr on r t has the same value as ts on s Example: Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as: ( ( )) Silberschatz, Korth and Sudarshan2.38Database System Concepts - 5th Edition, Oct 5, 2006 r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s)) Natural Join OperationNatural Join Operation ­­ ExampleExample Relations r, s: A B 1 C D a B 1 D a E 2 4 1 a b a 3 1 2 3 a a b b 2 b 3 b r s A B 1 C D a E r s 1 1 1 a a a Silberschatz, Korth and Sudarshan2.39Database System Concepts - 5th Edition, Oct 5, 2006 2 b Division OperationDivision Operation Notation: r s Suited to queries that include the phrase "for all". Let r and s be relations on schemas R and S respectively hwhere R = (A1, ..., Am , B1, ..., Bn ) S = (B B ) S = (B1, ..., Bn) The result of r s is a relation on schema R ­ S = (A1 A )R S (A1, ..., Am) r s = { t | t R-S (r) u s ( tu r ) } where tu means the concatenation of tuples t and u towhere tu means the concatenation of tuples t and u to produce a single tuple. Silberschatz, Korth and Sudarshan2.40Database System Concepts - 5th Edition, Oct 5, 2006 Division OperationDivision Operation ­­ ExampleExample Relations r, s: B A Br s 1 2 1 2 3 s 2 3 1 1 1 1 3 4 6 r s A 6 1 2 r s A Silberschatz, Korth and Sudarshan2.41Database System Concepts - 5th Edition, Oct 5, 2006 Another Division ExampleAnother Division Example A B C D E Relations r, s: D Er sA B a a C D a a E 1 1 D a b E 1 1 r s a a a b a b 1 1 3 1 a a a a b b 1 1 1 r s A B CA B a a C Silberschatz, Korth and Sudarshan2.42Database System Concepts - 5th Edition, Oct 5, 2006 Division Operation (Cont.)Division Operation (Cont.) 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 RLet 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 rR S,S ( ) p y 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. Silberschatz, Korth and Sudarshan2.43Database System Concepts - 5th Edition, Oct 5, 2006 Assignment OperationAssignment 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 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 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 the left of the . May use variable in subsequent expressions. Silberschatz, Korth and Sudarshan2.44Database System Concepts - 5th Edition, Oct 5, 2006 Bank Example QueriesBank Example Queries Find the names of all customers who have a loan and an account at bank. customer_name (borrower) customer_name (depositor) Find the name of all customers who have a loan at the bank and the loan amountloan amount customer_name, loan_number, amount (borrower loan) Silberschatz, Korth and Sudarshan2.45Database System Concepts - 5th Edition, Oct 5, 2006 Bank Example QueriesBank Example Queries Find all customers who have an account from at least the "Downtown" and the Uptown" branches. Query 1 customer_name (branch_name = "Downtown" (depositor account )) customer_name (branch_name = "Uptown" (depositor account)) Q 2 Query 2 customer_name, branch_name (depositor account) t (b h ) ({("Downtown" ) ("Uptown" )}) temp(branch_name) ({( Downtown ), ( Uptown )}) Note that Query 2 uses a constant relation. Silberschatz, Korth and Sudarshan2.46Database System Concepts - 5th Edition, Oct 5, 2006 Bank Example QueriesBank Example Queries Find all customers who have an account at all branches located in Brooklyn city.y y customer_name, branch_name (depositor account) b h (b h it "B kl " (branch)) branch_name (branch_city = "Brooklyn" (branch)) Silberschatz, Korth and Sudarshan2.47Database System Concepts - 5th Edition, Oct 5, 2006 Extended RelationalExtended Relational--Algebra OperationsAlgebra Operations Generalized Projection Aggregate FunctionsAggregate Functions Outer Join Silberschatz, Korth and Sudarshan2.48Database System Concepts - 5th Edition, Oct 5, 2006 Generalized ProjectionGeneralized Projection Extends the projection operation by allowing arithmetic functions to be used in the projection list. )(,,, 21 EnFFF E is any relational-algebra expression Each of F1, F2, ..., Fn are are arithmetic expressions involving constants and attributes in the schema of Eand attributes in the schema of E. Given relation credit_info(customer_name, limit, credit_balance), find how much more each person can spend: customer_name, limit ­ credit_balance (credit_info) Silberschatz, Korth and Sudarshan2.49Database System Concepts - 5th Edition, Oct 5, 2006 Aggregate Functions and OperationsAggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational G algebra Aggregate operation in relational G algebra )()(,,(),(,,, 221121 Ennn AFAFAFGGG G E is any relational-algebra expression G1, G2 ..., Gn is a list of attributes on which to group (can be empty) Each Fi is an aggregate function Each Ai is an attribute name Silberschatz, Korth and Sudarshan2.50Database System Concepts - 5th Edition, Oct 5, 2006 Aggregate OperationAggregate Operation ­­ ExampleExample Relation r A B CA B C 7 7 7 3 10 10 G sum(C) (r) sum(C) 2727 Silberschatz, Korth and Sudarshan2.51Database System Concepts - 5th Edition, Oct 5, 2006 Aggregate OperationAggregate Operation ­­ ExampleExample Relation account grouped by branch-name: branch_name account_number balance Perryridge P id A-102 A 201 400 900Perryridge Brighton Brighton Redwood A-201 A-217 A-215 A 222 900 750 750 700 branch_name G sum(balance) (account) Redwood A-222 700 branch_name sum(balance) Perryridge 1300y g Brighton Redwood 1500 700 Silberschatz, Korth and Sudarshan2.52Database System Concepts - 5th Edition, Oct 5, 2006 Aggregate Functions (Cont.)Aggregate Functions (Cont.) Result of aggregation does not have a name Can use rename operation to give it a nameCan use rename operation to give it a name x(branch_name,sum_balance) ( branch name G sum(balance) (account ) )branch_name G sum(balance) ( ) ) For convenience, we permit renaming as part of aggregate tioperation branch_name G sum(balance) as sum_balance (account) Silberschatz, Korth and Sudarshan2.53Database System Concepts - 5th Edition, Oct 5, 2006 Outer JoinOuter Join An extension of the join operation that avoids loss of information. Example of natural join:Example of natural join: customer_name loan_numberloan_number amountbranch_name loan borrower Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 L-170 L-230 L-260 Downtown Redwood Perryridge loan number amount customer namebranch name loan borrower loan_number amount L-170 L-230 3000 4000 customer_name Jones Smith branch_name Downtown Redwood Silberschatz, Korth and Sudarshan2.54Database System Concepts - 5th Edition, Oct 5, 2006 Outer JoinOuter Join (cont.)(cont.) Computes the join and then adds tuples form 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. We shall study precise meaning of comparisons with nulls latery p g p Silberschatz, Korth and Sudarshan2.55Database System Concepts - 5th Edition, Oct 5, 2006 Left Outer JoinLeft Outer Join ­­ ExampleExample Left Outer Join loan borrower customer_name loan_number Jones L-1703000 loan_number amount L-170 branch_name Downtown loan borrower Smith Hayes L-230 L-155 4000 1700 L-230 L-260 Redwood Perryridge loan_number amount customer_namebranch_name loan borrower Jones Smith null L-170 L-230 L-260 3000 4000 1700 Downtown Redwood Perryridgey g Silberschatz, Korth and Sudarshan2.56Database System Concepts - 5th Edition, Oct 5, 2006 Right Outer JoinRight Outer Join ­­ ExampleExample Right Outer Join loan borrower customer_name loan_number Jones L-1703000 loan_number amount L-170 branch_name Downtown loan borrower Smith Hayes L-230 L-155 4000 1700 L-230 L-260 Redwood Perryridge loan_number amount customer_namebranch_name loan borrower L-170 L-230 L-155 3000 4000 null Jones Smith Hayes Downtown Redwood null y Silberschatz, Korth and Sudarshan2.57Database System Concepts - 5th Edition, Oct 5, 2006 Full Outer JoinFull Outer Join ­­ ExampleExample Full Outer Join loan borrower customer_name loan_number Jones L-1703000 loan_number amount L-170 branch_name Downtown loan borrower Smith Hayes L-230 L-155 4000 1700 L-230 L-260 Redwood Perryridge loan_number amount customer_namebranch_name loan borrower L-170 L-230 L-260 3000 4000 1700 Jones Smith null Downtown Redwood Perryridge L-155 null Hayes y g null Silberschatz, Korth and Sudarshan2.58Database System Concepts - 5th Edition, Oct 5, 2006 Modification of the DatabaseModification of the Database The content of the database may be modified using the following operations: Deletion Insertion U d i Updating All these operations are expressed using the assignment operator. Silberschatz, Korth and Sudarshan2.59Database System Concepts - 5th Edition, Oct 5, 2006 DeletionDeletion 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 attributesp A deletion is expressed in relational algebra by: r r ­ E where r is a relation and E is a relational algebra query. Silberschatz, Korth and Sudarshan2.60Database System Concepts - 5th Edition, Oct 5, 2006 Deletion ExamplesDeletion Examples Delete all account records in the Perryridge branch. account account ­ b h "P id " (account ) Delete all loan records with amount in the range of 0 to 50 account account ­ branch_name = "Perryridge" (account ) Delete all accounts at branches located in Needham. b h (b h b h it t ) loan loan ­ amount 0and amount 50 (loan) branch (branch_name, branch_city, assets ) account (account_number, branch_name, balance ) depositor (customer_name, account_number ) r1 branch city = "Needham" (account branch )r1 branch_city = Needham" (account branch ) r2 account_number, branch_name, balance (r1) r3 customer name account number (r2 depositor)3 customer_name, account_number ( 2 ) account account ­ r2 depositor depositor ­ r3 Silberschatz, Korth and Sudarshan2.61Database System Concepts - 5th Edition, Oct 5, 2006 InsertionInsertion To insert data into a relation, we either: specify a tuple to be inserted specify a tuple to be inserted 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. Silberschatz, Korth and Sudarshan2.62Database System Concepts - 5th Edition, Oct 5, 2006 Insertion ExamplesInsertion Examples 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 depositor depositor {( Smith , A-973 )} , $ g as the account number for the new savings account. account (account_number, branch_name, balance ) loan (loan_number, branch_name, amount ) d it ( t t b )depositor (customer_name, account_number ) borrower (customer_name, loan_number ) r1 (branch_name = "Perryridge" (borrower loan)) account account loan_number, branch_name, 200 (r1) depositor depositor customer_name, loan_number (r1) Silberschatz, Korth and Sudarshan2.63Database System Concepts - 5th Edition, Oct 5, 2006 UpdatingUpdating A mechanism to change a value in a tuple without charging all values in the tuplep Use the generalized projection operator to do this task )(rr FFF Each Fi is either the i th attribute of r, if the i th attribute is not updated, or, )(,,, 21 nFFF the i attribute of r, if the i attribute is not updated, or, if the attribute is to be updated Fi is an expression, involving only constants and the attributes of r, which gives the new value for the attributeattribute Silberschatz, Korth and Sudarshan2.64Database System Concepts - 5th Edition, Oct 5, 2006 Update ExamplesUpdate Examples account (account_number, branch_name, balance ) Make interest payments by increasing all balances by 5 percent. account account_number, branch_name, balance*1.05 (account) Pay all accounts with balances over $10 000 6 percent interest Pay all accounts with balances over $10,000 6 percent interest and pay all others 5 percent account account_number, branch_name, balance*1.06 ( balance 10000 (account )) account_number, branch_name, balance*1.05 (balance 10000 (account)) Silberschatz, Korth and Sudarshan2.65Database System Concepts - 5th Edition, Oct 5, 2006 ViewsViews In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know a customer's name and loan number, but has no need to see the loan amount. This person should see a relation described, in relational algebra, by, g , y customer_name, loan_number (borrower loan) A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a "virtual relation" is called a view. Silberschatz, Korth and Sudarshan2.66Database System Concepts - 5th Edition, Oct 5, 2006 View DefinitionView Definition A view is defined using the create view statement which has the form create view v as < query expression >create view v as < query expression > where is any legal relational algebra expression. The view name is represented by v.p y Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. Wh i i t d th i i t d i th d t b When a view is created, the query expression is stored in the database; the expression is substituted into queries using the view. So view is not the same as creating a new relation by evaluation the query expression. Silberschatz, Korth and Sudarshan2.67Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries A view consisting of branches and their customers t i ll tcreate view all_customer as branch_name, customer_name (depositor account) (borrower loan) branch_name, customer_name (borrower loan) Find all customers of the Perryridge branch customer_name ( branch_name = `Perryridge' (all_customer)) Silberschatz, Korth and Sudarshan2.68Database System Concepts - 5th Edition, Oct 5, 2006 Views Defined Using Other ViewsViews Defined Using Other Views One view may be used in the expression defining another view A view relation v1 is said to depend directly on a view relation v2 if v2 A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1 A view relation v1 is said to depend on view relation v2 if either v1 depends directl to or there is a path of dependencies from v todepends directly to v2 or there is a path of dependencies from v1 to v2 A view relation v is said to be recursive if it depends on itself. Silberschatz, Korth and Sudarshan2.69Database System Concepts - 5th Edition, Oct 5, 2006 View ExpansionView Expansion A way to define the meaning of views defined in terms of other views. Let view v1 be defined by an expression e1 that may itself contain uses Let view v1 be defined by an expression e1 that may itself contain uses of view relations. View expansion of an expression repeats the following replacement step:step: repeat Find any view relation vi in e1 Replace the view relation vi by the expression defining vi until no more view relations are present in e1 As long as the view definitions are not recursive, this loop willg , p terminate Silberschatz, Korth and Sudarshan2.70Database System Concepts - 5th Edition, Oct 5, 2006 Update of a ViewUpdate of a View Database modifications expressed as views must be translated to modifications of the actual relations in the database. Consider the person who needs to see all loan data in the loan relation except amount. The view given to the person, branch_loan, is defined as: create view loan_branch as loan_number, branch_name (loan) Since we allow a view name to appear wherever a relation name is allowed, the user may write: loan branch loan brach {('L 37` 'Perr ridge`)}loan_branch loan_brach {('L-37`, 'Perryridge`)} Silberschatz, Korth and Sudarshan2.71Database System Concepts - 5th Edition, Oct 5, 2006 Update of a View (cont.)Update of a View (cont.) The previous insertion must be represented by an insertion into the actual relation loan from which the view branch-loan is constructed. An insertion into loan requires a value for amount. The insertion can be dealt with by eitherbe dealt with by either rejecting the insertion and returning an error message to the user; inserting the tupleg p ('L-37', 'Perryridge', null) into the loan relation. Silberschatz, Korth and Sudarshan2.72Database System Concepts - 5th Edition, Oct 5, 2006 Tuple Relational CalculusTuple Relational Calculus A nonprocedural query language, where each query is of the form {t | P(t) }{t | P(t) } It is the set of all tuples t such that predicate P is true for t t is a tuple variable, t [A] denotes the value of tuple t on attribute Ap , [ ] p t r denotes that tuple t is in relation r P is a formula similar to that of the predicate calculus Silberschatz, Korth and Sudarshan2.73Database System Concepts - 5th Edition, Oct 5, 2006 Predicate Calculus FormulaPredicate Calculus Formula 1. Set of attributes and constants 2 Set of comparison operators: (e g )2. Set of comparison operators: (e.g., , , , , , ) 3. Set of connectives: and (), or (v), not () 4. Implication (): x y, if x if true, then y is truep ( ) y, , y x y x v y 5. Set of quantifiers: t r (Q (t )) "there exists" a tuple in t in relation r such that predicate Q (t ) is true t r (Q (t )) Q is true "for all" tuples t in relation r t r (Q (t )) Q is true for all tuples t in relation r Silberschatz, Korth and Sudarshan2.74Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries loan (loan_number, branch_name, amount ) 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 | s loan (t [loan number ] = s [loan number ] s [amount ] 1200)} { | [ ] } {t | s loan (t [loan_number ] = s [loan_number ] s [amount ] 1200)} Notice that a relation on schema (loan_number) is implicitly defined by the query. Relation schema of an expression is determined by either of: If t r is present in the expression the resulting schema is of r If t r is present in the expression, the resulting schema is of r Otherwise the resulting schema is determined by all attributes of t used in the expression. N t If t[A] i d th th tt ib t A i i th l ti h j t Silberschatz, Korth and Sudarshan2.75Database System Concepts - 5th Edition, Oct 5, 2006 Note: If t[A] is used more than once, the attribute A is in the relation schema just once!!! Example QueriesExample Queries depositor (customer_name, account_number ) borrower (customer name loan number ) borrower (customer_name, loan_number ) Find the names of all customers having a loan, an account, or both atg , , the bank {t | s borrower ( t [customer_name ] = s [customer_name ]) u depositor ( t [customer name ] = u [customer name ]) } Find the names of all customers who have a loan and an account t th b k u depositor ( t [customer_name ] = u [customer_name ]) } {t | s borrower ( t [customer_name ] = s [customer_name ]) u depositor ( t [customer name ] = u [customer name] ) } at the bank u depositor ( t [customer_name ] u [customer_name] ) } Silberschatz, Korth and Sudarshan2.76Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries loan (loan_number, branch_name, amount ) depositor (customer name account number ) depositor (customer_name, account_number ) borrower (customer_name, loan_number ) Find the names of all customers having a loan at the Perryridge branch {t | s borrower (t [customer_name ] = s [customer_name ] u loan (u [branch name ] = "Perryridge" Find the names of all customers who have a loan at the u loan (u [branch_name ] = Perryridge u [loan_number ] = s [loan_number ] ) ) } {t | s borrower (t [customer_name ] = s [customer_name ] u loan (u [branch name ] = "Perryridge" Perryridge branch, but no account at any branch of the bank u loan (u [branch_name ] Perryridge u [loan_number ] = s [loan_number ] ) ) v depositor (v [customer_name ] = t [customer name ] ) } Silberschatz, Korth and Sudarshan2.77Database System Concepts - 5th Edition, Oct 5, 2006 [ _ ] ) } Example QueriesExample Queries branch (branch_name, branch_city, assets ) customer (customer name customer street customer city ) 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 ) Find the names of all customers having a loan at the Perryridge branch, and the cities in which they liveand the cities in which they live {t | s loan (s [branch_name ] = "Perryridge" u borrower (u [loan_number ] = s [loan_number ] t [customer name ] = u [customer name ] ) t [customer_name ] = u [customer_name ] ) v customer (u [customer_name ] = v [customer_name ] t [customer_city ] = v [customer_city ] ) ) } Silberschatz, Korth and Sudarshan2.78Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries branch (branch_name, branch_city, assets ) customer (customer name, customer street, customer city )( _ , _ , _ y ) account (account_number, branch_name, balance ) loan (loan_number, branch_name, amount ) depositor (customer_name, account_number )p ( _ _ ) borrower (customer_name, loan_number ) Find the names of all customers who have an account at all branches Find the names of all customers who have an account at all branches located in Brooklyn: {t | r customer (t [customer_name ] = r [customer_name ]) ( u branch (u [branch_city ] = "Brooklyn" s depositor (r [customer_name ] = s [customer_name ] w account ( w[account_number ] = s [account_number ] ( w [branch_name ] = u [branch_name ] ) ) ) ) ) } Silberschatz, Korth and Sudarshan2.79Database System Concepts - 5th Edition, Oct 5, 2006 ) ) } Safety of ExpressionsSafety of Expressions It is possible to write tuple calculus expressions that generate infinite relations. For example, { t | t r } results in an infinite relation if the domain of any attribute of relation r is infinite T d i t th bl t i t th t f ll bl To guard against the problem, we restrict the set of allowable expressions to safe expressions. An expression {t | P(t) } in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P NOTE: this is more than just a syntax condition.O t s s o e t a just a sy ta co d t o E.g. { t | t [A] = 5 true } is not safe ­ it defines an infinite set with attribute values that do not appear in any relation or tuples or constants in Por constants in P. Silberschatz, Korth and Sudarshan2.80Database System Concepts - 5th Edition, Oct 5, 2006 Domain Relational CalculusDomain Relational Calculus A nonprocedural query language equivalent in power to the tuple relational calculus Each query is an expression of the form: { x1, x2, ..., xn | P (x1, x2, ..., xn) } x1 x2 x represent domain variables x1, x2, ..., xn represent domain variables P represents a formula similar to that of the predicate calculus Silberschatz, Korth and Sudarshan2.81Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries loan (loan_number, branch_name, amount ) depositor (customer name, account number )p ( _ , _ ) borrower (customer_name, loan_number ) Fi d th l b b h d t f l f $1200 Find the loan_number, branch_name, and amount for loans of over $1200 { l, b, a | l, b, a loan a > 1200} Find the names of all customers who have a loan of over $1200 Find the names of all customers who have a loan of over $1200 { c | l, b, a ( c, l borrower l, b, a loan a > 1200)} Find the names of all customers who have a loan at the Perryridge branch and the loan amount: { c, a | l ( c, l borrower b ( l, b, a loan b = "Perryridge"))}y g ))} { c, a | l ( c, l borrower l, "Perryridge", a loan)} Silberschatz, Korth and Sudarshan2.82Database System Concepts - 5th Edition, Oct 5, 2006 Example QueriesExample Queries branch (branch_name, branch_city, assets ) customer (customer name, customer street, customer city )( _ , _ , _ y ) account (account_number, branch_name, balance ) loan (loan_number, branch_name, amount ) depositor (customer_name, account_number )p ( _ _ ) borrower (customer_name, loan_number ) Find the names of all customers having a loan, an account, or both at the Perryridge branch:the Perryridge branch: { c | l ( c, l borrower b,a ( l, b, a loan b = "Perryridge")) a ( c a depositor a ( c, a depositor b,n ( a, b, n account b = "Perryridge"))} Find the names of all customers who have an account at all branches l t d i B kllocated in Brooklyn: { c | s,t ( c, s, t customer) x,y,z ( x, y, z branch y = "Brooklyn") Silberschatz, Korth and Sudarshan2.83Database System Concepts - 5th Edition, Oct 5, 2006 a,b ( a, x, b account c,a depositor)} Safety of ExpressionsSafety of Expressions The expression: { x x x | P (x x x )}{ x1, x2, ..., xn | P (x1, x2, ..., xn )} is safe if all of the following hold: 1. All values that appear in tuples of the expression are values from dom (P ) (that is, the values appear either in P or in a tuple of a relation mentioned in P ). 2. For every "there exists" subformula of the form x (P1(x)), the subformula is true if and only if there is a value of x in dom (P1) such that P1(x) is true.suc t at 1( ) s t ue 3. For every "for all" subformula of the form x (P1(x)), the subformula is true if and only if P1(x) is true for all values x from dom (P1). Silberschatz, Korth and Sudarshan2.84Database System Concepts - 5th Edition, Oct 5, 2006