Relational Database Systems 1

Size: px
Start display at page:

Download "Relational Database Systems 1"

Transcription

1 Relational Database Systems 1 Wolf-Tilo Balke / Stephan Mennicke Hermann Kroll / Janus Wawrzinek Institut für Informationssysteme Technische Universität Braunschweig

2 6 Relational Algebra Motivation Relational Algebra basic relational algebra operations Query Optimization additional derived operations Advanced relational algebra Outer Joins Aggregation Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 2

3 6.1 Motivation A data model has three parts: Structure Data structures used to create databases representing modeled objects Integrity Rules expressing constraints placed on these data structures to ensure structural integrity Manipulation Operators that can be applied to the data structures, to update and query the data contained in the database Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 3

4 6.1 Motivation Last week we introduced the relational model based on set theory relations can be written as tables: relation name attributes tuples PERSON first_name last_name sex Clark Joseph Kent m Louise Lane f Lex Luthor m Charles Xavier m Erik Magnus m Jeanne Gray f Ororo Munroe f domain values Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 4

5 Relational Model Tables correspond to relations, table rows to tuples, table columns to attributes or domains Relations are a subset of the Cartesian product of its attribute domains, i.e., R D 1 D n. There are additional constraints on relations Especially, each relation has a primary key with the unique key constraint There can be foreign key constraints, i.e., links between relations Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 5

6 6.1 Motivation How do you work and query relations? Relational algebra! proposed by Edgar F. Codd: A Relational Model for Large Shared Data Banks, Communications of the ACM, 1970 The theoretical foundation of all relational databases describes how to manipulate relations and retrieve interesting parts of available relations relational algebra is mandatory for advanced tasks like query optimization Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 6

7 6.1 Motivation Why do we need special query languages at all? Won t conventional languages like C or Java suffice to ask and answer any computational question about relations? Relational algebra is useful, because it is less powerful than C or Java. 2 rewards ease of programming ability of the compiler to often produce highly optimized code [GUW09, 2] Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 7

8 6.1 Motivation The first thing that happens to an SQL query is that it gets translated into relational algebra. Query Parser & Translator Relational Algebra Expression Query Optimizer Query Result Evaluation Engine Execution Plan Access Paths Statistics Data Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 8

9 6.1 Motivation Queries are translated into an internal form queries posed in a declarative DB language what should be returned, not how the query should be processed queries can be evaluated in different ways Several relational algebra expressions might lead to the same results each statement can be used for query evaluation but different statements might also result in vastly different performance! This is the area of query optimization, the heart of every database kernel Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 9

10 6.1 Motivation Imagine a relation schema LECTURES(Series,Episode,Date) and a relation L over LECTURES. Initially, L =. How do you add/remove/update information in L? How do you query for information in L? Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 10

11 6 Relational Algebra Motivation Relational Algebra basic relational algebra operations Query Optimization additional derived operations Advanced relational algebra Outer Joins Aggregation ρ σ π Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 11

12 6.2 Relational Algebra What is an algebra after all? Study of symbols and their manipulation it consists of operators and atomic operands. it allows to build expressions by applying operators to operands and / or other expressions of the algebra. e.g., algebra of arithmetics atomic operands: variables like x and constants like 15 operators: addition, subtraction, multiplication and division Relational algebra: an example of an algebra made for the relational model of databases atomic operands variables, that stand for relations constants, which are finite relations Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 12

13 6.2 Relational Algebra Elementary operations set algebra operations Set Union Set Intersection Set Difference Cartesian Product new relational algebra operations Selection σ Projection π Renaming ρ Additional derived operations (for convenience) all sorts of joins,,, division EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 13

14 6.2 Example Relations mat no firstname result crs no Student (0,*) (0,*) exam Course lastname sex title Student(mat_no, firstname, lastname, sex) Course(crs_no, title) exam(student Student, course Course, result) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 14

15 6.2 Example Relations Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m 6676 Erik Magnus m 8024 Jeanne Gray f 9876 Logan NULL m Student(mat_no, firstname, lastname, sex) Course(crs_no, title) exam(student Student, course Course, result) EN 6 Course crs_no title 100 Intro to being a Superhero 101 Secret Identities How to take over the world exam student course result Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 15

16 6.2 Relational Algebra Selection σ selects all tuples (rows) from a relation that satisfy the given Boolean predicate (condition) Selection = create a new relation that contains exactly the satisfying tuples Intensional definition of a set of tuples! σ φ R t R t φ Condition (φ) is a Boolean statement given by a connection of clauses Connected by, and (logical AND, OR, and NOT) Condition clauses attribute θ value attribute θ attribute θ {=, <,,, >, } EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 16

