Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

Similar documents
Chapter 6: Formal Relational Query Languages

Relational Algebra. Procedural language Six basic operators

Chapter 2: Relational Model

Chapter 3: Relational Model

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

QQ Group

Database System Concepts

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Chapter 2: Relational Model

Other Relational Languages

Chapter 5: Other Relational Languages

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL

Chapter 2: Intro to Relational Model

Chapter 6 Formal Relational Query Languages

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

The SQL data-definition language (DDL) allows defining :

Chapter 3: SQL. Chapter 3: SQL

Upon completion of this Unit, students will be introduced to the following

Chapter 3: Introduction to SQL

Silberschatz, Korth and Sudarshan See for conditions on re-use

Lecture 6 - More SQL

CS3DB3/SE4DB3/SE6M03 TUTORIAL

Chapter 4: SQL. Basic Structure

UNIT 2 RELATIONAL MODEL

CS 582 Database Management Systems II

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

ERRATA for Database System Concepts, 6 th Edition, 2010 Silberschatz, Korth, and Sudarshan Last updated: December 6, 2010

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Database Systems SQL SL03

Database Systems SQL SL03

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

Chapter 8: Relational Algebra

Relational Model, Relational Algebra, and SQL

DATABASE DESIGN I - 1DL300

UNIT- II (Relational Data Model & Introduction to SQL)

Explain in words what this relational algebra expression returns:

DATABASE TECHNOLOGY. Spring An introduction to database systems

Simple SQL Queries (2)

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

CS425 Fall 2017 Boris Glavic Chapter 4: Introduction to SQL

The SQL database language Parts of the SQL language

Other Query Languages II. Winter Lecture 12

Chapter 2: Intro to Relational Model

Relational Model. Prepared by: Ms. Pankti Dharwa ( Asst. Prof, SVBIT)

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

Chapter 14: Query Optimization

Database Technology Introduction. Heiko Paulheim

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

DATABASE TECHNOLOGY - 1MB025

DATABASE TECHNOLOGY - 1MB025

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

DATABASTEKNIK - 1DL116

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Chapter 13: Query Optimization. Chapter 13: Query Optimization

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

ARTICLE RELATIONAL ALGEBRA

CSCC43H: Introduction to Databases. Lecture 3

CSIE30600/CSIEB0290 Database Systems Relational Algebra and Calculus 2

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Outline. CSIE30600 Database Systems Relational Algebra and Calculus 2

Relational model and basic SQL

Other Relational Query Languages

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 13: Query Optimization

Relational Algebra 1. Week 4

Informationslogistik Unit 4: The Relational Algebra

Query Processing. Debapriyo Majumdar Indian Sta4s4cal Ins4tute Kolkata DBMS PGDBA 2016

3. Relational Data Model 3.5 The Tuple Relational Calculus

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

Chapter 2 Introduction to Relational Models

Database Systems. Answers

CMP-3440 Database Systems

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Query processing and optimization

Query Processing & Optimization

CSCB20. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

DATABASE DESIGN I - 1DL300

Database System Concepts

Unit 2: SQL AND PL/SQL

Database Systems CSE Comprehensive Exam Spring 2005

CS3DB3/SE4DB3/SE6M03 TUTORIAL

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

Chapter 12: Query Processing

Relational Algebra and Calculus

Lecture 1: Conjunctive Queries

Darshan Institute of Engineering & Technology Relational Model

RELATIONAL DATA MODEL: Relational Algebra

CS 377 Database Systems

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Chapter 6 The Relational Algebra and Calculus

Other Query Languages. Winter Lecture 11

Transcription:

Chapter 3: Formal Relational Query Languages CS425 Fall 2013 Boris Glavic Chapter 3: Formal Relational Query Languages Relational Algebra Tuple Relational Calculus Domain Relational Calculus Textbook: Chapter 6 Modified from: Database System Concepts, 6 th Ed. Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use 3.2 Relational Algebra Select Operation Example Procedural language Six basic operators Relation r select: σ project: union: set difference: Cartesian product: x rename: ρ The operators take one or two relations as inputs and produce a new relation as a result. composable σ A=B ^ D > 5 (r) 3.3 3.4 Select Operation Project Operation Example Notation: σ p (r) p is called the selection predicate Defined as: p(r) ={t t 2 r ^ p(t)} Where p is a formula in propositional calculus consisting of terms connected by : (and), (or), (not) Each term is one of: <attribute> op <attribute> or <constant> where op is one of: =,, >,. <. Example of selection: σ dept_name= Physics (instructor) Theory Warning Relation r: A,C (r) 3.5 3.6 1

