1. Introduction; Selection, Projection. 2. Cartesian Product, Join. 5. Formal Definitions, A Bit of Theory

Size: px
Start display at page:

Download "1. Introduction; Selection, Projection. 2. Cartesian Product, Join. 5. Formal Definitions, A Bit of Theory"

Transcription

1 Relational Algebra 169 After completing this chapter, you should be able to enumerate and explain the operations of relational algebra (there is a core of 5 relational algebra operators), write relational algebra queries of the type join select project, discuss correctness and equivalence of given relational algebra queries. Relational Algebra 170 Overview 1. Introduction; Selection, Projection 2. Cartesian Product, Join 3. Set Operations 4. Outer Join 5. Formal Definitions, A Bit of Theory

2 Example Database (recap) 171 STUDENTS SID FIRST LAST 101 Ann Smith Michael Jones (null) 103 Richard Turner Maria Brown... EXERCISES CAT ENO TOPIC MAXPT H 1 Rel.Alg. 10 H 2 SQL 10 M 1 SQL 14 RESULTS SID CAT ENO POINTS 101 H H M H H M H M 1 7 Relational Algebra (1) 172 Relational algebra (RA) is a query language for the relational model with a solid theoretical foundation. Relational algebra is not visible at the user interface level (not in any commercial RDBMS, at least). However, almost any RDBMS uses RA to represent queries internally (for query optimization and execution). Knowledge of relational algebra will help in understanding SQL and relational database systems in general.

3 Relational Algebra (2) 173 In mathematics, an algebra is a set (the carrier), and operations that are closed with respect to the set. Example: (N, {, +}) forms an algebra. In case of RA, the carrier is the set of all finite relations. We will get to know the operations of RA in the sequel (one such operation is, for example, ). Relational Algebra (3) 174 Another operation of relational algebra is selection. In contrast to operations like + in N, the selection σ is parameterized by a simple predicate. For example, the operation σ SID=101 selects all tuples in the input relation which have the value 101 in column SID. Relational algebra: selection σ SID=101 RESULTS SID CAT ENO POINTS 101 H H M H H M H M 1 7 = SID CAT ENO POINTS 101 H H M 1 12

4 Relational Algebra (4) 175 Since the output of any RA operation is some relation R again, R may be the input for another RA operation. The operations of RA nest to arbitrary depth such that complex queries can be evaluated. The final results will always be a relation. A query is a term (or expression) in this relational algebra. A query π FIRST,LAST (STUDENTS σ CAT= M (RESULTS)) Relational Algebra (5) 176 There are some difference between the two query languages RA and SQL: Null values are usually excluded in the definition of relational algebra, except when operations like outer join are defined. Relational algebra treats relations as sets, i.e., duplicate tuples will never occur in the input/output relations of an RA operator. Remember: In SQL, relations are multisets (bags) and may contain duplicates. Duplicate elimination is explicit in SQL (SELECT DISTINCT).

5 Relational Algebra (6) 177 Relational algebra is the query language when it comes to the study of relational query languages (DB Theory): The semantics of RA is much simpler than that of SQL. RA features five basic operations (and can be completely defined on a single page, if you will). RA is also a yardstick for measuring the expressiveness of query languages. If a query language QL can express all possible RA queries, then QL is said to be relationally complete. SQL is relationally complete. Vice versa, every SQL query (without null values, aggregation, and duplicates) can also be written in RA. Selection (1) 178 Selection The selection σ ϕ selects a subset of the tuples of a relation, namely those which satisfy predicate ϕ. Selections acts like a filter on a set. Selection σ A=1 A B A B =

6 Selection (2) 179 A simple selection predicate ϕ has the form Term ComparisonOperator Term. Term is an expression that can be evaluated to a data value for a given tuple: an attribute name, a constant value, an expression built from attributes, constants, and data type operations like +,,, /. Selection (3) 180 ComparisonOperator is = (equals), (not equals), < (less than), > (greater than),,, or other data type-dependent predicates (e.g., LIKE). Examples for simple selection predicates: LAST = Smith POINTS 8 POINTS = MAXPT.

7 Selection (4) 181 σ ϕ (R) may be imlemented as: Naive selection create a new temporary relation T ; foreach t R do p ϕ(t); if p then insert t into T ; fi od return T ; If index structures are present (e.g., a B-tree index), it is possible to evaluate σ ϕ (R) without reading every tuple of R. Selection (5) 182 A few corner cases σ C=1 σ A=A σ 1=2 A B A B A B = = (schema error) A B = A B

8 Selection (6) 183 σ ϕ (R) corresponds to the following SQL query: SELECT * FROM R WHERE ϕ A different relational algebra operation called projection corresponds to the SELECT clause. Source of confusion. Selection (7) 184 More complex selection predicates may be performed using the Boolean connectives: ϕ 1 ϕ 2 ( and ), ϕ 1 ϕ 2 ( or ), ϕ 1 ( not ). Note: σ ϕ1 ϕ 2 (R) = σ ϕ1 (σ ϕ2 (R)). and Are the Boolean connectives, strictly needed? The selection predicate must permit evaluation for each input tuple in isolation. Thus, exists ( ) and for all ( ) or nested relational algebra queries are not permitted in selection predicates. Actually, such predicates do not add to the expressiveness of RA.

9 Projection (1) 185 Projection The projection π L eliminates all attributes (columns) of the input relation but those mentioned in the projection list L. Projection π A,C A B C = A C Projection (2) 186 The projection π Ai1,...,A ik (R) produces for each input tuple (A 1 : d 1,..., A n : d n ) an output tuple (A i1 : d i1,..., A ik : d ik ). π may be used to reorder columns. σ discards rows, π discards columns. DB slang: All attributes not in L are projected away.

10 Projection (3) 187 In general, the cardinalities of the input and output relations are not equal. Projection eliminates duplicates A B π B = B 4 5 Projection (4) 188 π Ai1,...,A ik (R) may be imlemented as: Naive projection create a new temporary relation T ; foreach t = (A 1 : d 1,..., A n : d n ) R do u (A i1 : d i1,..., A ik : d ik ); insert u into T ; od eliminate duplicate tuples in T ; return T ; The necessary duplicate elimination makes π L one of the more costly operations in RDBMSs. Thus, query optimizers try hard to prove that the duplicate eliminaton step is not necessary.

11 Projection (5) 189 If RA is used to formalize the semantics of SQL, the format of the projection list is often generalized: Attribute renaming: π B1 A i1,...,b k A ik (R). Computations (e.g., string concatenation via ) to derive the value in new columns, e.g.: π SID,NAME FIRST LAST (STUDENTS). Such generalized π operators are also referred to as map operators (as in functional programming languages). Projection (6) 190 π A1,...,A k (R) corresponds to the SQL query: SELECT DISTINCT FROM A 1,...,A k R π B1 A 1,...,B k A k (R) is equivalent to the SQL query: SELECT DISTINCT FROM A 1 [AS] B 1,...,A k [AS] B k R