17 6.2 Relational Algebra Example Student mat_no firstname lastname sex 1005 Clark Kent m Select all female students. σ sex= f Student 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m 6676 Erik Magnus m 8024 Jeanne Gray f 9876 Logan NULL m σ sex= f Student mat_no firstname lastname sex 2832 Louise Lane f 8024 Jeanne Gray f EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 17

18 Example 6.2 Relational Algebra exam student course result Select exams within course 100 with a result worse than 3.0. σ course=100 result>3.0 exam σ course=100 result>3.0 exam student course result EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 18

19 6.2 Relational Algebra Projection π retains only specified attributes (columns) again: creates a new relation with only those attribute sets which are specified within an attribute list in practice (tables), if tuples are identical after projection, duplicate tuples need to be removed This is due to the set-based nature of relational algebra Let R be a relation over R(A 1,, A k ) and let A ij {A 1,, A k } for 1 j n. π Ai 1,,A in R t A i 1,, A in t R Course crs_no title 100 Intro. to being a Superhero 101 Secret Identities How to take over the world All courses titles. π title Course title Intro. to being a Superhero Secret Identities 2 How to take over the world EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 19

20 6.2 Relational Algebra Of course, operations can be combined Retrieve only the first name and last name of all female students. π firstname, lastname σ sex=ʼfʼ Student Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m 6676 Erik Magnus m 8024 Jeanne Gray f 9876 Logan m σ sex= f Student mat_no firstname lastname sex 2832 Louise Lane f 8024 Jeanne Gray f π firstname, lastname σ sex=ʼfʼ Student firstname lastname Loise Lane Jeanne Gray EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 20

21 6.2 Relational Algebra Renaming operator ρ renames a relation and/or its attributes ρ S(B1, B2,, Bn) R or ρ S R or ρ (B1, B2,, Bn) R Retrieve all course numbers contained in Course and name the resulting relation Lecture. ρ Lecture π crs_no Course Course crs_no title 100 Intro. to being a Superhero 101 Secret Identities How to take over the world Lecture course EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 21

22 6.2 Relational Algebra Renaming operator ρ Select all result of course 100 from exam; the resulting relation should be called result and contain the attributes mat_no, course, and grade. ρ results(mat_no, course, grade) σ course=100 exam σ course=100 exam student course result results mat_no course grade EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 22

23 6.2 Relational Algebra Union, intersection, and set difference operators work as in set theory but: operands have to be union-compatible (i.e. they must consist of the same attributes) We define same as having the same name & same domain written as R S, R S, and R S, respectively σ crs_no=100 Course crs_no title σ crs_no=100 Course σ crs_no=102 Course 100 Intro. to being a Superhero crs_no title σ crs_no=102 Course crs_no title 100 Intro. to being a Superhero 102 How to take over the world 102 How to take over the world EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 23

24 6.2 Relational Algebra Cartesian product written as R S also called cross product creates a new relation by combining each tuple of the first relation with every tuple of the second relation attribute set = all attributes of R plus all attributes of S the resulting relation contains exactly R S tuples EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 24

25 6.2 Relational Algebra Student exam mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m Student exam mat_no firstname lastname sex student course result 1005 Clark Kent m Clark Kent m Clark Kent m Louise Lane f Louise Lane f student course result EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 25

26 6.2 Relational Algebra π lastname, title, result σ mat_no=student course=crs_no (Student exam Course) lastname title result Kent Secret Identities Kent Intro to being a Superhero 1.3 Xavier Secret Identities This type of expression is very important! Cartesian product, followed by selections/projections selections/projections involve different source relations this kind of query is called a join EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 26

27 6.2 Relational Algebra Theta join θ (θ is a boolean condition) sometimes also called inner join or just join creates a new relation by combining related tuples from different source relations written as R condition S or σ condition (R S) Joins can be used to join related relations i.e., related in an ER model, and now linked via foreign keys π lastname, title, result (Student mat_no=student exam course=crs_no Course) = π lastname, title, result σ mat_no=student course=crs_no (Student exam Course ) EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 27

28 6.2 Relational Algebra Equi-join condition joins two relations only using equality conditions R condition S condition may only contain equality statements between attributes (A 1 =A 2 ) a special case of the theta join Most used join operator π lastname, title, result (Student mat_no=student exam course=crs_no Course) = π lastname, title, result σ mat_no=student course=crs_no (Student exam Course) EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 28

