RELATIONAL ALGEBRA CHAPTER 6

Similar documents
The Relational Algebra

Relational Databases

Lecture 4: More SQL and Relational Algebra

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Introduction to database design

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1


Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Relational Algebra. Basic Operations Algebra of Bags

Oracle Database 10g: Introduction to SQL

Exercise 1: Relational Model

CS 338 Join, Aggregate and Group SQL Queries

3. Relational Model and Relational Algebra

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Relational Algebra and SQL

Schema Mappings and Data Exchange

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

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Introduction to SQL: Data Retrieving

SQL Query Evaluation. Winter Lecture 23

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Chapter 13: Query Processing. Basic Steps in Query Processing

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Relational Database: Additional Operations on Relations; SQL

SQL Database queries and their equivalence to predicate calculus

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Information Systems SQL. Nikolaj Popov

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

CS143 Notes: Normalization Theory

Data Integration and Exchange. L. Libkin 1 Data Integration and Exchange

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Relational Algebra and SQL Query Visualisation

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Comp 5311 Database Management Systems. 16. Review 2 (Physical Level)

Introduction to Querying & Reporting with SQL Server

Chapter 13: Query Optimization

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

VIEWS virtual relation data duplication consistency problems

Introduction to SQL ( )

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

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Access Path Selection in a Relational Database Management System

CIS 631 Database Management Systems Sample Final Exam

SQL Nested & Complex Queries. CS 377: Database Systems

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

SQL: Queries, Programming, Triggers

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Relational Foundations for Functorial Data Migration

SQL: Queries, Programming, Triggers

Introduction to tuple calculus Tore Risch

CSC 443 Fall 2011 Dr. R. M. Siegfried. Answers to Assignment #1

1 Structured Query Language: Again. 2 Joining Tables

Schema Refinement, Functional Dependencies, Normalization

Chapter 5. SQL: Queries, Constraints, Triggers

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Query optimization. DBMS Architecture. Query optimizer. Query optimizer.

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

Database Programming with PL/SQL: Learning Objectives

Query Processing C H A P T E R12. Practice Exercises

SQL (structured query language)

Effective Use of SQL in SAS Programming

Evaluation of Expressions

Programming with SQL

CSC 443 Data Base Management Systems. Basic SQL

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

Summary on Chapter 4 Basic SQL

The Entity-Relationship Model

Oracle Database 11g: SQL Tuning Workshop

Database Fundamentals: 1

Chapter 14: Query Optimization

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

The process of database development. Logical model: relational DBMS. Relation

Advance DBMS. Structured Query Language (SQL)

Database Theory, Fall Semester MMXV: Week #3

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Relational Calculus. Module 3, Lecture 2. Database Management Systems, R. Ramakrishnan 1


Instant SQL Programming

Files. Files. Files. Files. Files. File Organisation. What s it all about? What s in a file?

Introduction to Microsoft Jet SQL

Translating SQL into the Relational Algebra

Chapter 8 The Enhanced Entity- Relationship (EER) Model

Automata and Formal Languages

Oracle 10g PL/SQL Training

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Oracle Database: SQL and PL/SQL Fundamentals

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

6 Creating the Animation

Data Analysis 1. SET08104 Database Systems. Napier University

C H A P T E R 1 Introducing Data Relationships, Techniques for Data Manipulation, and Access Methods

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

SQL Server. 1. What is RDBMS?

Structured Query Language (SQL)

Transcription:

1 RELATIONAL ALGEBRA CHAPTER 6

2 LECTURE OUTLINE Unary Relational Operations: SELECT and PROJECT Relational Algebra Operations from Set Theory Binary Relational Operations: JOIN and DIVISION Query Trees

3 THE RELATIONAL ALGEBRA Relational algebra Basic set of operations for the relational model Similar to algebra that operates on numbers Operands and results are relations instead of numbers Relational algebra expression Composition of relational algebra operations Possible because of closure property Model for SQL Explain semantics formally Basis for implementations Fundamental to query optimization