12 Selection vs. Projection 191 Selection vs. Projection Selection σ Projection π A 1 A 2 A 3 A 4 A 1 A 2 A 3 A 4 Filter some rows Maps all rows Combining Operations (1) 192 Since the result of any relational algebra operation is a relation again, this intermediate result may be the input of a subsequent RA operation. Example: retrieve the exercises solved by student with ID 102: π CAT,ENO (σ SID=102 (RESULTS)). We can think of the intermediate result to be stored in a named temporary relation (or as a macro definition): S102 σ SID=102 (RESULTS); π CAT,ENO (S102)

13 Combining Operations (2) 193 Composite RA expressions are typically depicted as operator trees: π CAT,ENO σ SID=102 + y RESULTS x 2 In these trees, computation proceeds bottom-up. The evaluation order of sibling branches is not pre-determined. Combining Operations (3) 194 SQL-92 permits the nesting of queries (the result of a SQL query may be used in a place of a relation name): Nested SQL Query SELECT DISTINCT CAT, ENO FROM (SELECT * FROM RESULTS WHERE SID = 102) AS S102 Note that this is not the typical style of SQL querying.

14 Combining Operations (4) 195 Instead, a single SQL query is equivalent to an RA operator tree containing σ, π, and (multiple) (see below): SELECT-FROM-WHERE Block SELECT DISTINCT CAT, ENO FROM RESULTS WHERE SID = 102 Really complex queries may be constructed step-by-step (using SQL s view mechanism), S102 may be used like a relation: SQL View Definition CREATE VIEW S102 AS SELECT * FROM RESULTS WHERE SID = 102 Relational Algebra 196 Overview 1. Introduction; Selection, Projection 2. Cartesian Product, Join 3. Set Operations 4. Outer Join 5. Formal Definitions, A Bit of Theory

15 Cartesian Product (1) 197 In general, queries need to combine information from several tables. In RA, such queries are formulated using, the Cartesian product. Cartesian Product The Cartesian product R S of two relations R, S is computed by concatenating each tuple t R with each tuple u S. ( denotes tuple concatenation.) Cartesian Product (2) 198 Cartesian Product A B C D = A B C D Since attribute names must be unique within a tuple, the Cartesian product may only be applied if R, S do not share any attribute names. (This is no real restriction because we have π.)

16 Cartesian Product (3) 199 If t = (A 1 : a 1,..., A n : a n ) and u = (B 1 : b 1,..., B m : b m ), then t u = (A 1 : a 1,..., A n : a n, B 1 : b 1,..., B m : b m ). Cartesian Product: Nested Loops create a new temporary relation T ; foreach t R do foreach u S do insert t u into T ; od od return T ; Cartesian Product and Renaming 200 R S may be computed by the equivalent SQL query (SQL does not impose the unique column name restriction, a column A of relation R may uniquely be identified by R.A): Cartesian Product in SQL SELECT * FROM R, S In RA, this is often formalized by means of of a renaming operator ϱ X (R). If sch(r) = (A 1 : D 1,..., A n : D n ), then ϱ X (R) π X.A1 A 1,...,X.A n A n (R).

17 Join (1) 201 The intermediate result generated by a Cartesian product may be quite large in general ( R = n, S = m R S = n m). Since the combination of Cartesian product and selection in queries is common, a special operator join has been introduced. Join The (theta-)join R θ S between relations R, S is defined as R θ S σ θ (R S). The join predicate θ may refer to attribute names of R and S. Join (2) 202 ϱ S (STUDENTS) S.SID=R.SID ϱ R (RESULTS) S.SID S.FIRST S.LAST S. R.SID R.CAT R.ENO R.POINTS 101 Ann Smith H Ann Smith H Ann Smith M Michael Jones (null) 102 H Michael Jones (null) 102 H Michael Jones (null) 102 M Richard Turner H Richard Turner M 1 7 Note: student Maria Brown does not appear in the join result.

18 Join (3) 203 R θ S can be evaluated by folding the procedures for σ, : Nested Loop Join create a new temporary relation T ; foreach t R do foreach u S do if θ(t u) then insert t u into T ; fi od od return T ; Join (4) 204 Join combines tuples from two relations and acts like a filter: tuples without join partner are removed. Note: if the join is used to follow a foreign key relationship, then no tuples are filtered: Join follows a foreign key relationship (dereference) RESULTS SID=S.SID π S.SID SID,FIRST,LAST, (STUDENTS) There are join variants which act like filters only: left and right semijoin (, ): R θ S π sch(r) (R θ S), or do not filter at all: outer-join (see below).

19 Natural Join 205 The natural join provides another useful abbreviation ( RA macro ). In the natural join R S, the join predicate θ is defined to be an conjunctive equality comparison of attributes sharing the same name in R, S. Natural join handles the necessary attribute renaming and projection. Natural Join Assume R(A, B, C) and S(B, C, D). Then: R S = π A,B,C,D (σ B=B C=C (R π B B,C C,D(S))) (Note: shared columns occur once in the result.) Joins in SQL (1) 206 In SQL, R θ S is normally written as Join in SQL ( classic and SQL-92) SELECT SELECT FROM R,S or FROM WHERE θ R JOIN S ON θ Note: this left query is exactly the SQL equivalent of σ θ (R S) we have seen before. SQL is a declarative language: it is the task of the SQL optimizer to infer that this query may be evaluated using a join instead of a Cartesian product.

20 Algebraic Laws (1) 207 The join satisfies the associativity condition (R S) T R (S T ). In join chains, parentheses are thus superfluous: R S T. Join is not commutative unless it is followed by a projection, i.e., a column reordering: π L (R S) π L (S R). Algebraic Laws (2) 208 A significant number of further algebraic laws hold, which are heavily utilized by the query optimizer. Example: selection push-down. If predicate ϕ refers to attributes in S only, then σ ϕ (R S) R σ ϕ (S). Selection push-down Why is selection push-down considered one of the most significant algebraic optimizations? (Such effficiency considerations are the subject of Datenbanken II. )

21 A Common Query Pattern (1) 209 The following operator tree structure is very common: π A1,...,A k σ ϕ θ1 θ2 R 1 θn 1 R n R n 1 R 2 1 Join all tables needed to answer the query, 2 select the relevant tuples, 3 project away all irrelevant columns. A Common Query Pattern (2) 210 The select-project-join query π A1,...,A k (σ ϕ (R 1 θ1 R 2 θ2 θn 1 R n )) has the obvious SQL equivalent SELECT DISTINCT FROM WHERE AND A 1,...,A k R 1,...,R n ϕ θ 1 AND AND θ n 1 It is a common source of errors to forget a join condition: think of the scenario R(A, B), S(B, C), T (C, D) when attributes A, D are relevant for the query output.