29 6.2 Relational Algebra Natural join a special case of the equi-join R attributelist S implicit join condition each attribute in attributelist must be contained in both source relations for each of these attributes, an equality condition is created all these conditions are connected by logical AND If no attributes are explicitly stated, all attributes with equal names are implicitly used EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 29

30 6.2 Relational Algebra Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m Course crs_no title 100 Intro. to being a Superhero 101 Secret Identities How to take over the world exam student course result Student mat_no=student exam course=crs_no Course EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 30

31 6.2 Query Optimization Relational algebra usually allows for several equivalent evaluation plans respective execution costs may strongly differ e.g., in memory space, response time, etc. Idea: Find the best plan, before evaluating the query Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 31

32 6.2 Query Optimization Example Select all results better than 1.7, their student s name and course title. Student mat_no firstname lastname size 3519 Bilbo Baggins Samwise Gamgee Meriadoc Brandybuck Erna Broosh Frodo Baggins Peregrin Took Sméagol NULL 98 4 Byte 30 Byte 30 Byte 4 Byte Course crs_no title 41 Cooking rabbits 40 Destroying rings 42 Flying eagles exam 4 Byte student course result Byte 4 Byte 4 Byte 8 Byte Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 32

33 6.2 Query Optimization Example Select all results better than 1.7, their student s name and course title. π lastname, result, title σ result 1.3 course=crs_no mat_no=student (Course Student exam) Student mat_no firstname lastname size 3519 Bilbo Baggins Samwise Gamgee Meriadoc Brandybuck Erna Broosh Frodo Baggins Peregrin Took Sméagol NULL 98 Course crs_no title 41 Cooking rabbits 40 Destroying rings 42 Flying eagles exam 4 Byte student course result Byte 4 Byte 4 Byte 8 Byte Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 33

34 6.2 Query Optimization π lastname, result, title σ result 1.3 course = crs_no mat_no = student (Course Student exam) π lastname, result, title σ result 1.3 Create Canonical Operator Tree operator tree visualized the order of primitive functions note: Illustration is not really canonical tree as selection is already split in three parts Student σ course=crs_no σ student=matno Course exam Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 34

35 6.2 Query Optimization How much space is needed for the intermediate results? 2 * 68B = 136B 2 * 118B = 236B 6 * 118B = 708B 18 * 118B = 2,124B π lastname, result, title σ result 1.3 σ course=crs_no σ student=mat_no This seems like a lot of unnecessary work! cant we optimize this? => yes, we can! 126 * 118B = 14,868B 42 * 84B = 3,528B Course Student exam 3 * 34B= 102B 7 * 68B = 476B 6 * 16B = 96B Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 35

36 Optimized Operator Tree 2 * 42B = 84B 2 * 50B = 100B 7 * 34B = 238B 6.2 Query Optimization 2 * 68B = 136B 2 * 76B = 152B π lastname, mat_no Student 7 * 68B = 585B student=mat_no π lastname, result, title π lastname, result, course course=crs_no σ result 1.3 exam 6 * 16B = 96B Course 3 * 34B= 102B 2 * 16B = 32B π lastname, result, title σ result 1.3 course = crs_no mat_no = student (Course Student exam) = π lastname, result, title (Course course=crs_no π lastname, result, course (π lastname, mat_no Student mat_no=student σ result 1.3 exam)) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 36

37 6.2 Query Optimization Comparison of intermediate results 2 * 68B = 136B π lastname, result, title 2 * 118B = 236B σ result * 68B = 136B π lastname, result, title 6 * 118B = 708B 18 * 118B = 2,124B σ course=crs_no σ student=mat_no 126 * 118B = 14,868B 2 * 42B = 84B 2 * 50B = 100B 2 * 76B = 152B π lastname, result, course student=mat_no course=crs_no 3 * 34B= 102B Course 7 * 34B = 238B 2 * 16B = 32B π lastname, mat_no σ result * 84B = 3,528B Course Student exam 3 * 34B= 102B Student exam 7 * 68B = 585B 6 * 16B = 96B 7 * 65B = 476B 6 * 16B = 96B Intermediate result sets summed up sizes: B versus 606 B Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 37

38 Have a Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 38

39 Note on Query Optimization Query evaluation problem: Given relational database DB, query Q and output tuple t. Does t belong to the result of Q(DB)? PSPACE-complete Query inclusion: Given two queries Q 1 and Q 2 over relational schema R. Is Q 1 (DB) included in Q 2 (DB) for every instance DB over R? undecidable Consequence: query equivalence is undecidable, too. Query optimization is an active field of research! Companies are interested in it, because the faster their product the more money they earn. AHV 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 39

