1 Relational Algebra, Principles and Part I Hugh Darwen CS252.HACD: Fundamentals of Relational Databases.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Relational Algebra Part II
(Fuzzy Set Operations)
Tuples Given a collection of types Ti (i=1,2,….n), a tuple value on those types is a set of ordered triples of the form
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Fourth normal form: 4NF 1. 2 Normal forms desirable forms for relations in DB design eliminate redundancies avoid update anomalies enforce integrity constraints.
Relational data objects 1 Lecture 6. Relational data objects 2 Answer to last lectures activity.
5NF and other normal forms
Relational operators 1 Lecture 7 Relational Operators.
Relational data integrity
1 Relational Algebra Hugh Darwen (invited lecturer) CS319: Relational Algebra (revisited, reviewed, revised,
1 Introduction to Relational Databases Hugh Darwen CS252.HACD: Fundamentals of Relational Databases.
1 Constraints and Updating Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section 7: Constraints.
1 Database Design Issues, Part I Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section.
CS 319: Theory of Databases
1 Relational Algebra, Part III, and Other Operators Hugh Darwen CS252.HACD: Fundamentals of Relational.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: C3.
1 Values, Types, Variables, Operators Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section.
Dr. A.I. Cristea CS 319: Theory of Databases: FDs.
1 Predicates and Propositions Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section 3:
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
Dr. Alexandra I. Cristea CS 319: Theory of Databases: C6.
ZMQS ZMQS
Reducing Order Enforcement Cost in Complex Query Plans Ravindra Guravannavar and S. Sudarshan (To appear in ICDE 2007)
ABC Technology Project
Probability for a First-Order Language Ken Presting University of North Carolina at Chapel Hill.
Advance Mathematics Section 3.5 Objectives:
Solving Equations How to Solve Them
Relational Algebra Chapter 4, Part A
The Properties of Mathematics
Chapter 5 Test Review Sections 5-1 through 5-4.
More Two-Step Equations
Addition 1’s to 20.
25 seconds left…...
Copyright © Cengage Learning. All rights reserved.
Week 1.
We will resume in: 25 Minutes.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Relational Algebra and Relational Calculus
Copyright © Cengage Learning. All rights reserved.
Lecture 15 Functions CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
1 Unit 1 Kinematics Chapter 1 Day
ANHAI DOAN ALON HALEVY ZACHARY IVES CHAPTER 14: DATA PROVENANCE PRINCIPLES OF DATA INTEGRATION.
The Relational Algebra
Copyright © C. J. Date 2005page 1 AGENDA : 1. A few preliminaries 2. Laying the foundations 3.Building on the foundations.
Equivalence Relations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
1 Database Design Issues, Part II Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
COMP3017 Advanced Databases
Introduction to Relational Databases
Chapter 2: Intro to Relational Model
Presentation transcript:

1 Relational Algebra, Principles and Part I Hugh Darwen CS252.HACD: Fundamentals of Relational Databases Section 4: Relational Algebra, Principles and Part I

2 Anatomy of a Relation StudentIdNameCourseId S1AnneC1 attribute nameattribute values n-tuple, or tuple. This is a 3-tuple. The tuples constitute the body of the relation. The number of tuples in the body is the cardinality of the relation. Heading (a set of attributes) The degree of this heading is 3, which is also the degree of the relation.

3 ENROLMENT Example StudentIdNameCourseId S1AnneC1 S1AnneC2 S2BorisC1 S3CindyC3 S4DevinderC1 Predicate: StudentId is called Name and is enrolled on CourseId Note redundancy: S1 is always called Anne! ENROLMENT (a relation variable, or relvar)

4 Splitting ENROLMENT StudentIdName S1Anne S2Boris S3Cindy S4Devinder S5Boris StudentIdCourseId S1C1 S1C2 S2C1 S3C3 S4C1 IS_CALLEDIS_ENROLLED_ON Student StudentId is called Name Student StudentId is enrolled on course CourseId

5 Relations and Predicates (1) Consider the predicate: StudentId is called Name … is called --- is the intension (meaning) of the predicate. The parameter names are arbitrary. S is called N means the same thing (has the same intension). The extension of the predicate is the set of true propositions that are instantiations of it: { S1 is called Anne, S2 is called Boris, S3 is called Cindy, S4 is called Devinder, S5 is called Boris } Each tuple in the body (extension) of the relation provides the values to substitute for the parameters in one such instantiation.

6 Relations and Predicates (2) Moreover, each proposition in the extension has exactly one corresponding tuple in the relation. This 1:1 correspondence reflects the Closed-World Assumption: The Closed-World Assumption underpins the operators we are about to meet. A tuple representing a true instantiation is in the relation. A tuple representing a false one is out.

7 Relational Algebra Operators that operate on relations and return relations. In other words, operators that are closed over relations. Just as arithmetic operators are closed over numbers. Closure means that every invocation can be an operand, allowing expressions of arbitrary complexity to be written. Just as, in arithmetic, e.g., the invocation b-c is an operand of a+(b-c). The operators of the relational algebra are relational counterparts of logical operators: AND, OR, NOT, EXISTS. Each, when invoked, yields a relation, which can be interpreted as the extension of some predicate.

8 Logical Operators Because relations are used to represent predicates, it makes sense for relational operators to be counterparts of operators on predicates. We will meet examples such as these: Student StudentId is called Name AND StudentId is enrolled on course CourseId. Student StudentId is enrolled on some course. Student StudentId is enrolled on course CourseId AND StudentId is NOT called Devinder. Student StudentId is NOT enrolled on any course OR StudentId is called Boris.

9 Meet The Operators ANDJOIN restriction (WHERE) extension SUMMARIZE and some more EXISTSprojection ORUNION (AND) NOT(semi)difference RENAME LogicRelational counterpart

10 JOIN (= AND) StudentId is called Name AND StudentId is enrolled on CourseId. IS_CALLEDJOINIS_ENROLLED_ON NameStudentId AnneS1 BorisS2 CindyS3 DevinderS4 BorisS5 StudentIdCourseId S1C1 S1C2 S2C1 S3C3 S4C1

11 IS_CALLED JOIN IS_ENROLLED_ON StudentIdNameCourseId S1AnneC1 S1AnneC2 S2BorisC1 S3CindyC3 S4DevinderC1 Seen this before? Yes, this is our original ENROLMENT. The JOIN has reversed the split. (And has lost the second Boris.)

12 Definition of JOIN Let s = r1 JOIN r2. Then: The heading Hs of s is the union of the headings of r1 and r2. The body of s consists of those tuples having heading Hs that can be formed by taking the union of t1 and t2, where t1 is a tuple of r1 and t2 is a tuple of r2. If c is a common attribute, then it must have the same declared type in both r1 and r2. (I.e., if it doesnt, then r1 JOIN r2 is undefined.) Note: JOIN, like AND, is both commutative and associative.

13 RENAME Sid1Name S1Anne S2Boris S3Cindy S4Devinder S5Boris StudentIdName S1Anne S2Boris S3Cindy S4Devinder S5Boris Sid1 is called Name IS_CALLED RENAME ( StudentId AS Sid1 )

14 Definition of RENAME Let s = r RENAME ( A1 AS B1, … An AS Bn ) The heading of s is the heading of r except that attribute A1 is renamed to B1 and so on. The body of s consists of the tuples of r except that in each tuple attribute A1 is renamed to B1 and so on.

15 RENAME and JOIN Sid1 is called Name AND so is Sid2 IS_CALLED RENAME (StudentId AS Sid1 ) JOIN IS_CALLED RENAME (StudentId AS Sid2 ) Sid1NameSid2 S1AnneS1 S2BorisS2 BorisS5 BorisS2 S3CindyS3 S4DevinderS4 S5BorisS5

16 Special Cases of JOIN What is the result of R JOIN R? What if all attributes are common to both operands? What if no attributes are common to both operands? R It is called intersection. It is called Cartesian product

17 Interesting Properties of JOIN It is commutative: r1 JOIN r2 r2 JOIN r1 It is associative: (r1 JOIN r2) JOIN r3 r1 JOIN (r2 JOIN r3) So Tutorial D allows JOIN{r1, r2, …} (note the braces) Of course it is no coincidence that logical AND is also both commutative and associative. We note in passing that these properties are important for optimisation (in particular, of query evaluation).

18 Projection (= EXISTS) Student StudentId is enrolled on some course. StudentIdCourseId S1C1 S1C2 S2C1 S3C3 S4C1 Given: StudentId S1 S2 S3 S4 To obtain: IS_ENROLLED_ON { StudentId } = IS_ENROLLED_ON { ALL BUT CourseId }

19 Definition of Projection Let s = r { A1, … An } ( = r { ALL BUT B1, … Bm } ) The heading of s is the subset of the heading of r given by { A1, … An }. The body of s consists of each tuple that can be formed from a tuple of r by removing from it the attributes named B1, … Bm. Note that the cardinality of s can be less than that of r but cannot be more than that of r.

20 How ENROLMENT Was Split VAR IS_CALLED BASE SAME_TYPE_AS (ENROLMENT { StudentId, Name }) KEY { StudentId }; IS_CALLED := ENROLMENT { StudentId, Name }; VAR IS_ENROLLED_ON BASE SAME_TYPE_AS (ENROLMENT { ALL BUT Name }) KEY { StudentId, CourseId } ; IS_ENROLLED_ON := ENROLMENT { ALL BUT Name }; Can be done even more economicallysee the Notes!

21 Special Cases of Projection What is the result of R { ALL BUT }? What is the result of R { }? R A relation with no attributes at all, of course! There are two such relations, of cardinality 1 and 0. The pet names TABLE_DEE and TABLE_DUM have been advanced for these two, respectively.

22 Another Special Case of JOIN What is the result of R JOIN TABLE_DEE ? R So TABLE_DEE is the identity under JOIN (cf. 0 under addition and 1 under multiplication.)