22 Relational Algebra Quiz (Level: Novice) 211 STUDENTS SID FIRST LAST 101 Ann Smith Michael Jones (null) 103 Richard Turner Maria Brown... EXERCISES CAT ENO TOPIC MAXPT H 1 Rel.Alg. 10 H 2 SQL 10 M 1 SQL 14 RESULTS SID CAT ENO POINTS 101 H H M H H M H M 1 7 Relational Algebra Quiz (Level: Novice) 212 Formulate equivalent queries in RA 1 Print all homework results for Ann Smith (print exercise number and points). 2 Who has got the maximum possible number of points (MAXPT) for a homework? Print full name and homework number. 3 (Who has got the maximum number of points for all homework exercises?)

23 Self Joins (1) 213 Sometimes it is necessary to refer to more than one tuple of the same relation at the same time. Example: Who got more points than the student with ID 101 for any of the exercises? Two answer this query, we need to compare two tuples t, u of the relation RESULTS: 1 tuple t corresponding to the student with ID 101, 2 tuple u, corresponding to the same exercise as the tuple t, in which u.points > t.points. Self Joins (2) 214 This requires a generalization of the select-project-join query pattern, in which two instances of the same relation are joined (the attributes in at least one instances must be renamed first): S := ϱ X (RESULTS) ϱ Y (RESULTS) X.CAT=Y.CAT X.ENO=Y.ENO π X.SID (σ X.POINTS>Y.POINTS Y.SID=101 )(S) Such joins are commonly referred to as self joins.

24 Relational Algebra 215 Overview 1. Introduction; Selection, Projection 2. Cartesian Product, Join 3. Set Operations 4. Outer Join 5. Formal Definitions, A Bit of Theory Set Operations (1) 216 Relations are sets (of tuples). The usual family of binary set operations can also be applied to relations. It is a requirement that both input relations have the same schema. Set Operations The set operations of relational algebra are R S, R S, and R S (union, intersection, difference). A minimal set of operations Which of these set operations is redundant (i.e., may be derived using an alternative RA expression, just like )?

25 Set Operations (2) 217 R R S R S S R S S R Set Operations (3) 218 R S may be implemented as follows: Union create a new temporary relation T ; foreach t R do insert t into T ; od foreach t S do insert t into T ; od remove duplicates in T ; return T ;

26 Set Operations (4) 219 R S may be implemented as follows: Difference create a new temporary relation T ; foreach t R do remove false; foreach u S do remove remove (t = u); od if remove then insert t into T ; fi od return T ; Union (1) 220 In RA queries, a typical application for is case analysis. Example: Grading MPOINTS := π SID,POINTS (σ CAT= M ENO=1 (RESULTS))) π SID,GRADE A (σ POINTS 12 (MPOINTS)) π SID,GRADE B (σ POINTS 10 POINTS<12 (MPOINTS)) π SID,GRADE C (σ POINTS 7 POINTS<10 (MPOINTS)) π SID,GRADE F (σ POINTS<7 (MPOINTS))

27 Union (2) 221 In SQL, is directly supported: keyword UNION. UNION may be placed between two SELECT-FROM-WHERE blocks: SQL s UNION SELECT SID, A AS GRADE FROM RESULTS WHERE CAT = M AND ENO = 1 AND POINTS = 12 UNION SELECT SID, B AS GRADE FROM RESULTS WHERE CAT = M AND ENO = 1 AND POINTS = 10 AND POINTS 12 UNION... Set Difference (1) 222 Note: the RA operators σ, π,,, are monotic by definition, e.g.: R S = σ ϕ (R) σ ϕ (S). Then it follows that every query Q that exclusively uses the above operators behaves monotonically: Let I 1 be a database state, and let I 2 = I 1 {t} (database state after insertion of tuple t). Then every tuple u contained in the answer to Q in state I 1 is also contained in the anser to Q in state I 2. Database insertion never invalidates a correct answer.

