Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.

Slides:



Advertisements
Similar presentations
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Relational Algebra Lecture 2. Relational Model Basic Notions Fundamental Relational Algebra Operations Additional Relational Algebra Operations Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Relational Algebra Lecture 2. Relational Model Basic Notions Fundamental Relational Algebra Operations Additional Relational Algebra Operations Extended.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Instructor: Mohamed Eltabakh
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Relational Model Database Management Systems I Alex Coman, Winter 2006.
Relational Model. 2 Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com 1 Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 6: Formal Relational Query Languages. 6.2 Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Mathematics and Computer Science San Jose State University.
1 Session 3 Welcome: To session 3-the fifth learning sequence “ Relational algebra “ Recap : In the previous learning sequences, we discussed the eight.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Yufis Azhar – Teknik Informatika – UMM.  Aggregation function takes a collection of values (of a single attribute) and returns a single value as a result.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
ICOM 5016 – Introduction to Database Systems Lecture 8 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Example.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Relational Model Structure of Relational Databases Relational Algebra.
Chapter 2: Relational Model. 2.2Unite International CollegeDatabase Management Systems Chapter 2: Relational Model Structure of Relational Databases Fundamental.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 2 RELATIONAL MODEL 2.1.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Database System Concepts, 5 th Ed. Bin Mu at Tongji University Chapter 2: Relational Model.
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
©Silberschatz, Korth and Sudarshan1Database System Concepts The Relational Model Structure of Relational Databases Relational Algebra.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Aljabar Relasi.
Midlands State University Topic: Relational DB Model
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Session 3 Welcome: To session 3-the fifth learning sequence
Chapter 3: Relational Model III
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Relational Algebra.
Chapter 2: Relational Model
CS 405G: Introduction to Database Systems
Presentation transcript:

Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views

Additional Operations We define additional operations that do not add any power to the relational algebra, but that simplify common queries. Set intersection Set intersection Natural join Natural join Division Division Assignment Assignment

Set-Intersection Operation Notation: r  s Notation: r  s Defined as: Defined as: r  s ={ t | t  r and t  s } r  s ={ t | t  r and t  s } Assume: Assume: r, s have the same arity r, s have the same arity attributes of r and s are compatible attributes of r and s are compatible Note: r  s = r - (r - s) Note: r  s = r - (r - s)

Set-Intersection Operation - Example Relation r, s: Relation r, s: r  s r  s A B   2323 r s  2

Natural-Join Operation n n Notation: r s Example: Example: R = (A, B, C, D) S = (E, B, D) Result schema = (A, B, C, D, E) 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)) 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))

Natural Join Operation – Example Relations r, s: Relations r, s: AB  CD  aababaabab B D aaabbaaabb E  r AB  CD  aaaabaaaab E  s r s

Example List account number and the branch city that the account belongs to List account number and the branch city that the account belongs to account-number, branch-city (account branch)  account-number, branch-city (account branch)

Division Operation Suited to queries that include the phrase “for all”. Suited to queries that include the phrase “for all”. Let r and s be relations on schemas R and S respectively where Let r and s be relations on schemas R and S respectively where R = (A 1, …, A m, B 1, …, B n ) R = (A 1, …, A m, B 1, …, B n ) S = (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 ) }

Division Operation – Example Relations r, s: r  s:r  s: A B  1212 AB  r s

Another Division Example AB  aaaaaaaaaaaaaaaa CD  aabababbaabababb E Relations r, s: r  s:r  s: D abab E 1111 AB  aaaa C  r s

Example Queries Find all customers who have an account from at least the “Downtown” and the Uptown” branches. 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)) Query 2  customer-name, branch-name (depositor account)   temp(branch-name ) ({(“Downtown”), (“Uptown”)})

Find all customers who have an account at all branches located in Brooklyn city. Find all customers who have an account at all branches located in Brooklyn city. Example Queries  customer-name, branch-name (depositor account)   branch-name (  branch-city = “Brooklyn” (branch))

Assignment Operation The assignment operation (  ) provides a convenient way to express complex queries. The assignment operation (  ) provides a convenient way to express complex queries. Write query as a sequential program consisting of Write query as a sequential program consisting of a series of assignments a series of assignments followed by an expression whose value is displayed as a result of the query. followed by an expression whose value is displayed as a result of the query. Example: Example: Temp1  r x s Temp1  r x s (Temp1 )  A,B (Temp1 )

Extended Relational-Algebra- Operations Generalized Projection Generalized Projection Outer Join Outer Join Aggregate Functions Aggregate Functions