40 6.2 Relational Algebra Left semi-join and right semi-join combination of a theta-join and projection R condition S = π (list of all attributes in R) (R condition S) R condition S = π (list of all attributes in S) (R condition S) creates a copy of R (or S) while removing all those tuples that do not have a join partner This is a filtering operation with a dictionary lookup Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 40

41 6.2 Relational Algebra Left semi-join: Student Student mat_no=student exam mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m exam student course result Student matnr=course exam mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f All hard-trying students (those who did exams). Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 41

42 6.2 Relational Algebra Division written as R S S contains only attributes that are also present in R division restricts R in terms of attributes and tuples result s attributes = those in R but not in S result s tuples = those in R such that every tuple in S is a join partner useful for queries like Find the sailors who have reserved all boats. more formally: let A R ={attributes of R}; A S ={attributes of S}; A S A R A = A R A S R S π A R π A ((π A (R) S) R) why the name division? because (R S) S = R. but: (R S) S = R is not true in general Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 42

43 6.2 Relational Algebra Division: SC = ρ SC (π mat_no, lastname, course (Student mat_no=student exam)) mat_no lastname course 1000 Kent Kent Lane Luther Luther Luther Xavier Xavier 100 Result contains all those students who took the same courses as Clark Kent (and possibly some more). CCK = ρ CCK (π course σ lastname= Clark Kent SC) course SC CCK mat_no 1000 Kent lastname 1002 Luther EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 43

44 6.2 Relational Algebra Alternative definition Read division as a for all statement Given relations R and S based on schemas R(A 1,, A n, B 1,, B m ) and S(B 1,, B m ) R S { t dom(a 1 ) dom(a n ) u S tu R } tu means plain concatenation of t and u e.g., t =< 1,2 >, u =< 3,4 >, tu =< 1,2,3,4 > Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 44

45 6.2 Relational Algebra Conflicting attribute names In the previous examples, all relations had different attribute names What to do, if not? Simple case: Use qualified attribute names: Student(matno, name) Thesis(id, student Student, name) π Student.name, Thesis.name (Student matno=student Thesis) General case: Use renaming: Person(id, name, supervisor Person) Person supervisor=sid (ρ Supervisor(sid, sname, ssup) Person) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 45

46 6.2 Relational Algebra Operator Precedence unary operators are applied first (σ, π, ρ, F) cross product and joins are applied afterwards (,, ) followed by union und set minus (, ) last step is intersection ( ) (exam course=crs_no (σ crs_no=101 Course)) = (exam course=crs_no (σ crs_no=100 Course)) exam course=crs_no σ crs_no=101 Course exam course=crs_no σ crs_no=100 Course π lastname, result Student mat_no=student exam projection is applied first! Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 46

47 6.2 Summary Basic relational algebra Selection σ Projection π Renaming ρ Union, set difference Cartesian product Intersection Extended relational algebra Theta-join θ Equi-join <=-cond> Natural join <attributelist> Left semi-join and right semi-join Division Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 47

48 6.2 Summary (cont d) SPC Selection σ Projection π Cartesian product SPJR Selection σ Projection π Natural join <attributelist> Renaming ρ Beyond conjunctive queries Union Set difference \ AHV 4 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 48

49 6 Relational Algebra Motivation Relational Algebra basic relational algebra operations Query Optimization additional derived operations Advanced relational algebra Outer Joins Aggregation Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 49

50 6.3 What does this do? Movie(id, name, year, type, remark) produced_in(movie Movie, country Country) Country(name, residents) π country, name ( Country name=country produced_in movie=id σ year=1893 type="cinema" Movie ) From which countries are the cinema movies of the year 1893 and what are their names? Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 50

51 6.3 What does this do? Person(id, name, sex) plays(person Person, movie Movie, role) Movie(id, title, year, type) π name ( ) σ title = "Star Wars" type="cinema" Movie Movie.id=movie σ role= Diplomat" plays person=person.id σ sex="female" Person Which female actors from Star Wars cinema movies played a diplomat? Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 51

52 6.3 What does this do? Person(id, name, sex) plays(person Person, movie Movie, role) Movie(id, title, year, type) has_genre(movie Movie, genre Genre) Genre(name, description) π name ( ) π id, name (Person id=person σ role="postman" plays ) \ π id, name (Person id=person plays plays.movie=has_genre.movie σ genre="western" has_genre ) Which actors have played a postman, but never participated in a Western? Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 52

