CS 405G: Introduction to Database Systems

Slides:



Advertisements
Similar presentations
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Advertisements

1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
©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 Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
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.
Instructor: Mohamed Eltabakh
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
©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, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
CS 370 Database Systems Lecture 11 Relational Algebra.
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,
Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 2: Relational Model. 2.2Unite International CollegeDatabase Management Systems Chapter 2: Relational Model Structure of Relational Databases Fundamental.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
©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.
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.
CS 405G: Introduction to Database Systems Relational Algebra.
ASET Relational Algebra continues…. ASET Rename Operation Allows us to name, and therefore to refer to, the results of relational-algebra expressions.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
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.
Aljabar Relasi.
Relational Algebra Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Midlands State University Topic: Relational DB Model
CS 480: Database Systems Lecture 17 February 22, 2013.
Temple University – CIS Dept. CIS661– Principles of Database Systems
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Algebra - Part 1
Chapter 3: Relational Model III
SQL Structured Query Language 11/9/2018 Introduction to Databases.
Chapter 2: Relational Model
CS 405G: Introduction to Database Systems
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Introduction to Database
Session 3 Welcome: To session 3-the sixth learning sequence
Instructor: Mohamed Eltabakh
Relational Algebra.
CS 405G: Introduction to Database Systems
Chapter 2: Relational Model
Presentation transcript:

CS 405G: Introduction to Database Systems Relational Algebra Part III

Jinze Liu @ University of Kentucky Topics Outer join Division Aggregate Operation Modification Insert Delete Update 12/8/2019 Jinze Liu @ University of Kentucky

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. 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

Null Values Comparisons with null values return the special truth value unknown If false was used instead of unknown, then not (A < 5) would not be equivalent to A >= 5 Three-valued logic using the truth value 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 NOT: (not unknown) = unknown Result of select predicate is treated as false if it evaluates to unknown

Jinze Liu @ University of Kentucky Additional Operators Outer join Division 12/8/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky (Left) Outer Join Input: two tables R and S Notation: R P S Purpose: pairs rows from two tables Output: for each row r in R and each row s in S, if p satisfies, output a row rs (concatenation of r and s) Otherwise, output a row r with NULLs Right outer join and full outer join are defined similarly 12/8/2019 Jinze Liu @ University of Kentucky

Left Outer Join Example Employee Eid = Mid Department Did Mid Dname 4 1234 Research 5 1123 Finance Eid Name 1234 John Smith 1123 Mary Carter 1011 Bob Lee Eid = Mid Eid Name Did Mid Dname 1234 John Smith 4 Research 1123 Mary Carter 5 Finance 1011 Bob Lee NULL 12/8/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Division Operator Input: two tables R and S Notation: R  S Purpose: Find the subset of items in one set R that are related to all items in another set 12/8/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Division Operator Find professors who have taught courses in all departments Why does this involve division? ProfId DeptId DeptId Contains row <p,d> if professor p has taught a course in department d All department Ids 12/8/2019 Jinze Liu @ University of Kentucky

Aggregate 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 algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) 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

Aggregate Operation – Example B C Relation r:     7 3 10 sum-C g sum(c) (r) 27

Aggregate Operation – Example Relation account grouped by branch-name: branch-name account-number balance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-name g sum(balance) (account) branch-name balance Perryridge Brighton Redwood 1300 1500 700

Modification of the Database The content of the database may be modified using the following operations: Deletion Insertion Updating All these operations are expressed using the assignment operator.

Deletion 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.

Deletion Examples 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. r1  branch_city = “Needham” (account branch ) r2  branch_name, account_number, balance (r1) r3   customer_name, account_number (r2 depositor) account  account – r2 depositor  depositor – r3

Insertion To insert data into a relation, we either: 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.

Insertion Examples Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch. account  account  {(“Perryridge”, A-973, 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. r1  (branch_name = “Perryridge” (borrower loan)) account  account  branch_name, loan_number,200 (r1) depositor  depositor  customer_name, loan_number (r1)

Updating A mechanism to change a value in a tuple without changing all values in the tuple Use the generalized projection operator to do this task Each Fi is either the i th attribute of r, if the ith 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 attribute

Update Examples 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 and pay all others 5 percent account   account_number, branch_name, balance * 1.06 ( BAL  10000 (account ))   account_number, branch_name, balance * 1.05 (BAL  10000 (account))

Jinze Liu @ University of Kentucky Exercises of R. A. Reserves Sailors Boats Jinze Liu @ University of Kentucky

Problem 1 Find names of sailors who’ve reserved boat #103 Solution: Sailors  πsname Who reserved boat #103? Reserves σbid = “103” Boat #103 Jinze Liu @ University of Kentucky

Problem 2: Find names of sailors who’ve reserved a red boat Information about boat color only available in Boats; so need an extra join: Names of sailors who reserved red boat Sailors  πsname Reserve  πSID Who reserved red boats? Boat σcolor = “red” Red boats Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Problem 3: Find names of sailors who’ve reserved a red boat or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Names of sailors who reserved red boat Sailors  πsname Reserve  πSID Who reserved red boats? Boat σcolor = “red”  color = “green” Red boats Jinze Liu @ University of Kentucky

Problem 4: Find names of sailors who’ve reserved only one boat Jinze Liu @ University of Kentucky

Mathematical model consisting of: What is “algebra” Mathematical model consisting of: Operands --- Variables or values; Operators --- Symbols denoting procedures that construct new values from a given values Relational Algebra is algebra whose operands are relations and operators are designed to do the most commons things that we need to do with relations

Why is r.a. a good query language? Simple A small set of core operators who semantics are easy to grasp Declarative? Yes, compared with older languages like CODASYL Though operators do look somewhat “procedural” Complete? With respect to what? 12/8/2019 Jinze Liu @ University of Kentucky

Jinze Liu @ University of Kentucky Review Expression tree Tips in writing R.A. Use temporary variables Use foreign keys to join tables A comparison is to identify a relationship Use set minus in non-monotonic results 12/8/2019 Jinze Liu @ University of Kentucky