28 Set Difference (2) 223 If we pose non-monotonic queries, e.g., Which student has not solved any exercise? Who got the most points for Homework 1? Who has solved all exercises in the database? then it is obvious that σ, π,,, are not sufficient to formulate the query. Such queries require set difference ( ). A non-monotonic query Which student has not solved any exercise? (Print full name (FIRST, LAST). (Example database tables repeated on next slide.) Example Database (recap) 224 STUDENTS SID FIRST LAST 101 Ann Smith Michael Jones (null) 103 Richard Turner Maria Brown... EXERCISES CAT ENO TOPIC MAXPT H 1 Rel.Alg. 10 H 2 SQL 10 M 1 SQL 14 RESULTS SID CAT ENO POINTS 101 H H M H H M H M 1 7

29 Set Difference (3) 225 A correct solution? π FIRST,LAST (STUDENTS SID SID2 π SID2 SID (RESULTS)) A correct solution? π SID,FIRST,LAST (STUDENTS π SID (RESULTS)) Correct solution! Set Difference (4) 226 A typical RA query pattern involving set difference is the anti-join. Given R(A, B) and S(B, C), retrieve the tuples of R that do not have a (natural) join partner in S (Note: sch(r) sch(s) = {B}): R (π B (R) π B (S)). (The following is equivalent: R π sch(r) (R S).) There is no common symbol for this anti-join, but R S seems appropriate (complemented semi-join).

30 Set Operations and Complex Selections 227 Note that the availability of, (and ) renders complex selection predicates superfluous: Predicate Simplification Rules σ ϕ1 ϕ 2 (Q) = σ ϕ1 (Q) σ ϕ2 (Q) σ ϕ1 ϕ 2 (Q) = σ ϕ1 (Q) σ ϕ2 (Q) σ ϕ (Q) = Q σ ϕ (Q) RDBMS implement complex selection predicates anyway Why? Relational Algebra Quiz (Level: Intermediate) 228 The RA quiz below refers to the Homework DB. Schema: RESULTS (SID STUDENTS,(CAT, ENO) EXERCISES, POINTS) STUDENTS (SID,FIRST,LAST, ) EXERCISES (CAT,ENO,TOPIC,MAXPT) Formulate equivalent queries in RA 1 Who got the most points (of all students) for Homework 1? 2 Which students solved all exercises in the database?

31 Union vs. Join 229 Find RA expressions that translate between the two Two alternative representations of the homework, midterm exam, and final totals of the students are: RESULTS 1 STUDENT H M F Jim Ford Ann Smith RESULTS 2 STUDENT CAT PCT Jim Ford H 95 Jim Ford M 60 Jim Ford F 75 Ann Smith H 80 Ann Smith M 90 Ann Smith F 95 Summary 230 The five basic operations of relational algebra are: 1 σ ϕ 2 π L Selection Projection Cartesian Product Union Difference Derived (and thus redundant) operations: Theta-Join θ, Natural Join, Semi-Join, Renaming ϱ, and Intersection.

32 Relational Algebra 231 Overview 1. Introduction; Selection, Projection 2. Cartesian Product, Join 3. Set Operations 4. Outer Join 5. Formal Definitions, A Bit of Theory Outer Join (1) 232 Join ( ) eliminates tuples without partner: A B a 1 b 1 a 1 b 2 B C b 2 c 2 = b 3 c 3 A B C a 2 b 2 c 2 The left outer join preserves all tuples in its left argument, even if a tuple does not team up with a partner in the join: A B a 1 b 1 a 1 b 2 B C b 2 c 2 = b 3 c 3 A B C a 1 b 1 (null) a 2 b 2 c 2

33 Outer Join (2) 233 The right outer join preserves all tuples in its right argument: A B a 1 b 1 a 1 b 2 B C b 2 c 2 = b 3 c 3 A B C a 2 b 2 c 2 (null) b 3 c 3 The full outer join preserves all tuples in both arguments: A B a 1 b 1 a 1 b 2 B C b 2 c 2 = b 3 c 3 A B C a 1 b 1 (null) a 2 b 2 c 2 (null) b 3 c 3 Outer Join (3) 234 R θ S create a new temporary relation T ; foreach t R do haspartner false; foreach u S do if θ(t u) then insert t u into T ; haspartner true; fi od if haspartner then insert t (null,..., null) into T ; }{{} # attributes in S fi od return T ;

34 Outer Join (4) 235 Example: Prepare a full homework results report, including those students who did not hand in any solution at all: STUDENTS SID=SID π SID SID,ENO,POINTS(σ CAT= H (RESULTS)) SID FIRST LAST SID ENO POINTS 101 Ann Smith Ann Smith Michael Jones (null) Michael Jones (null) Richard Turner Maria Brown... (null) (null) (null) Outer Join (5) 236 Join vs. Outer Join Is there any difference between STUDENTS RESULTS and STUDENTS RESULTS? (Can you tell without looking at the table states?) Note: Outer join is a derived operation (like, ), i.e., it can simulated using the five basic relational algebra operations. Consider R(A, B) and S(B, C). Then R S (R S) ( (R π A,B (R S)) {(C:null)} ) SQL-92 provides {FULL, LEFT, RIGHT} OUTER JOIN.

35 Relational Algebra 237 Overview 1. Introduction; Selection, Projection 2. Cartesian Product, Join 3. Set Operations 4. Outer Join 5. Formal Definitions, A Bit of Theory Definitions: Syntax (1) 238 Let the following be given: A set D of data type names and for each D D a set val(d) of values. A set A of valid attribute names (identifiers). Relational Database Schema A relational database schema S consists of a finite set of relation names R, and for every R R a relation schema sch(r). (We ill ignore constraints here.)

36 Definitions: Syntax (2) 239 The set of syntactically correct RA expressions or queries is defined recursively, together with the resulting schema of each expression. Syntax of RA (Base Cases) 1 R (relation name) For every R R, R is an RA expression with schema sch(r). 2 {(A 1 :d 1,..., A n :d n )} (relation constant) A relation constant is an RA expression if A 1,..., A n A, d i val(d i ) for 1 i n with D 1,..., D n D. The schema of this expression is (A 1 :D 1,..., A n :D n ). Definitions: Syntax (3) 240 Let Q be an RA expr. with schema s = (A 1 :D 1,..., A n :D n ). Syntax of RA (Recursive Cases) 3 σ Ai =A j (Q) for i, j {1,..., n} is an RA expression with schema s. 4 σ Ai =d(q) for i {1,..., n} and d val(d i ) is an RA expression with schema s. 5 π B1 A i1,...,b m A im (Q) for i 1,..., i m {1,..., n} and B 1,..., B m A such that B j B k for j k is an RA expression with schema (B 1 :D i1,..., B m :D im ).

37 Definitions: Syntax (4) 241 Let Q 1, Q 2 be an RA expressions with the same schema s. Syntax of RA (Recursive Cases) 6 Q 1 Q 2 and 7 Q 1 Q 2 are RA expressions with schema s. Let Q 1, Q 2 be RA expressions with schemas (A 1 :D 1,..., A n :D n ) and (B 1 :E 1,..., B m :E m ), respectively. Syntax of RA (Recursive Cases) 8 Q 1 Q 2 is an RA expression with schema (A 1 :D 1,..., A n :D n, B 1 :E 1,..., B m :E m ) if {A 1,..., A n } {B 1,..., B m } =. Definitions: Semantics (1) 242 Database State A database state I (instance) defines a relation I(R) for every relation name R in the database schema S. The result of a query Q, i.e., an RA expression, in a database state I is a relation. This relation is denoted by I[Q] and defined recursively corresponding to the syntactic structure of Q.

38 Definition: Semantics (2) 243 I[Q] If Q is a relation name R, then I[Q] := I(R). If Q is a constant relation {(A 1 :d 1,..., A n :d n )}, then I[Q] := {(d 1,..., d n )}. If Q has the form σ Ai =A j (Q 1 ), then I[Q] := {(d 1,..., d n ) I[Q 1 ] d i = d j } If Q has the form σ Ai =d(q 1 ), then I[Q] := {(d 1,..., d n ) I[Q 1 ] d i = d} If Q has the form π B1 A i1,...,b m A im (Q 1 ), then I[Q] := {(d i1,..., d im ) (d 1,..., d n ) I[Q 1 ]} Definition: Semantics (3) 244 I[Q] (continued) If Q has the form Q 1 Q 2, then I[Q] := I[Q 1 ] I[Q 2 ] If Q has the form Q 1 Q 2, then I[Q] := I[Q 1 ] I[Q 2 ] If Q has the form Q 1 Q 2, then I[Q] := { (d 1,..., d n, e 1,..., e m ) (d 1,..., d n ) I[Q 1 ], (e 1,..., e m ) I[Q 2 ]}.

39 Monotonicity 245 Smaller Database State A database state I 1 is smaller than (or equal to) a database state I 2, written I 1 I 2, iff I 1 (R) I 2 (R) for all relation names R R of schema S. Theorem: RA \{ } is monotonic If an RA expression Q does not contain the (set difference) operator, then the following holds for all database states I 1, I 2 : I 1 I 2 = I 1 [Q] I 2 [Q]. Formulate proof by induction on syntactic structure of Q ( structural induction ). Equivalence (1) 246 Equivalence of RA Expressions Two RA expressions Q 1 and Q 2 are equivalent iff they have the same (result) schema and for all database states I, the following holds: I[Q 1 ] = I[Q 2 ]. Examples: σ ϕ1 (σ ϕ2 (Q)) = σ ϕ2 (σ ϕ1 (Q)) (Q 1 Q 2 ) Q 3 = Q 1 (Q 2 Q 3 ) If A is an attribute in the result schema of Q 1, then σ A=d (Q 1 Q 2 ) = (σ A=d (Q 1 )) Q 2. Theorem: The equivalence of (arbitrary) relational algebra expressions is undecidable.

40 Limitations of RA (1) 247 Let R be a relation name and assume sch(r) = (A:D, B:D), i.e., both columns share the same data type D. Let val(d) be infinite. The transitive closure of I(R) is the set of all (d, e) val(d) val(d) such that there are n N, n 1, and d 0, d 1,..., d n val(d) with d = d 0, e = d n and (d i 1, d i ) I(R) for i = 1,..., n. a d b c R from a b c to b c d Limitations of RA (2) 248 Theorem: There is no RA expression Q such that I[Q] is the transitive closure of I(R) for all database states I. In the directed graph example, one self-join (of R with itself) is needed, to follow the edges in the graph: π S.from,T.to (ϱ S (R) S.to=T.from ϱ T (R)) An n-fold self-join will find all paths in the graph of length n + 1. To compute the transitive closure for arbitrary graphs, i.e., for all database states I, is impossible in RA.

41 Limitations of RA (3) 249 This of course implies that relational algebra is not computationally complete. There are functions from database states to relations (query results), for which we could write a program 4, but we will not be able to find an equivalent RA expression to do the same. However, this would have been truly unexpected and actually unwanted, because want a guarantee that query evaluation always terminates. This is guaranteed for RA. Otherwise, we would have solved the halting problem. 4 Pick your favourite programming language. Limitations of RA (4) 250 All RA queries can be evaluated in time that is polynomically in the size of the database state. This implies that certain complex problems cannot be formulated in relational algebra. For example, if you find a way to formulate the Traveling Salesman problem in RA, you have solved the famous P = NP problem. (With a solution that nobody expects; contact me to collect your PhD.) As the transitive closure example shows, even not all problems of polynomial complexity can be formulated in classical RA.

42 Expressive Power (1) 251 Relational Completeness A query language L for the relational model is called strong relationally complete if, for every DB schema S and for every RA epxression Q 1 with respect to S there is a query Q 2 L such that for all database states I with respect to S the two queries produce the same results: I[Q 1 ] = I[Q 2 ]. Read as: It is possible to write an RA-to-L query compiler. Expressive Power (2) 252 SQL is strong relationally complete. If we can even write RA-to-L as well as L-to-RA compilers, both query languages are equivalent. SQL and RA are not equivalent. SQL contains concepts, e.g., the aggregate COUNT, which cannot be simulated in RA. Equivalent Query Languages 1 Relational algebra, 2 SQL without aggregations and with mandatory duplicate elimination, 3 Tuple relational calculus, 4 Datalog (a Prolog variant) without recursion.

Database Systems I Foundations of Databases

Database Systems I Foundations of Databases Database Systems I Foundations of Databases Summer term 2010 Melanie Herschel melanie.herschel@uni-tuebingen.de Database Systems Group, University of Tübingen 1 Chapter 5 Relational Algebra After completing

More information

Databases. Jörg Endrullis. VU University Amsterdam

Databases. Jörg Endrullis. VU University Amsterdam Databases Jörg Endrullis VU University Amsterdam The Relational Model Overview 1. Relational Model Concepts: Schema, State 2. Null Values 3. Constraints: General Remarks 4. Key Constraints 5. Foreign Key

More information

Databases. Jörg Endrullis. VU University Amsterdam

Databases. Jörg Endrullis. VU University Amsterdam Databases Jörg Endrullis VU University Amsterdam 2015 SQL Overview Overview 1. SELECT-FROM-WHERE Blocks, Tuple Variables 2. Subqueries, Non-Monotonic Constructs 3. Aggregations I: Aggregation Functions

More information

After completing this chapter, you should be able to

After completing this chapter, you should be able to SQL 229 After completing this chapter, you should be able to write advanced SQL queries including, e.g., multiple tuple variables over different/the same relation, use aggregation, grouping, UNION, be

More information

Relational Databases

Relational Databases Relational Databases Jan Chomicki University at Buffalo Jan Chomicki () Relational databases 1 / 49 Plan of the course 1 Relational databases 2 Relational database design 3 Conceptual database design 4

More information

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes Announcements Relational Model & Algebra CPS 216 Advanced Database Systems Lecture notes Notes version (incomplete) available in the morning on the day of lecture Slides version (complete) available after

More information

Relational Model: History

Relational Model: History Relational Model: History Objectives of Relational Model: 1. Promote high degree of data independence 2. Eliminate redundancy, consistency, etc. problems 3. Enable proliferation of non-procedural DML s

More information

Relational Model and Relational Algebra

Relational Model and Relational Algebra Relational Model and Relational Algebra CMPSCI 445 Database Systems Fall 2008 Some slide content courtesy of Zack Ives, Ramakrishnan & Gehrke, Dan Suciu, Ullman & Widom Next lectures: Querying relational

More information

Information Systems (Informationssysteme)

Information Systems (Informationssysteme) Information Systems (Informationssysteme) Jens Teubner, TU Dortmund jens.teubner@cs.tu-dortmund.de Summer 2016 c Jens Teubner Information Systems Summer 2016 1 Part V The Relational Data Model c Jens Teubner

More information

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

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

More information

Relational Database: The Relational Data Model; Operations on Database Relations

Relational Database: The Relational Data Model; Operations on Database Relations Relational Database: The Relational Data Model; Operations on Database Relations Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin Overview

More information

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems Relational Model & Algebra CompSci 316 Introduction to Database Systems Announcements (Tue. Sep. 3) Homework #1 has been posted Sign up for Gradiance now! Windows Azure passcode will be emailed soon sign

More information

Ian Kenny. November 28, 2017

Ian Kenny. November 28, 2017 Ian Kenny November 28, 2017 Introductory Databases Relational Algebra Introduction In this lecture we will cover Relational Algebra. Relational Algebra is the foundation upon which SQL is built and is

More information

Relational Model, Relational Algebra, and SQL

Relational Model, Relational Algebra, and SQL Relational Model, Relational Algebra, and SQL August 29, 2007 1 Relational Model Data model. constraints. Set of conceptual tools for describing of data, data semantics, data relationships, and data integrity

More information

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto

Lecture Query evaluation. Combining operators. Logical query optimization. By Marina Barsky Winter 2016, University of Toronto Lecture 02.03. Query evaluation Combining operators. Logical query optimization By Marina Barsky Winter 2016, University of Toronto Quick recap: Relational Algebra Operators Core operators: Selection σ

More information

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems

Relational Model & Algebra. Announcements (Thu. Aug. 27) Relational data model. CPS 116 Introduction to Database Systems Relational Model & Algebra CPS 116 Introduction to Database Systems Announcements (Thu. Aug. 27) 2 Homework #1 will be assigned next Tuesday Office hours: see also course website Jun: LSRC D327 Tue. 1.5

More information

CMP-3440 Database Systems

CMP-3440 Database Systems CMP-3440 Database Systems Relational DB Languages Relational Algebra, Calculus, SQL Lecture 05 zain 1 Introduction Relational algebra & relational calculus are formal languages associated with the relational

More information

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions. COSC 304 Introduction to Database Systems Relational Model and Algebra Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Relational Model History The relational model was

More information

CS 377 Database Systems

CS 377 Database Systems CS 377 Database Systems Relational Algebra and Calculus Li Xiong Department of Mathematics and Computer Science Emory University 1 ER Diagram of Company Database 2 3 4 5 Relational Algebra and Relational

More information

EECS 647: Introduction to Database Systems

EECS 647: Introduction to Database Systems EECS 647: Introduction to Database Systems π Instructor: Luke Huan Spring 2009 dministrative You should already have your PgSQL password. Login cycle2.eecs.ku.edu using your EECS ID and password. Type

More information

8. SQL II 8-1. Part 8: SQL II

8. SQL II 8-1. Part 8: SQL II 8. SQL II 8-1 References: Part 8: SQL II Elmasri/Navathe:Fundamentals of Database Systems, 3rd Edition, 1999. Chap. 8, SQL The Relational Database Standard (Sect. 8.2, 8.3.3, part of 8.3.4.) Silberschatz/Korth/Sudarshan:

More information

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls Today s topics CompSci 516 Data Intensive Computing Systems Lecture 4 Relational Algebra and Relational Calculus Instructor: Sudeepa Roy Finish NULLs and Views in SQL from Lecture 3 Relational Algebra

More information

Chapter 10: XML Support in Modern SQL Databases References:

Chapter 10: XML Support in Modern SQL Databases References: 10. XML-Support in Modern SQL Databases 10-1 Chapter 10: XML Support in Modern SQL Databases References: Georg Lausen: Datenbanken Grundlagen und XML-Technologien. Elsevier/Spektrum, 2005. Folien zu Kapile

More information

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop CMSC 424 Database design Lecture 18 Query optimization Mihai Pop More midterm solutions Projects do not be late! Admin Introduction Alternative ways of evaluating a given query Equivalent expressions Different

More information

RELATIONAL DATA MODEL: Relational Algebra

RELATIONAL DATA MODEL: Relational Algebra RELATIONAL DATA MODEL: Relational Algebra Outline 1. Relational Algebra 2. Relational Algebra Example Queries 1. Relational Algebra A basic set of relational model operations constitute the relational

More information

Informationslogistik Unit 4: The Relational Algebra

Informationslogistik Unit 4: The Relational Algebra Informationslogistik Unit 4: The Relational Algebra 26. III. 2012 Outline 1 SQL 2 Summary What happened so far? 3 The Relational Algebra Summary 4 The Relational Calculus Outline 1 SQL 2 Summary What happened

More information

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University Relational Algebra Lecture 4A Kathleen Durant Northeastern University 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple,

More information

Lecture 16. The Relational Model

Lecture 16. The Relational Model Lecture 16 The Relational Model Lecture 16 Today s Lecture 1. The Relational Model & Relational Algebra 2. Relational Algebra Pt. II [Optional: may skip] 2 Lecture 16 > Section 1 1. The Relational Model

More information

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say

Sets 1. The things in a set are called the elements of it. If x is an element of the set S, we say Sets 1 Where does mathematics start? What are the ideas which come first, in a logical sense, and form the foundation for everything else? Can we get a very small number of basic ideas? Can we reduce it

More information

Relational Algebra 1

Relational Algebra 1 Relational Algebra 1 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports simple, powerful QLs: Strong formal foundation based

More information

Databases Relational algebra Lectures for mathematics students

Databases Relational algebra Lectures for mathematics students Databases Relational algebra Lectures for mathematics students March 5, 2017 Relational algebra Theoretical model for describing the semantics of relational databases, proposed by T. Codd (who authored

More information

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity COS 597A: Principles of Database and Information Systems Relational model continued Understanding how to use the relational model 1 with as weak entity folded into folded into branches: (br_, librarian,

More information

Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Algebra and SQL. Basic Operations Algebra of Bags Relational Algebra and SQL Basic Operations Algebra of Bags 1 What is an Algebra Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators

More information

Relational Algebra. Procedural language Six basic operators

Relational Algebra. Procedural language Six basic operators Relational algebra Relational Algebra Procedural language Six basic operators select: σ project: union: set difference: Cartesian product: x rename: ρ The operators take one or two relations as inputs

More information

Chapter 3. The Relational Model. Database Systems p. 61/569

Chapter 3. The Relational Model. Database Systems p. 61/569 Chapter 3 The Relational Model Database Systems p. 61/569 Introduction The relational model was developed by E.F. Codd in the 1970s (he received the Turing award for it) One of the most widely-used data

More information

Relational Algebra and SQL

Relational Algebra and SQL Relational Algebra and SQL Relational Algebra. This algebra is an important form of query language for the relational model. The operators of the relational algebra: divided into the following classes:

More information

Database Tuning and Physical Design: Basics of Query Execution

Database Tuning and Physical Design: Basics of Query Execution Database Tuning and Physical Design: Basics of Query Execution Spring 2018 School of Computer Science University of Waterloo Databases CS348 (University of Waterloo) Query Execution 1 / 43 The Client/Server

More information

Chapter 2 The relational Model of data. Relational algebra

Chapter 2 The relational Model of data. Relational algebra Chapter 2 The relational Model of data Relational algebra 1 Contents What is a data model? Basics of the relational model How to define? How to query? Constraints on relations 2 An algebraic query language

More information

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra 1.1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple,

More information

CMPT 354: Database System I. Lecture 5. Relational Algebra

CMPT 354: Database System I. Lecture 5. Relational Algebra CMPT 354: Database System I Lecture 5. Relational Algebra 1 What have we learned Lec 1. DatabaseHistory Lec 2. Relational Model Lec 3-4. SQL 2 Why Relational Algebra matter? An essential topic to understand

More information

Part V. Working with Information Systems. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Part V. Working with Information Systems. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1 Part V Working with Information Systems Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1 Outline of this part 1 Introduction to Database Languages Declarative Languages Option 1: Graphical

More information

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

The SQL data-definition language (DDL) allows defining : Introduction to SQL Introduction to SQL Overview of the SQL Query Language Data Definition Basic Query Structure Additional Basic Operations Set Operations Null Values Aggregate Functions Nested Subqueries

More information

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra Note: Slides are posted on the class website, protected by a password written on the board Reading: see class home page www.cs.umb.edu/cs630. Relational Algebra CS430/630 Lecture 2 Relational Query Languages

More information

Database Systems SQL SL03

Database Systems SQL SL03 Inf4Oec10, SL03 1/52 M. Böhlen, ifi@uzh Informatik für Ökonomen II Fall 2010 Database Systems SQL SL03 Data Definition Language Table Expressions, Query Specifications, Query Expressions Subqueries, Duplicates,

More information

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation Relational Databases Relational Databases 1 Relational Model of Data 2 Relational Algebra (and connection to Datalog) Relational database: a set of relations/tables Relation schema : specifies name of

More information

Database Systems SQL SL03

Database Systems SQL SL03 Checking... Informatik für Ökonomen II Fall 2010 Data Definition Language Database Systems SQL SL03 Table Expressions, Query Specifications, Query Expressions Subqueries, Duplicates, Null Values Modification

More information

Chapter 6 The Relational Algebra and Relational Calculus

Chapter 6 The Relational Algebra and Relational Calculus Chapter 6 The Relational Algebra and Relational Calculus Fundamentals of Database Systems, 6/e The Relational Algebra and Relational Calculus Dr. Salha M. Alzahrani 1 Fundamentals of Databases Topics so

More information

Relational Algebra. Algebra of Bags

Relational Algebra. Algebra of Bags Relational Algebra Basic Operations Algebra of Bags What is an Algebra Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols

More information

4/10/2018. Relational Algebra (RA) 1. Selection (σ) 2. Projection (Π) Note that RA Operators are Compositional! 3.

4/10/2018. Relational Algebra (RA) 1. Selection (σ) 2. Projection (Π) Note that RA Operators are Compositional! 3. Lecture 33: The Relational Model 2 Professor Xiannong Meng Spring 2018 Lecture and activity contents are based on what Prof Chris Ré of Stanford used in his CS 145 in the fall 2016 term with permission

More information

Chapter 6 The Relational Algebra and Calculus

Chapter 6 The Relational Algebra and Calculus Chapter 6 The Relational Algebra and Calculus 1 Chapter Outline Example Database Application (COMPANY) Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary

More information

CSC 261/461 Database Systems Lecture 13. Fall 2017

CSC 261/461 Database Systems Lecture 13. Fall 2017 CSC 261/461 Database Systems Lecture 13 Fall 2017 Announcement Start learning HTML, CSS, JavaScript, PHP + SQL We will cover the basics next week https://www.w3schools.com/php/php_mysql_intro.asp Project

More information

Relational Model and Algebra. Introduction to Databases CompSci 316 Fall 2018

Relational Model and Algebra. Introduction to Databases CompSci 316 Fall 2018 Relational Model and Algebra Introduction to Databases CompSci 316 Fall 2018 2 Announcements (Thu. Aug. 30) Sign up for Piazza, NOW! Homework #1 to be posted today; due in 2½ weeks Sign up for Gradiance

More information

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

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University Lecture 3 SQL Shuigeng Zhou September 23, 2008 School of Computer Science Fudan University Outline Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views

More information

CompSci 516: Database Systems

CompSci 516: Database Systems CompSci 516 Database Systems Lecture 4 Relational Algebra and Relational Calculus Instructor: Sudeepa Roy Duke CS, Fall 2018 CompSci 516: Database Systems 1 Reminder: HW1 Announcements Sakai : Resources

More information

Database Systems. Basics of the Relational Data Model

Database Systems. Basics of the Relational Data Model Database Systems Relational Design Theory Jens Otten University of Oslo Jens Otten (UiO) Database Systems Relational Design Theory INF3100 Spring 18 1 / 30 Basics of the Relational Data Model title year

More information

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow Relational Algebra R & G, Chapter 4 By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental power of

More information

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data Outline Conceptual Design: ER model Relational Algebra Calculus Yanlei Diao UMass Amherst Logical Design: ER to relational model Querying and manipulating data Practical language: SQL Declarative: say

More information

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa ICS 624 Spring 2011 Overview of DB & IR Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/12/2011 Lipyeow Lim -- University of Hawaii at Manoa 1 Example

More information

L22: The Relational Model (continued) CS3200 Database design (sp18 s2) 4/5/2018

L22: The Relational Model (continued) CS3200 Database design (sp18 s2)   4/5/2018 L22: The Relational Model (continued) CS3200 Database design (sp18 s2) https://course.ccs.neu.edu/cs3200sp18s2/ 4/5/2018 256 Announcements! Please pick up your exam if you have not yet HW6 will include

More information

2.2.2.Relational Database concept

2.2.2.Relational Database concept Foreign key:- is a field (or collection of fields) in one table that uniquely identifies a row of another table. In simpler words, the foreign key is defined in a second table, but it refers to the primary

More information

Relational Query Languages: Relational Algebra. Juliana Freire

Relational Query Languages: Relational Algebra. Juliana Freire Relational Query Languages: Relational Algebra Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs: Simple

More information

Final Exam Review 2. Kathleen Durant CS 3200 Northeastern University Lecture 23

Final Exam Review 2. Kathleen Durant CS 3200 Northeastern University Lecture 23 Final Exam Review 2 Kathleen Durant CS 3200 Northeastern University Lecture 23 QUERY EVALUATION PLAN Representation of a SQL Command SELECT {DISTINCT} FROM {WHERE

More information

Introduction to Data Management. Lecture #11 (Relational Algebra)

Introduction to Data Management. Lecture #11 (Relational Algebra) Introduction to Data Management Lecture #11 (Relational Algebra) Instructor: Mike Carey mjcarey@ics.uci.edu Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Announcements v HW and exams:

More information

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao Chapter 5 Nguyen Thi Ai Thao thaonguyen@cse.hcmut.edu.vn Spring- 2016 Contents 1 Unary Relational Operations 2 Operations from Set Theory 3 Binary Relational Operations 4 Additional Relational Operations

More information

EECS 647: Introduction to Database Systems

EECS 647: Introduction to Database Systems EECS 647: Introduction to Database Systems Instructor: Luke Huan Spring 2009 Stating Points A database A database management system A miniworld A data model Conceptual model Relational model 2/24/2009

More information

Database Technology Introduction. Heiko Paulheim

Database Technology Introduction. Heiko Paulheim Database Technology Introduction Outline The Need for Databases Data Models Relational Databases Database Design Storage Manager Query Processing Transaction Manager Introduction to the Relational Model

More information

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA Relational Algebra BASIC OPERATIONS 1 What is an Algebra Mathematical system consisting of: Operands -- values from which new values can be constructed. Operators -- symbols denoting procedures that construct

More information

Relational Algebra 1

Relational Algebra 1 Relational Algebra 1 Relational Algebra Last time: started on Relational Algebra What your SQL queries are translated to for evaluation A formal query language based on operators Rel Rel Op Rel Op Rel

More information

Database System Concepts

Database System Concepts Chapter 14: Optimization Departamento de Engenharia Informática Instituto Superior Técnico 1 st Semester 2007/2008 Slides (fortemente) baseados nos slides oficiais do livro c Silberschatz, Korth and Sudarshan.

More information

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton Relational Model and Relational Algebra Rose-Hulman Institute of Technology Curt Clifton Administrative Notes Grading Weights Schedule Updated Review ER Design Techniques Avoid redundancy and don t duplicate

More information

Chapter 2: Intro to Relational Model

Chapter 2: Intro to Relational Model Non è possibile visualizzare l'immagine. Chapter 2: Intro to Relational Model Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Example of a Relation attributes (or columns)

More information

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL Chapter 3: Introduction to SQL Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 3: Introduction to SQL Overview of the SQL Query Language Data Definition Basic Query

More information

Database Theory VU , SS Introduction: Relational Query Languages. Reinhard Pichler

Database Theory VU , SS Introduction: Relational Query Languages. Reinhard Pichler Database Theory Database Theory VU 181.140, SS 2018 1. Introduction: Relational Query Languages Reinhard Pichler Institut für Informationssysteme Arbeitsbereich DBAI Technische Universität Wien 6 March,

More information

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model Web Science & Technologies University of Koblenz Landau, Germany Relational Data Model Overview Relational data model; Tuples and relations; Schemas and instances; Named vs. unnamed perspective; Relational

More information

CS317 File and Database Systems

CS317 File and Database Systems CS317 File and Database Systems Lecture 3 Relational Calculus and Algebra Part-2 September 10, 2017 Sam Siewert RDBMS Fundamental Theory http://dilbert.com/strips/comic/2008-05-07/ Relational Algebra and

More information

Relational Algebra Part I. CS 377: Database Systems

Relational Algebra Part I. CS 377: Database Systems Relational Algebra Part I CS 377: Database Systems Recap of Last Week ER Model: Design good conceptual models to store information Relational Model: Table representation with structures and constraints

More information

Can We Trust SQL as a Data Analytics Tool?

Can We Trust SQL as a Data Analytics Tool? Can We Trust SQL as a Data nalytics Tool? SQL The query language for relational databases International Standard since 1987 Implemented in all systems (free and commercial) $30B/year business Most common

More information

Detecting Logical Errors in SQL Queries

Detecting Logical Errors in SQL Queries Detecting Logical Errors in SQL Queries Stefan Brass Christian Goldberg Martin-Luther-Universität Halle-Wittenberg, Institut für Informatik, Von-Seckendorff-Platz 1, D-06099 Halle (Saale), Germany (brass

More information

Lecture 1: Conjunctive Queries

Lecture 1: Conjunctive Queries CS 784: Foundations of Data Management Spring 2017 Instructor: Paris Koutris Lecture 1: Conjunctive Queries A database schema R is a set of relations: we will typically use the symbols R, S, T,... to denote

More information

ECE 650 Systems Programming & Engineering. Spring 2018

ECE 650 Systems Programming & Engineering. Spring 2018 ECE 650 Systems Programming & Engineering Spring 2018 Relational Databases: Tuples, Tables, Schemas, Relational Algebra Tyler Bletsch Duke University Slides are adapted from Brian Rogers (Duke) Overview

More information

Chapter 12: Query Processing

Chapter 12: Query Processing Chapter 12: Query Processing Overview Catalog Information for Cost Estimation $ Measures of Query Cost Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions Transformation

More information

Chapter 2: Relational Model

Chapter 2: Relational Model Chapter 2: Relational Model Database System Concepts, 5 th Ed. See www.db-book.com for conditions on re-use Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations

More information

Chapter 13: Query Optimization

Chapter 13: Query Optimization Chapter 13: Query Optimization Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 13: Query Optimization Introduction Transformation of Relational Expressions Catalog

More information

Semantic Errors in Database Queries

Semantic Errors in Database Queries Semantic Errors in Database Queries 1 Semantic Errors in Database Queries Stefan Brass TU Clausthal, Germany From April: University of Halle, Germany Semantic Errors in Database Queries 2 Classification

More information

Chapter 3: Relational Model

Chapter 3: Relational Model Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational Calculus Extended Relational-Algebra-Operations Modification of the Database

More information

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board Note: Slides are posted on the class website, protected by a password written on the board Reading: see class home page www.cs.umb.edu/cs630. Relational Algebra CS430/630 Lecture 2 Slides based on Database

More information

Relational Query Languages

Relational Query Languages Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful, declarative QLs with precise semantics: Strong formal foundation

More information

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

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See  for conditions on re-use Chapter 3: SQL Database System Concepts, 5th Ed. See www.db-book.com for conditions on re-use Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested

More information

Discrete Mathematics Lecture 4. Harper Langston New York University

Discrete Mathematics Lecture 4. Harper Langston New York University Discrete Mathematics Lecture 4 Harper Langston New York University Sequences Sequence is a set of (usually infinite number of) ordered elements: a 1, a 2,, a n, Each individual element a k is called a

More information

Chapter 3: SQL. Chapter 3: SQL

Chapter 3: SQL. Chapter 3: SQL Chapter 3: SQL Database System Concepts, 5th Ed. See www.db-book.com for conditions on re-use Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested

More information

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Semantics via Syntax. f (4) = if define f (x) =2 x + 55. 1 Semantics via Syntax The specification of a programming language starts with its syntax. As every programmer knows, the syntax of a language comes in the shape of a variant of a BNF (Backus-Naur Form)

More information

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

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination The Extended Algebra Duplicate Elimination 2 δ = eliminate duplicates from bags. τ = sort tuples. γ = grouping and aggregation. Outerjoin : avoids dangling tuples = tuples that do not join with anything.

More information

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of... Introduction to Data Management Lecture #13 (Relational Calculus, Continued) Instructor: Mike Carey mjcarey@ics.uci.edu Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 It s time for another

More information

Chapter 6 Part I The Relational Algebra and Calculus

Chapter 6 Part I The Relational Algebra and Calculus Chapter 6 Part I The Relational Algebra and Calculus Copyright 2004 Ramez Elmasri and Shamkant Navathe Database State for COMPANY All examples discussed below refer to the COMPANY database shown here.

More information

Slides for Faculty Oxford University Press All rights reserved.

Slides for Faculty Oxford University Press All rights reserved. Oxford University Press 2013 Slides for Faculty Assistance Preliminaries Author: Vivek Kulkarni vivek_kulkarni@yahoo.com Outline Following topics are covered in the slides: Basic concepts, namely, symbols,

More information

Chapter 6: Formal Relational Query Languages

Chapter 6: Formal Relational Query Languages Chapter 6: Formal Relational Query Languages Database System Concepts, 6 th Ed. See www.db-book.com for conditions on re-use Chapter 6: Formal Relational Query Languages Relational Algebra Tuple Relational

More information

Query Processing & Optimization

Query Processing & Optimization Query Processing & Optimization 1 Roadmap of This Lecture Overview of query processing Measures of Query Cost Selection Operation Sorting Join Operation Other Operations Evaluation of Expressions Introduction

More information

Chapter 4: SQL. Basic Structure

Chapter 4: SQL. Basic Structure Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Joined Relations Data Definition Language Embedded SQL

More information

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

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property Relational Algebra ICOM 06 Database Systems Relational Algebra Dr. Amir H. Chinaei Department of Electrical and Computer Engineering University of Puerto Rico, Mayagüez Slides are adapted from: Introduction.

More information

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1 Chapter Seven: Regular Expressions Formal Language, chapter 7, slide The first time a young student sees the mathematical constant π, it looks like just one more school artifact: one more arbitrary symbol

More information