53 6.3 Advanced Relational Algebra Advanced relational algebra Left outer join, Right outer join, Full outer join, Aggregation F these operations cannot be expressed with basic relational algebra Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 53

54 6.3 Advanced Relational Algebra Left outer join and right outer join Theta-joins and its specializations join exactly those tuples that satisfy the join condition tuples without a matching join partner are eliminated and will not appear in the result all information about non-matching tuples gets lost outer joins allow to keep all tuples of a relation padding with NULL values if there is no join partner Left outer join : Keeps all tuples of the left relation Right outer join : Keeps all tuples of the right relation Full outer join : Keeps all tuples of both relations EN 6 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 54

55 6.3 Advanced Relational Algebra Example: List students and their exam results π lastname, course, result (Student mat_no=student exam) Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m exam student course result π lastname, course, result (Student mat_no=student exam) lastname course result Kent Kent Lex Luther and Charles Xavier are lost because they didn t take any exams! Also, information on student 9876 disappears Lane Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 55

56 6.3 Advanced Relational Algebra Left outer join: List students and their exam results π lastname, course, result (Student mat_no=student exam) Student exam matnr firstname lastname sex student course result 1005 Clark Kent m Louise Lane f Lex Luther m Charles Xavier m π lastname, course, result (Student mat_no=student exam) lastname course result Kent Kent Lane Luther NULL NULL Xavier NULL NULL All student names with course and result if present. Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 56

57 6.3 Advanced Relational Algebra Right outer join: List exams and their participants. π lastname, course, result (Student mat_no=student exam) Student exam mat_no firstname lastname sex student course result 1005 Clark Kent m Louise Lane f Lex Luther m Charles Xavier m π lastname, course, result (Student mat_no=student exam) lastname course result Kent Kent Lane NULL All exam result with student names if known. Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 57

58 6.3 Advanced Relational Algebra Full outer join: Combination of left and right outer joins. π lastname, course, result (Student mat_no=student exam) Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m exam student course result π lastname, course, result (Student mat_no=student exam) lastname course result Kent Kent Lane Luther NULL NULL NULL Xavier NULL NULL All student names with course and result if present AND all exam result with student names if known. Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 58

59 6.3 Aggregation Aggregation operator: Typically used in simple statistical computations merges tuples into groups computes (simple) statistics for each group Examples Compute the average exam score. For each student, count the total number of exams he/she has taken so far. Find the highest exam score ever. Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 59

60 6.3 Aggregation Written as grouping attributes F function list R F is called script F Creates a new relation all tuples in R that have identical values with respect to all grouping attributes, are grouped together all functions in the function list are applied to each group for each group, a new tuple is created, which contains the result values of all the functions the schema of the resulting relation looks as follows: the grouping attributes and, for each function, one new attribute all other attributes of the old relation are lost Example: course F average(result) exam Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 60

61 6.3 Aggregation Attention Within groups, no duplicates are eliminated Some available functions Sum(attribute) sum of all non-null values of attribute Average(attribute) mean of all non-null values of attribute Maximum(attribute) maximum value of all non-null values of attribute Minimum(attribute) minimum value of all non-null values of attribute Count(attribute) number of tuples having a non-null values of attribute Count(*) number of tuples Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 61

62 6.3 Aggregation Example (without grouping) if there are no grouping attributes, the whole input relation is treated as one group ρ Without_Groups(sum, avg_result, min_result, max_result) ( F sum(student), average(result), min(result), max(result) exam) exam student course result Without_Groups sum avg_result min_result max_result Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 62

