Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational Model
©Silberschatz, Korth and Sudarshan2.2Database System Concepts - 5 th Edition, Oct 5, 2006 Examples Consider the following relations: Student(roll, name, address, phone) Course(code, title) Registered(roll, code) Find out the course code in which at least one student is registered π code ( Registered) Find out the titles of registered courses π title ( Course.code = Registered.code (Course X Registered )) Find out the course code in which no student is registered π code ( Course ) - π code ( Registered )
©Silberschatz, Korth and Sudarshan2.3Database System Concepts - 5 th Edition, Oct 5, 2006 Examples Find out the student names and course titles they registered to π name, title ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X Course)) Consider the following relations: Student(roll, name, address, phone) Course(code, title) Registered(roll, code) Name of student who are registered to ‘Database’ or ‘Algorithms’ course π name ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X (σ title=’Database’ Course)) ) ∪ π name ( Student.roll = Registered.roll^ Registered.code=Course.code (Student X Registered X (σ title=’Algorithm’ Course)) )
©Silberschatz, Korth and Sudarshan2.4Database System Concepts - 5 th Edition, Oct 5, 2006 Rename Operation Allows us to name and therefore to refer to the results of relational- algebra expressions. Allows us to refer to a relation by more than one name. Example: x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2, …., A n.
©Silberschatz, Korth and Sudarshan2.5Database System Concepts - 5 th Edition, Oct 5, 2006 Banking Example branch (branch_name, branch_city, assets) 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)
©Silberschatz, Korth and Sudarshan2.6Database System Concepts - 5 th Edition, Oct 5, 2006 Example Queries 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)))
©Silberschatz, Korth and Sudarshan2.7Database System Concepts - 5 th Edition, Oct 5, 2006 Example Queries Find the names of all customers who have a loan at the Dhanmondi branch. Query 2 customer_name ( loan.loan_number = borrower.loan_number ( ( branch_name = “Dhanmondi ” (loan)) x borrower)) Query 1 customer_name ( branch_name = “Dhanmondi” ( borrower.loan_number = loan.loan_number (borrower x loan)))
©Silberschatz, Korth and Sudarshan2.8Database System Concepts - 5 th Edition, Oct 5, 2006 Formal Definition A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation Let E 1 and E 2 be relational-algebra expressions; the following are all relational-algebra expressions: E 1 E 2 E 1 – E 2 E 1 x E 2 p (E 1 ), P is a predicate on attributes in E 1 s (E 1 ), S is a list consisting of some of the attributes in E 1 x (E 1 ), x is the new name for the result of E 1
©Silberschatz, Korth and Sudarshan2.9Database System Concepts - 5 th Edition, Oct 5, 2006 Additional Operations We define additional operations that do not add any power to the relational algebra, but that simplify common queries. Set intersection Natural join Division Assignment
©Silberschatz, Korth and Sudarshan2.10Database System Concepts - 5 th Edition, Oct 5, 2006 Set-Intersection Operation Notation: r s Defined as: r s = { t | t r and t s } Assume: r, s have the same arity attributes of r and s are compatible Note: r s = r – (r – s)
©Silberschatz, Korth and Sudarshan2.11Database System Concepts - 5 th Edition, Oct 5, 2006 Set-Intersection Operation – Example Relation r, s: r s A B 2323 rs 2
©Silberschatz, Korth and Sudarshan2.12Database System Concepts - 5 th Edition, Oct 5, 2006 Example Query Find the names of all customers who have a loan and an account at the bank. customer_name (borrower) customer_name (depositor)
©Silberschatz, Korth and Sudarshan2.13Database System Concepts - 5 th Edition, Oct 5, 2006 Notation: r s Natural-Join Operation 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 t r from r and t s from s. If t r and t s 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 r on r t has the same value as t s on s Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) r s is defined as: r.A, r.B, r.C, r.D, s.E ( r.B = s.B r.D = s.D (r x s))
©Silberschatz, Korth and Sudarshan2.14Database System Concepts - 5 th Edition, Oct 5, 2006 Natural Join Operation – Example Relations r, s: AB CD aababaabab B D aaabbaaabb E r AB CD aaaabaaaab E s r s
©Silberschatz, Korth and Sudarshan2.15Database System Concepts - 5 th Edition, Oct 5, 2006 Example Query Find the name and loan amount of all customers who have a loan at the bank. customer_name, amount (borrower loan) customer. customer_name ( customer_city = “Jessore ” (customer depositor account )) Find the names of all customers who have an account in the bank and who live in Jessore Natural Join is associative
©Silberschatz, Korth and Sudarshan2.16Database System Concepts - 5 th Edition, Oct 5, 2006 Example Query Find the names of all customers who have a loan and an account at the bank. customer_name (borrower) customer_name (depositor) customer_name (borrower depositor) When R S =R X S ? If R S= φ
©Silberschatz, Korth and Sudarshan2.17Database System Concepts - 5 th Edition, Oct 5, 2006 Summary
©Silberschatz, Korth and Sudarshan2.18Database System Concepts - 5 th Edition, Oct 5, 2006 Division Operation Notation: 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 ) } Where tu means the concatenation of tuples t and u to produce a single tuple r s
©Silberschatz, Korth and Sudarshan2.19Database System Concepts - 5 th Edition, Oct 5, 2006 Division Operation – Example Relations r, s: r s: B A 1212 AB r s
©Silberschatz, Korth and Sudarshan2.20Database System Concepts - 5 th Edition, Oct 5, 2006 Another Division Example AB aaaaaaaaaaaaaaaa CD aabababbaabababb E Relations r, s: r s: D abab E 1111 AB aaaa C r s
©Silberschatz, Korth and Sudarshan2.21Database System Concepts - 5 th Edition, Oct 5, 2006 Find all customers who have an account at all branches located in Brooklyn city. Bank Example Queries customer_name, branch_name (depositor account) branch_name ( branch_city = “Brooklyn” (branch))
©Silberschatz, Korth and Sudarshan2.22Database System Concepts - 5 th Edition, Oct 5, 2006 Assignment Operation The assignment operation ( ) provides a convenient way to express complex queries. Write query as a sequential program consisting of a series of assignments followed by an expression whose value is displayed as a result of the query. Assignment must always be made to a temporary relation variable. Example: The result to the right of the is assigned to the relation variable on the left of the . May use variable in subsequent expressions. r 1 branch_city = “dhaka” (account branch ) r 2 account_number (r 1 )