Generalized Projection Extends the projection operation by allowing arithmetic functions to be used in the projection list.  F1, F2, …, Fn (E) Extends the projection operation by allowing arithmetic functions to be used in the projection list.  F1, F2, …, Fn (E) E is any relational-algebra expression E is any relational-algebra expression Each of F 1, F 2, …, F n are are arithmetic expressions involving constants and attributes in the schema of E. Each of F 1, F 2, …, F n are are arithmetic expressions involving constants and attributes in the schema of E.

Example Given relation credit-info(customer-name, limit, credit-balance), find how much more each person can spend: Given relation credit-info(customer-name, limit, credit-balance), find how much more each person can spend:  customer-name, limit – credit-balance (credit-info)

Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. 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 Aggregate operation in relational algebra Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1 ), F2( A2 ),…, Fn( An ) (E) E is any relational-algebra expression E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each F i is an aggregate function Each A i is an attribute name Each A i is an attribute name

Aggregate Operation – Example Relation r: Relation r: AB   C g sum(c) (r) sum-C 27

Aggregate Operation – Example Relation account grouped by branch-name: Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-nameaccount-numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A branch-namebalance Perryridge Brighton Redwood

Aggregate Functions (Cont.) Result of aggregation does not have a name Result of aggregation does not have a name Can use rename operation to give it a name Can use rename operation to give it a name For convenience, we permit renaming as part of aggregate operation For convenience, we permit renaming as part of aggregate operation branch-name g sum(balance) as sum-balance (account)

Outer Join An extension of the join operation that avoids loss of information. An extension of the join operation that avoids loss of information. Computes the join and then adds tuples form one relation that do not match tuples in the other relation to the result of the join. Computes the join and then adds tuples form one relation that do not match tuples in the other relation to the result of the join. Uses null values: Uses null values: null signifies that the value is unknown or does not exist null signifies that the value is unknown or does not exist All comparisons involving null are (roughly speaking) false by definition. All comparisons involving null are (roughly speaking) false by definition. Will study precise meaning of comparisons with nulls later Will study precise meaning of comparisons with nulls later

Outer Join – Example Relation loan Relation loan Relation borrower customer-nameloan-number Jones Smith Hayes L-170 L-230 L loan-numberamount L-170 L-230 L-260 branch-name Downtown Redwood Perryridge

Outer Join – Example Inner Join Inner Join loan Borrower loan Borrower loan-numberamount L-170 L customer-name Jones Smith branch-name Downtown Redwood Jones Smith null loan-numberamount L-170 L-230 L customer-namebranch-name Downtown Redwood Perryridge Left Outer Join loan Borrower

Outer Join – Example Right Outer Join Right Outer Join loan borrower loan borrower loan borrower Full Outer Join loan-numberamount L-170 L-230 L null customer-name Jones Smith Hayes branch-name Downtown Redwood null loan-numberamount L-170 L-230 L-260 L null customer-name Jones Smith null Hayes branch-name Downtown Redwood Perryridge null

Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes 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. null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null. The result of any arithmetic expression involving null is null. Aggregate functions simply ignore null values Aggregate functions simply ignore null values For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same

Null Values Comparisons with null values return the special truth value unknown Comparisons with null values return the special truth value unknown If false was used instead of unknown, then If false was used instead of unknown, then not (A = 5 not (A = 5

Three-valued logic Three-valued logic using the truth value unknown: Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown NOT: (not unknown) = unknown In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown Result of select predicate is treated as false if it evaluates to unknown Result of select predicate is treated as false if it evaluates to unknown

Views In some cases, it is not desirable for all users to see the entire logical model (i.e., all the actual relations stored in the database.) In some cases, it is not desirable for all users to see the entire logical model (i.e., all the actual relations stored in the database.) Consider a person who needs to know a customer’s loan number and branch-name but has no need to see the loan amount. This person should see a relation described, in the relational algebra, by Consider a person who needs to know a customer’s loan number and branch-name but has no need to see the loan amount. This person should see a relation described, in the relational algebra, by  customer-name, loan-number, branch_name (borrower loan) Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.

View Definition A view is defined using the create view statement which has the form A view is defined using the create view statement which has the form create view v as <query expression where is any legal relational algebra query expression. The view name is represented by v. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates.

View Examples Consider the view (named all-customer) consisting of branches and their customers. Consider the view (named all-customer) consisting of branches and their customers. We can find all customers of the Perryridge branch by writing: create view all-customer as  branch-name, customer-name (depositor account)   branch-name, customer-name (borrower loan)  customer-name (  branch-name = “Perryridge” (all-customer))