Project Operation Union Operation Example Notation: A 1, A 2,, A k ( r) where A 1, A 2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets Let A be a subset of the attributes of relation r then: A (r) ={t.a t 2 r} Relations r, s: r s: Example: To eliminate the dept_name attribute of instructor ID, name, salary (instructor) Theory Warning 3.7 3.8 Union Operation Set difference of two relations Notation: r s Defined as: r [ s = {t t 2 r _ t 2 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: 2 nd column of r deals with the same type of values as does the 2 nd column of s) Relations r, s: r s: Example: to find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in both course_id (σ semester= Fall Λ year=2009 (section)) course_id (σ semester= Spring Λ year=2010 (section)) 3.9 3.10 Set Difference Operation Cartesian-Product Operation Example Notation r s Defined as: r s = {t t 2 r ^ t 62 s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible Example: to find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester course_id (σ semester= Fall Λ year=2009 (section)) course_id (σ semester= Spring Λ year=2010 (section)) Relations r, s: r x s: 3.11 3.12 2

Notation r x s Defined as: Cartesian-Product Operation r s = {t, t 0 t 2 r ^ t 0 2 s} 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. Composition of Operations Can build expressions using multiple operations Example: σ A=C (r x s) r x s σ A=C (r x s) 3.13 3.14 Rename Operation Example Query 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: ρ x (r) returns the expression E under the name X If a relational-algebra expression E has arity n, then ρ (r) x(a1,a 2,...,A n ) returns the result of expression E under the name X, and with the attributes renamed to A 1, A 2,., A n. X (r) ={t(x) t 2 r} X(A) (r) ={t(x).a t 2 r} Find the largest salary in the university Step 1: find instructor salaries that are less than some other instructor salary (i.e. not maximum) using a copy of instructor under a new name d instructor.salary ( instructor.salary<d.salary (instructor d (instructor))) Step 2: Find the largest salary salary (instructor) instructor.salary ( instructor.salary<d.salary (instructor d (instructor))) 3.15 3.16 Example Queries Find the names of all instructors in the Physics department, along with the course_id of all courses they have taught Query 1 instructor.id,course id ( dept name= 0 Physics 0( instructor.id=teaches.id(instructor teaches))) Query 2 instructor.id,course id ( instructor.id=teaches.id ( dept name= 0 Physics0(instructor teaches))) Formal Definition (Syntax) A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation: e.g., {(1),(2)} 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 Theory Warning 3.17 3.18 3

Formal Definition (Semantics) Formal Definition (Semantics) Let E be an relational algebra expression. We use [E](I) to denote the evaluation of E over a database instance I For simplicity we will often drop I and [] The result of evaluating a simple relational algebra expression E over a database is defined as Simple relation: [R](I) = R(I) Constant relation: [C](I) = C Theory Warning Let E 1 and E 2 be relational-algebra expressions. [E 1 [ E 2 ]={t t 2 [E 1 ] _ t 2 [E 2 ]} [E 1 E 2 ]={t t 2 [E 1 ] ^ t 62 [E 2 ]} [E 1 E 2 ]={t, t 0 t 2 [E 1 ] ^ t 0 2 [E 2 ]} [ p(e 1 )] = {t t 2 [E 1 ] ^ p(t)} [ A (E 1 )] = {t.a t 2 [E 1 ]} [ X (E 1 )] = {t(x) t 2 [E 1 ]} Theory Warning 3.19 3.20 Null Values 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 (as in SQL) For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same (as in SQL) 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 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 3.21 3.22 Additional Operations We define additional operations that do not add any expressive power to the relational algebra, but that simplify common queries. Set intersection Natural join Assignment Outer join Notation: r s Defined as: Assume: Set-Intersection Operation r \ s = {t t 2 r ^ t 2 s} r, s have the same arity attributes of r and s are compatible Note: r s = r (r s) That is adding intersection to the language does not make it more expressive 3.23 3.24 4

Set-Intersection Operation Example Natural-Join Operation Relation r, s: r s Notation: r s 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 Example: t has the same value as t r on r t has the same value as t s on s 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)) 3.25 3.26 Natural-Join Operation (cont.) Natural Join Example Let r and s be relations on schemas R and S respectively. Then, r s is defined as: X = R \ S S 0 = S R r./ s = R,S 0( r.x=s.x (r s)) Relations r, s: r s 3.27 3.28 Natural Join and Theta Join Assignment Operation Find the names of all instructors in the Comp. Sci. department together with the course titles of all the courses that the instructors teach name, title (σ dept_name= Comp. Sci. (instructor course)) Natural join is associative (instructor teaches) course is equivalent to instructor (teaches course) Natural join is commutative (we ignore attribute order) instruct teaches is equivalent to teaches instructor The theta join operation r θ s is defined as r./ s = (r s) teaches 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. E 1 E 2 E 3 E 1 [ E 2 salary>40000(instructor) salary<10000(instructor) 3.29 3.30 5

Outer Join Outer Join Example An extension of the join operation that avoids loss of information. 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 later Relation instructor1 ID 10101 12121 15151 Relation teaches1 name Srinivasan Wu Mozart dept_name Comp. Sci. Finance Music ID 10101 12121 76766 course_id CS-101 FIN-201 BIO-101 3.31 3.32 Outer Join Example Outer Join Example Join instructor teaches Right Outer Join instructor teaches ID 10101 12121 name Srinivasan Wu dept_name Comp. Sci. Finance course_id CS-101 FIN-201 ID 10101 12121 76766 name Srinivasan Wu null dept_name Comp. Sci. Finance null course_id CS-101 FIN-201 BIO-101 Left Outer Join instructor teaches Full Outer Join instructor teaches ID 10101 12121 15151 name Srinivasan Wu Mozart dept_name Comp. Sci. Finance Music course_id CS-101 FIN-201 null ID 10101 12121 15151 76766 name Srinivasan Wu Mozart null dept_name Comp. Sci. Finance Music null course_id CS-101 FIN-201 null BIO-101 3.33 3.34 Outer Join using Joins Division Operator Outer join can be expressed using basic operations r >< s =(r./ s) [ ((r R (r./ s)) {(null,..., null)}) r >< s =(r./ s) [ ({(null,..., null)} (s S (r./ s))) r >< s =(r./ s) [ ((r R (r./ s)) {(null,..., null)}) [ ({(null,..., null)} (s S (r./ s))) Given relations r(r) and s(s), such that S R, r s is the largest relation t(r-s) such that t x s r Alternatively, all tuples from r.(r-s) such that all their extensions on R S with tuples from s exist in R E.g. let r(id, course_id) = ID, course_id (takes ) and s(course_id) = course_id (σ dept_name= Biology (course ) then r s gives us students who have taken all courses in the Biology department Can write r s as E 1 R S (r) E 2 R S ((E 1 s) R S,S (r./ s)) r s = E 1 E 2 3.35 3.36 6

Division Operator Example Extended Relational-Algebra-Operations Return the name of all persons that read all newspapers reads name newspaper Peter Bob Alice Alice Times Wall Street Times Wall Street reads newspaper = E 1 E 2 [reads newspaper] ={(Alice)} newspaper newspaper Times Wall Street E 1 name(reads) E 2 ((E 1 newspaper) name,newspaper(reads./ newspaper)) Generalized Projection Aggregate Functions 3.37 3.38 Generalized Projection Extends the projection operation by allowing arithmetic functions to be used in the projection list. E is any relational-algebra expression Each of F 1, F 2,, F n are arithmetic expressions and function calls involving constants and attributes in the schema of E. Given relation instructor(id, name, dept_name, salary) where salary is annual salary, get the same information but with monthly salary ID, name, dept_name, salary/12 (instructor) Adding functions increases expressive power F1,...,F n (E) In standard relational algebra there is no way to change attribute values Aggregate Functions and Operations Aggregation function takes a set 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 G 1,G 2,...,G n G F1 (A 1 ),F 2 (A 2 ),...,F n (A n )(E) E is any relational-algebra expression 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 A i is an attribute name Note: Some books/articles use γ instead of (Calligraphic G) 3.39 3.40 Aggregate Operation Example Aggregate Operation Example Relation r: Find the average salary in each department A B α α α β β β β β C 7 7 3 10 dept_name avg(salary) (instructor) avg_salary sum(c) (r) sum(c ) 27 3.41 3.42 7

Aggregate Functions (Cont.) Modification of the Database What are the names for attributes in aggregation results? Need some convention E.g., use the expression as a name avg(salary) For convenience, we permit renaming as part of aggregate operation dept_name avg(salary) as avg_sal (instructor) The content of the database may be modified using the following operations: Deletion Insertion Updating All these operations can be expressed using the assignment operator Example: Delete instructors with salary over $1,000,000 R R ( salary>1000000 (R)) 3.43 3.44 Restrictions for Modification Consider a modification where R=(A,B) and S=(C) R This would change the schema of R Should not be allowed Requirements for modifications C>5(S) The name R on the left-hand side of the assignment operator refers to an existing relation in the database schema The expression on the right-hand side of the assignment operator should be union-compatible with R Tuple Relational Calculus 3.45 3.46 Tuple Relational Calculus Predicate Calculus Formula A nonprocedural query language, where each query is of the form {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 A t r denotes that tuple t is in relation r P is a formula similar to that of the predicate calculus 1. Set of attributes and constants 2. Set of comparison operators: (e.g., <,, =,, >, ) 3. Set of logical connectives: and ( ), or (v) not ( ) 4. Implication ( ): x y, if x if true, then y is true 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 3.47 3.48 8

Example Queries Example Queries Find the ID, name, dept_name, salary for instructors whose salary is greater than $80,000 Find the names of all instructors whose department is in the Watson building {t t instructor t [salary ] > 80000} As in the previous query, but output only the ID attribute value {t s instructor (t [ID ] = s [ID ] s [salary ] > 80000)} Notice that a relation on schema (ID) is implicitly defined by the query, because 1) t is not bound to any relation by the predicate 2) we implicitly state that t has an ID attribute (t[id] = s[id]) 3.49 {t s instructor (t [name ] = s [name ] u department (u [dept_name ] = s[dept_name] u [building] = Watson ))} Find the set of all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or both {t s section (t [course_id ] = s [course_id ] s [semester] = Fall s [year] = 2009) v u section (t [course_id ] = u [course_id ] u [semester] = Spring u [year] = 2010)} 3.50 Example Queries Safety of Expressions Find the set of all courses taught in the Fall 2009 semester, and in the Spring 2010 semester {t s section (t [course_id ] = s [course_id ] s [semester] = Fall s [year] = 2009 ) u section (t [course_id ] = u [course_id ] u [semester] = Spring u [year] = 2010)} Find the set of all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester {t s section (t [course_id ] = s [course_id ] s [semester] = Fall s [year] = 2009) u section (t [course_id ] = u [course_id ] u [semester] = Spring u [year] = 2010)} 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 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. 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 P. 3.51 3.52 Universal Quantification Find all students who have taken all courses offered in the Biology department {t r student (t [ID] = r [ID]) ( u course (u [dept_name]= Biology s takes (t [ID] = s [ID ] s [course_id] = u [course_id]))} Note that without the existential quantification on student, the above query would be unsafe if the Biology department has not offered any courses. Domain Relational Calculus 3.53 3.54 9

Domain Relational Calculus Example Queries A nonprocedural query language equivalent in power to the tuple relational calculus Each query is an expression of the form: { < x 1, x 2,, x n > P (x 1, x 2,, x n )} x 1, x 2,, x n represent domain variables Variables that range of attribute values P represents a formula similar to that of the predicate calculus Tuples can be formed using <> E.g., < Einstein, Physics > Find the ID, name, dept_name, salary for instructors whose salary is greater than $80,000 {< i, n, d, s> < i, n, d, s> instructor s > 80000} As in the previous query, but output only the ID attribute value {< i> < i, n, d, s> instructor s > 80000} Find the names of all instructors whose department is in the Watson building {< n > i, d, s (< i, n, d, s > instructor b, a (< d, b, a> department b = Watson ))} 3.55 3.56 Example Queries Find the set of all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or both {<c> a, s, y, b, r, t ( <c, a, s, y, b, t > section s = Fall y = 2009 ) v a, s, y, b, r, t ( <c, a, s, y, b, t > section ] s = Spring y = 2010)} This case can also be written as {<c> a, s, y, b, r, t ( <c, a, s, y, b, t > section ( (s = Fall y = 2009 ) v (s = Spring y = 2010))} Find the set of all courses taught in the Fall 2009 semester, and in the Spring 2010 semester {<c> a, s, y, b, r, t ( <c, a, s, y, b, t > section s = Fall y = 2009 ) a, s, y, b, r, t ( <c, a, s, y, b, t > section ] s = Spring y = 2010)} The expression: Safety of Expressions { < x 1, x 2,, x n > P (x 1, x 2,, x n )} 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 as constants in P or in a tuple of a relation mentioned in P ). 2. For every there exists subformula of the form x (P 1 (x )), the subformula is true if and only if there is a value of x in dom (P 1 ) such that P 1 (x ) is true. 3. For every for all subformula of the form x (P 1 (x )), the subformula is true if and only if P 1 (x ) is true for all values x from dom (P 1 ). 3.57 3.58 Universal Quantification Find all students who have taken all courses offered in the Biology department {< i > n, d, tc ( < i, n, d, tc > student ( ci, ti, dn, cr ( < ci, ti, dn, cr > course dn = Biology si, se, y, g ( <i, ci, si, se, y, g> takes ))} Note that without the existential quantification on student, the above query would be unsafe if the Biology department has not offered any courses. Relationship between Relational Algebra and Tuple (Domain) Calculus Codd s theorem Relational algebra and tuple calculus are equivalent That means that every query expressible in relational algebra can also be expressed in tuple calculus and vice versa Since domain calculus is as expressive as tuple calculus the same holds for the domain calculus Note: Here relational algebra refers to the standard version (no aggregation and projection with functions) * Above query fixes bug in page 246, last query 3.59 3.60 10

Relational algebra Standard relational algebra: Recap Selection, projection, renaming, cross product, union, set difference Null values Semantic sugar operators: Intersection, joins, division, Extensions: Aggregation, extended projection Tuple Calculus safety Domain Calculus End of Chapter 3 Modified from: Database System Concepts, 6 th Ed. 3.61 Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Outline Figure 6.01 Introduction Relational Data Model Formal Relational Languages (relational algebra) SQL - Introduction Database Design Transaction Processing, Recovery, and Concurrency Control Storage and File Structures Indexing and Hashing Query Processing and Optimization 3.63 3.64 Figure 6.02 Figure 6.03 3.65 3.66 11

Figure 6.04 Figure 6.05 3.67 3.68 Figure 6.06 Figure 6.07 3.69 3.70 Figure 6.08 Figure 6.09 3.71 3.72 12

Figure 6.10 Figure 6.11 3.73 3.74 Figure 6.12 Figure 6.13 3.75 3.76 Figure 6.14 Figure 6.15 3.77 3.78 13

Figure 6.16 Figure 6.17 3.79 3.80 Figure 6.18 Figure 6.19 3.81 3.82 Figure 6.20 Figure 6.21 3.83 3.84 14

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. r 1 σ branch_city = Needham (account branch ) r 2 account_number, branch_name, balance (r 1 ) r 3 customer_name, account_number (r 2 account account r 2 depositor) depositor depositor r 3 3.85 3.86 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 {( 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 ) 3.87 3.88 Updating Update Examples A mechanism to change a value in a tuple without charging all values in the tuple Use the generalized projection operator to do this task Each F i is either r F 1, F 2,, F,( r ) l the I th attribute of r, if the I th attribute is not updated, or, if the attribute is to be updated F i is an expression, involving only constants and the attributes of r, which gives the new value for the attribute Make interest payments by increasing all balances by 5 percent. 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)) account account_number, branch_name, balance * 1.05 (account) 3.89 3.90 15

Example Queries Example Queries Find the names of all customers who have a loan and an account at bank. customer_name (borrower) customer_name (depositor) 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 )) Find the name of all customers who have a loan at the bank and the loan amount Query 2 customer_name (σ branch_name = Uptown (depositor account)) customer_name, loan_number, amount (borrower loan) customer_name, branch_name (depositor account) ρ temp(branch_name) ({( Downtown ), ( Uptown )}) Note that Query 2 uses a constant relation. 3.91 3.92 Bank Example Queries Find all customers who have an account at all branches located in Brooklyn city. customer_name, branch_name (depositor account) branch_name (σ branch_city = Brooklyn (branch)) 3.93 16