4 SELECT OPERATOR Unary operator (one relation as operand) Returns subset of the tuples from a relation that satisfies a selection condition: σ <selection condition> R where <selection condition> may have Boolean conditions AND, OR, and NOT has clauses of the form: <attribute name> <comparison op> <constant value> or <attribute name> <comparison op> <attribute name> Applied independently to each individual tuple t in operand Tuple selected iff condition evaluates to TRUE Example: σ Dno=4 AND Salary>2500 OR (Dno=5 AND Salary>30000) EMPLOYEE

5 SELECT OPERATOR (CONT D.) Do not confuse this with SQL s SELECT statement! Correspondence Relational algebra σ <selection condition> R SQL SELECT * FROM R WHERE <selection condition>

6 SELECT OPERATOR PROPERTIES Relational model is set-based (no duplicate tuples) Relation R has no duplicates, therefore selection cannot produce duplicates. Equivalences Selectivity σ C2 σ C1 (R) = σ C1 σ C2 (R) σ C2 σ C1 (R) = σ C1 AND C 2 (R) Fraction of tuples selected by a selection condition σ C (R) R

8 WHAT IS THE EQUIVALENT RELATIONAL ALGEBRA EXPRESSION? Employee ID Name S Dept JobType 12 Chen F CS Faculty 13 Wang M MATH Secretary 14 Lin F CS Technician 15 Liu M ECE Faculty SELECT * FROM Employee WHERE JobType = 'Faculty';

9 PROJECT OPERATOR Unary operator (one relation as operand) Keeps specified attributes and discards the others: Duplicate elimination π <attribute list> R Result of PROJECT operation is a set of distinct tuples Example: Correspondence π Fname,Lname,Address,Salary EMPLOYEE Relational algebra π <attribute list> R SQL SELECT DISTINCT <attribute list> FROM R Note the need for DISTINCT in SQL

10 PROJECT OPERATOR PROPERTIES π L R is defined only when L attr (R ) Equivalences π L2 π L1 (R) = π L2 (R) π L σ C (R) = σ C π L (R) as long as all attributes used by C are in L Degree Number of attributes in projected attribute list

11 WHAT IS THE EQUIVALENT RELATIONAL ALGEBRA EXPRESSION? Employee ID Name S Dept JobType 12 Chen F CS Faculty 13 Wang M MATH Secretary 14 Lin F CS Technician 15 Liu M ECE Faculty SELECT DISTINCT Name, S, Department FROM Employee WHERE JobType = 'Faculty';

12 WORKING WITH LONG EXPRESSIONS Sometimes easier to write expressions a piece at a time Incremental development Documentation of steps involved Consider in-line expression: π Fname,Lname,Salary σ Dno=5 (EMPLOYEE) Equivalent sequence of operations: DEP5_EMPS σ Dno=5 EMPLOYEE RESULT π Fname,Lname,Salary DEP5_EMPS

13 OPERATORS FROM SET THEORY Merge the elements of two sets in various ways Binary operators Relations must have the same types of tuples (union-compatible) UNION R S Includes all tuples that are either in R or in S or in both R and S Duplicate tuples eliminated INTERSECTION R S Includes all tuples that are in both R and S DIFFERENCE (or MINUS) R S Includes all tuples that are in R but not in S

14 CROSS PRODUCT OPERATOR Binary operator aka CARTESIAN PRODUCT or CROSS JOIN R S Attributes of result is union of attributes in operands deg(r S) = deg(r) + deg(s) Tuples in result are all combinations of tuples in operands R S = R * S Relations do not have to be union compatible Often followed by a selection that matches values of attributes Course TA Course TA dept cnum instructor term name major dept cnum instructor term name major CS 338 Jones Spring Ashley CS CS 338 Jones Spring Ashley CS CS 330 Smith Winter Lee STATS CS 330 Smith Winter Ashley CS STATS 330 Wong Winter STATS 330 Wong Winter Ashley CS CS 338 Jones Spring Lee STATS CS 330 Smith Winter Lee STATS STATS 330 Wong Winter Lee STATS What if both operands have an attribute with the same name?