63 6.3 Aggregation Example (with grouping) For each course, count results and compute the average score. ρ With_Grouping (course, avg_result, #result) ( coursef average(result), count(result) exam) exam student course result With_Grouping course avg_result #result Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 63

64 6.3 Aggregation Example For each student, count the exams and compute average result. ρ Overview(mat_no, #exams, avg_result) ( ) mat_nof count(course), avg(result) π mat_no, course, result (Student mat_no=student exam) exam student course result Overview mat_no #exams avg_result NULL NULL Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 64

65 6.3 Advanced Relational Algebra Subqueries: Conditions used in select or join operators compare a tuple s attribute to another value or attribute Condition clauses attribute θ value attribute θ attribute θ {=, <,,, >, } Sometimes, we need a dynamic value Convention for advanced relational algebra: We can treat a relation with a single attribute and a single row as a value (we call that a subquery) Example: Select those tuples from exam which have the overall average result as a result σ result= Favg result examexam Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 65

66 6.4 Guidelines Writing complex Queries Use intermediate results: stud_ex = ρ stud_ex(stud, num_ex) ( student F count(course) exam) σ stud_ex.num_ex>5 stud_ex = σ stud_ex.num_ex>5 (ρ stud_ex(stud, num_ex) ( student F count(course) exam)) Use proper indentation... not something like: π name (Student mat_no (π mat_no σ #exams>5 avg_result >2.7 ρ Overview(mat_no, #exams, avg_result)( mat_no F count(course), avg(result) π mat_no, course, result (Student mat_no=student exam)))) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 66

67 6.4 Exercise Student mat_no firstname lastname sex 1005 Clark Kent m 2832 Louise Lane f 4512 Lex Luther m 5119 Charles Xavier m 6676 Erik Magnus m 8024 Jeanne Gray f 9876 Logan m Student(mat_no, firstname, lastname, sex) Course(crs_no, title) Exam(mat_no Student, course Course, result) Course crs_no title 100 Intro. to being a Superhero 101 Secret Identities How to take over the world 103 Mind Reading Exam mat_no course result Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 67

68 6.4 Exercise List the student(s) who took the most exams. (by matno and firstname) Decompose the query in multiple parts: How many exams did each student take? What is the maximum number of exams a student took? Who took as many exams as the maximum? Student(mat_no, firstname, lastname, sex) Course(crs_no, title) Exam(mat_no Student, course Course, result) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 68

69 6.4 Exercise How many exams did each student take? StudentExams = ρ StudentExams mat_no,firstname,exam_count mat_no,firstname F count course Student Student.mat_no=Exam.mat_no Exam StudentExams mat_no firstname exam_count 1005 Clark Louise Lex Charles 3 Student(mat_no, firstname, lastname, sex) Course(crs_no, title) Exam(mat_no Student, course Course, result) 6676 Erik Jeanne Logan 1 Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 69

70 6.4 Exercise What is the maximum number of exams a student took? Max_Exams = F max( exam _ count ) StudentExams List the student(s) who took the most exams. StudentExams = ρ StudentExams mat_no,firstname,exam_count mat_no,firstname F count course Student Student.mat_no=Exam.mat_no Exam Max_Exams = F max( exam _ count ) StudentExams max_exam_count π matno,firstname σ exam_count=max_exams StudentExams Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 70 3 mat_no 1005 Clark firstname 5119 Charles

71 6.4 Recommendation The University of Innsbruck build a relational algebra calculator It can be helpful to improve your relational algebra skills Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 71

72 6 Next Lecture Tuple relational calculus foundation of SQL Domain relational calculus foundation of Query-by-example (QBE) Relational Database Systems 1 Wolf-Tilo Balke Institut für Informationssysteme TU Braunschweig 72

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Jan-Christoph Kalo Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 6 Relational Algebra Motivation Relational Algebra

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Overview Motivation Relational Algebra Query Optimization

More information

Relational Database Systems 2 5. Query Processing

Relational Database Systems 2 5. Query Processing Relational Database Systems 2 5. Query Processing Silke Eckstein Andreas Kupfer Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de 5 Query Processing 5.1 Introduction:

More information

5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator

5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator. 5.3 Parser and Translator 5 Query Processing Relational Database Systems 2 5. Query Processing Silke Eckstein Andreas Kupfer Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de 5.1 Introduction:

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Joachim Selke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Exercise 4.1 The four major steps of conceptual schema

More information

Relational Database Systems 2 5. Query Processing

Relational Database Systems 2 5. Query Processing Relational Database Systems 2 5. Query Processing Silke Eckstein Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de 4 Trees & Advanced Indexes

More information

Relational Database Systems 1. Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig

Relational Database Systems 1. Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig Relational Database Systems 1 Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 7.0 Summary Basic relational algebra Selection σ Projection

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Stephan Mennicke Jan-Christoph Kalo Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 7.0 Introduction Beside the relational algebra,

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Silke Eckstein Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Overview Relational tuple calculus SQUARE & SEQUEL

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Philipp Wille Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 8.0 Overview of SQL SQL Queries SELECT Data Manipulation

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Jan-Christoph Kalo Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 8.0 Overview of SQL SQL Queries SELECT Data Manipulation

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Overview SQL Queries SELECT Data manipulation language

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Overview Basic set theory Relational data model

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

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

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Overview Advanced database concepts for application

More information

Relational Database Systems 2 6. Query Optimization

Relational Database Systems 2 6. Query Optimization Relational Database Systems 2 6. Query Optimization Silke Eckstein Andreas Kupfer Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de 6 Query Optimization 6.1

More information

Relational Database Systems 2 6. Query Optimization

Relational Database Systems 2 6. Query Optimization Relational Database Systems 2 6. Query Optimization Silke Eckstein Benjamin Köhncke Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de 5 Query Processing The

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

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

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

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

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 Database Systems 1. Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig

Relational Database Systems 1. Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig Relational Database Systems 1 Christoph Lofi Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 10 Summary Design quality of database tables can be

More information

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall Relational Algebra Study Chapter 4.1-4.2 Comp 521 Files and Databases Fall 2010 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model

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

Relational Algebra for sets Introduction to relational algebra for bags

Relational Algebra for sets Introduction to relational algebra for bags Relational Algebra for sets Introduction to relational algebra for bags Thursday, September 27, 2012 1 1 Terminology for Relational Databases Slide repeated from Lecture 1... Account Number Owner Balance

More information

Relational Query Languages

Relational Query Languages 1 ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION JING YANG 2010 FALL Class 9: The Relational Algebra and Relational Calculus Relational Query Languages 2 Query languages: Allow manipulation and retrieval

More information

Relational Algebra. Relational Query Languages

Relational Algebra. Relational Query Languages Relational Algebra π CS 186 Fall 2002, Lecture 7 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,

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

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

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

Introduction Relational Algebra Operations

Introduction Relational Algebra Operations CMPT 354 Introduction Relational Algebra Operations Projection and Selection Set Operations Joins Division Tuple Relational Calculus John Edgar 2 Query languages allow the manipulation and retrieval of

More information

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1 Relational Algebra [R&G] Chapter 4, Part A CS4320 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports simple, powerful QLs:

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Hermann Kroll, Janus Wawrzinek, Stephan Mennicke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de Common Mistakes

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

Databases 1. Daniel POP

Databases 1. Daniel POP Databases 1 Daniel POP Week 4 Agenda The Relational Model 1. Origins and history 2. Key concepts 3. Relational integrity 4. Relational algebra 5. 12+1 Codd rules for a relational DBMSes 7. SQL implementation

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

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

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

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

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations 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

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus Chapter 6 The Relational Algebra and Calculus Chapter Outline Example Database Application (COMPANY) Relational Algebra Unary Relational Operations Relational Algebra Operations From Set Theory Binary

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

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview

Relational Algebra. Relational Algebra Overview. Relational Algebra Overview. Unary Relational Operations 8/19/2014. Relational Algebra Overview The Relational Algebra Relational Algebra Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) Relational

More information

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune Relational Algebra Mr. Prasad Sawant MACS College Pune MACS College Relational Algebra Tuple - a collection of attributes which describe some real world entity. Attribute - a real world role played by

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

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA Chapter 6: Relational Data Model and Relational Algebra 1 Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA RELATIONAL MODEL CONCEPTS The relational model represents the database as a collection

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

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

CIS 330: Applied Database Systems. ER to Relational Relational Algebra CIS 330: Applied Database Systems ER to Relational Relational Algebra 1 Logical DB Design: ER to Relational Entity sets to tables: ssn name Employees lot CREATE TABLE Employees (ssn CHAR(11), name CHAR(20),

More information

1 Relational Data Model

1 Relational Data Model Prof. Dr.-Ing. Wolfgang Lehner INTELLIGENT DATABASE GROUP 1 Relational Data Model What is in the Lecture? 1. Database Usage Query Programming Design 2 Relational Model 3 The Relational Model The Relation

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

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

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems Chapter 4 Relational Algebra Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Formal Relational Query Languages Two mathematical Query Languages form the basis

More information

The Relational Algebra

The Relational Algebra The Relational Algebra Relational Algebra Relational algebra is the basic set of operations for the relational model These operations enable a user to specify basic retrieval requests (or queries) 27-Jan-14

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

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

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1 COSC344 Database Theory and Applications σ a= c (P) S π A, C (H) P P X Q Lecture 4 Relational algebra COSC344 Lecture 4 1 Overview Last Lecture Relational Model This Lecture ER to Relational mapping Relational

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

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

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

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 Algebra and Calculus

Relational Algebra and Calculus and Calculus Marek Rychly mrychly@strathmore.edu Strathmore University, @ilabafrica & Brno University of Technology, Faculty of Information Technology Advanced Databases and Enterprise Systems 7 September

More information

Relational Database Systems 1 Wolf-Tilo Balke Hermann Kroll, Janus Wawrzinek, Stephan Mennicke

Relational Database Systems 1 Wolf-Tilo Balke Hermann Kroll, Janus Wawrzinek, Stephan Mennicke Relational Database Systems 1 Wolf-Tilo Balke Hermann Kroll, Janus Wawrzinek, Stephan Mennicke Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 10.0 Introduction

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

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Relational Algebra Chapter 4, Part A Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database.

More information

Chapter 8: The Relational Algebra and The Relational Calculus

Chapter 8: The Relational Algebra and The Relational Calculus Ramez Elmasri, Shamkant B. Navathe(2017) Fundamentals of Database Systems (7th Edition),pearson, isbn 10: 0-13-397077-9;isbn-13:978-0-13-397077-7. Chapter 8: The Relational Algebra and The Relational Calculus

More information

Relational Algebra 1 / 38

Relational Algebra 1 / 38 Relational Algebra 1 / 38 Relational Algebra Relational model specifies stuctures and constraints, relational algebra provides retrieval operations Formal foundation for relational model operations Basis

More information

CSCC43H: Introduction to Databases. Lecture 3

CSCC43H: Introduction to Databases. Lecture 3 CSCC43H: Introduction to Databases Lecture 3 Wael Aboulsaadat Acknowledgment: these slides are partially based on Prof. Garcia-Molina & Prof. Ullman slides accompanying the course s textbook. CSCC43: Introduction

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

Basic operators: selection, projection, cross product, union, difference,

Basic operators: selection, projection, cross product, union, difference, CS145 Lecture Notes #6 Relational Algebra Steps in Building and Using a Database 1. Design schema 2. Create schema in DBMS 3. Load initial data 4. Repeat: execute queries and updates on the database Database

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

Relational Algebra 1. Week 4

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

More information

Chapter 2: Intro to Relational Model

Chapter 2: Intro to Relational Model 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) tuples (or rows) 2.2 Attribute Types The

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

THE RELATIONAL DATABASE MODEL

THE RELATIONAL DATABASE MODEL THE RELATIONAL DATABASE MODEL Introduction to relational DB Basic Objects of relational model Properties of relation Representation of ER model to relation Keys Relational Integrity Rules Functional Dependencies

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

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

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan RELATIONAL ALGEBRA CS 564- Fall 2016 ACKs: Dan Suciu, Jignesh Patel, AnHai Doan RELATIONAL QUERY LANGUAGES allow the manipulation and retrieval of data from a database two types of query languages: Declarative:

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

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 Applications (15-415)

Database Applications (15-415) Database Applications (15-415) Relational Algebra Lecture 5, January 24, 2016 Mohammad Hammoud Today Last Session: The relational model Today s Session: Relational algebra Relational query languages (in

More information

Relational Database Systems 1

Relational Database Systems 1 Relational Database Systems 1 Wolf-Tilo Balke Simon Barthel Institut für Informationssysteme Technische Universität Braunschweig www.ifis.cs.tu-bs.de 10.0 Introduction Up to now, we have learned......

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra Relational algebra Iztok Savnik, FAMNIT Slides & Textbook Textbook: Raghu Ramakrishnan, Johannes Gehrke, Database Management Systems, McGraw-Hill, 3 rd ed., 2007. Slides: From Cow Book : R.Ramakrishnan,

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

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 Management System. Relational Algebra and operations

Database Management System. Relational Algebra and operations Database Management System Relational Algebra and operations Basic operations: Selection ( ) Selects a subset of rows from relation. Projection ( ) Deletes unwanted columns from relation. Cross-product

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

Oracle Database 10g: Introduction to SQL

Oracle Database 10g: Introduction to SQL ORACLE UNIVERSITY CONTACT US: 00 9714 390 9000 Oracle Database 10g: Introduction to SQL Duration: 5 Days What you will learn This course offers students an introduction to Oracle Database 10g database

More information

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

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU Relational Algebra 1 Structure of Relational Databases A relational database consists of a collection of tables, each of which is assigned a unique name. A row in a table represents a relationship among

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

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

MIS Database Systems Relational Algebra

MIS Database Systems Relational Algebra MIS 335 - Database Systems Relational Algebra http://www.mis.boun.edu.tr/durahim/ Ahmet Onur Durahim Learning Objectives Basics of Query Languages Relational Algebra Selection Projection Union, Intersection,

More information