Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell.

Slides:



Advertisements
Similar presentations
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Advertisements

พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.
Lecture 07: Relational Algebra
Relational Algebra Dashiell Fryer. What is Relational Algebra? Relational algebra is a procedural query language. Relational algebra is a procedural query.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
D ATABASE S YSTEMS I A DMIN S TUFF. 2 Mid-term exam Tuesday, Oct 2:30pm Room 3005 (usual room) Closed book No cheating, blah blah No class on Oct.
Oct 28, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Nov 18, 2003Murali Mani Relational Algebra B term 2004: lecture 10, 11.
Murali Mani Relational Algebra. Murali Mani What is Relational Algebra? Defines operations (data retrieval) for relational model SQL’s DML (Data Manipulation.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Chapter 3 Section 3.4 Relational Database Operators
Relational Algebra Instructor: Mohamed Eltabakh 1.
Lecture 2 of Advanced Databases Advanced SQL Instructor: Mr.Ahmed Al Astal.
Relational Model Concepts. The relational model represents the database as a collection of relations. Each relation resembles a table of values. A table.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Relational Algebra - Chapter (7th ed )
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
RELATIONAL ALGEBRA CHAPTER 6 1. LECTURE OUTLINE  Unary Relational Operations: SELECT and PROJECT  Relational Algebra Operations from Set Theory  Binary.
The Relational Algebra and Calculus
October 9, Announcements Exam Tuesday in class –Closed book Program 2 due Friday –Turn in using D2L dropbox –Only turn in the 4 files specified.
Dr Gordon Russell, Napier University Data Analysis 3 - V2.0 1 Data Analysis 3 Unit 2.3.
Data Analysis 3 Chapter 2.3 V3.0 Napier University Dr Gordon Russell.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Database Systems (Atzeni, Ceri, Paraboschi, Torlone) Chapter 3 : Relational algebra and calculus McGraw-Hill and Atzeni, Ceri, Paraboschi, Torlone 1999.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 6- 1.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 6 The Relational Algebra.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Part a The Relational Algebra and Relational Calculus Hours 1,2.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Al-Maarefa College for Science and Technology INFO 232: Database systems Chapter 3 “part 2” The Relational Algebra and Calculus Instructor Ms. Arwa Binsaleh.
Advanced Relational Algebra & SQL (Part1 )
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra p BIT DBMS II.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Dr Gordon Russell, Napier University Unit SQL 1 1 SQL 1 Unit 1.2.
IST 210 The Relational Language Todd S. Bacastow January 2005.
Dr Gordon Russell, Napier University Unit SQL 3 - V2.0 1 SQL 3 Unit 1.4.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Query Processing – Implementing Set Operations and Joins Chap. 19.
1 Relational Algebra and SQL. 2 Relational Query Languages Languages for describing queries on a relational database Relational AlgebraRelational Algebra.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 6 th Edition Chapter 8: Relational Algebra.
Chapter # 6 The Relational Algebra and Calculus
Fundamental of Database Systems
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Relational Algebra : #I
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Lecture 16 : The Relational Data Model
Chapter 2: Intro to Relational Model
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
Lecture 16 : The Relational Data Model
Unit Relational Algebra 1
Unit Relational Algebra 2
Data Analysis 3 Unit 2.3 Dr Gordon Russell, Napier University
Presentation transcript:

Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell

Relational Algebra - Example Consider the following SQL to find which departments have had employees on the `Further Accounting ‘ course. SELECT DISTINCT dname FROM department, course, empcourse, employee WHERE cname = `Further Accounting' AND course.courseno = empcourse.courseno AND empcourse.empno = employee.empno AND employee.depno = department.depno; The equivalent relational algebra is: PROJECT dname (department JOIN depno = depno ( PROJECT depno (employee JOIN empno = empno ( PROJECT empno (empcourse JOIN courseno = courseno ( PROJECT courseno (SELECT cname = `Further Accounting' course ) )) )) ))

Symbolic Notation From the example, one can see that for complicated cases a large amount of the answer is formed from operator names, such as PROJECT and JOIN. It is therefore commonplace to use symbolic notation to represent the operators. SELECT ->σ (sigma) PROJECT ->  (pi) PRODUCT ->  (times) JOIN -> |x| (bow-tie) UNION ->  (cup) INTERSECTION ->  (cap) DIFFERENCE -> - (minus) RENAME ->  (rho)

Usage The symbolic operators are used as with the verbal ones. So, to find all employees in department 1: SELECT depno = 1 (employee) becomes: σ depno = 1 (employee) Conditions can be combined together using ^ (AND) and v (OR). For example, all employees in department 1 called `Smith': SELECT depno = 1 ^ surname = `Smith ‘ (employee) becomes: σ depno = 1 ^ surname = `Smith ‘ (employee)

Usage Cont … The use of the symbolic notation can lend itself to brevity. Even better, when the JOIN is a natural join, the JOIN condition may be omitted from |x|. The earlier example resulted in: PROJECT dname (department JOIN depno = depno ( PROJECT depno (employee JOIN empno = empno ( PROJECT empno (empcourse JOIN courseno = courseno ( PROJECT courseno (SELECT cname = `Further Accounting' course))))))) becomes  dname (department |x| (  depno (employee |x| (  empno (empcourse |x| (  courseno (  cname = `Further Accounting' course) ))))))

Rename Operator The rename operator returns an existing relation under a new name.  A(B) is the relation B with its name changed to A. For example, find the employees in the same Department as employee 3.  emp2.surname,emp2.forenames (  employee.empno = 3 ^ employee.depno = emp2.depno ( employee  (  emp2 employee) )

Derivable Operators Fundamental operators: , , , , -,  Derivable operators: |x|,  A  B  A -(A – B)

Deriving |x| from , , and  A|x| c B  a1,a2,...aN (  c (A  B)) –where c is the join condition (eg A.a1 = B.a1), –and a1,a2,...aN are all the attributes of A and B without repetition. c is called the join-condition, and is usually the comparison of primary and foreign key. Where there are N tables, there are usually N-1 join- conditions. In the case of a natural join, the conditions can be missed out, but otherwise missing out conditions results in a cartesian product (a common mistake to make).

Equivalences The same relational algebraic expression can be written in many different ways. The order in which tuples appear in relations is never significant. A  B B  A A  B B  A A  B B  A (A - B) is not the same as (B - A)  c1 (  c2 (A))  c2 (  c1 (A))  c1 ^ c2 (A)  a1 (A)  a1 (  a1,etc (A)), where etc is any attributes of A....

While equivalent expressions always give the same result, some may be much easier to evaluate that others. When any query is submitted to the DBMS, its query optimiser tries to find the most efficient equivalent expression before evaluating it.

Comparing RA and SQL Relational algebra: –is closed (the result of every expression is a relation) –has a rigorous foundation –has simple semantics –is used for reasoning, query optimisation, etc. SQL: –is a superset of relational algebra –has convenient formatting features, etc. –provides aggregate functions –has complicated semantics –is an end-user language.

Comparing RA and SQL Any relational language as powerful as relational algebra is called relationally complete. A relationally complete language can perform all basic, meaningful operations on relations. Since SQL is a superset of relational algebra, it is also relationally complete.