15 RENAMING RELATIONS & ATTRIBUTES Unary RENAME operator Rename relation Rename attributes ρ S R ρ (B1,B2, Bn) R Rename relation and its attributes ρ S(B1,B2,,Bn) R Student name year Ashley 4 Lee 3 Dana 1 Jo 1 Jaden 2 Billie 3 Example: pairing upper year students with freshmen ρ Mentor(senior,class) σ year>2 Student σ year=1 Student

16 JOIN OPERATOR Binary operator R <join condition> S where join condition is a Boolean expression involving attributes from both operand relations Like cross product, combine tuples from two relations into single longer tuples, but only those that satisfy matching condition Formally, a combination of cross product and select R <join condition> S = σ <join condition> R S aka -join or inner join Join condition expressed as A B, where {=,,>,,<, } as opposed to outer joins, which will be explained later

17 JOIN OPERATOR (CONT D.) Examples: What are the names and salaries of all department managers? π Fname,Lname,Salary DEPARTMENT Mgr_ssn=Ssn EMPLOYEE Who can TA courses offered by their own department? Course TA Course dept=major TA dept cnum instructor term name major dept cnum instructor term name major CS 338 Jones Spring CS 330 Smith Winter Ashley Lee CS STATS CS 338 Jones Spring Ashley CS CS 330 Smith Winter Ashley CS STATS 330 Wong Winter STATS 330 Wong Winter Lee STATS Join selectivity Fraction of number tuples in result over maximum possible R C S R S Common case (as in examples above): equijoin

18 NATURAL JOIN R S No join condition Equijoin on attributes having identical names followed by projection to remove duplicate (superfluous) attributes Very common case Often attribute(s) in foreign keys have identical name(s) to the corresponding primary keys

19 NATURAL JOIN EXAMPLE Who has taken a course taught by Anderson? Acourses σ Instructor= Anderson SECTION π Name,Course_number,Semester,Year STUDENT GRADE_REPORT Acourses

22 DIVISION OPERATOR Binary operator R S Attributes of S must be a subset of the attributes of R attr(r S) = attr(r) attr(s) t tuple in (R S) iff (t S) is a subset of R Used to answer questions involving all e.g., Which employees work on all the critical projects? Works(enum,pnum) Critical(pnum) Works enum E35 E45 E35 E52 E52 E45 E35 pnum P10 P15 P12 P15 P17 P10 P15 Critical pnum P15 P10 Inverse of cross product Works Critical enum E45 E35 (Works Critical) Critical enum pnum E45 P15 E45 P10 E35 P15 E35 P10

23 REVIEW OF OPERATORS Select σ <selection condition> R Project π <attribute list> R Rename ρ <new schema> R Union R S Intersection R S Difference R S Cross product R S Join R <join condition> S Natural join R S Division R S

24 COMPLETE SET OF OPERATIONS Some operators can be expressed in terms of others e.g., R S = R S R S S R Set of relational algebra operations {σ, π,, ρ,, } is complete Other four relational algebra operation can be expressed as a sequence of operations from this set. 1. Intersection, as above 2. Join is cross product followed by select, as noted earlier 3. Natural join is rename followed by join followed by project 4. Division: R S = π Y R π Y π Y R S R where Y are attributes in R and not in S

25 NOTATION FOR QUERY TREES Representation for computation cf. arithmetic trees for arithmetic computations Leaf nodes are base relations Internal nodes are relational algebra operations

SAMPLE QUERIES 26

27 LECTURE SUMMARY Relational algebra Language for relational model of data Collection of unary and binary operators Retrieval queries only, no updates Notations Inline Sequence of assignments Operator tree