Relational Algebra. Relational Query Languages

Similar documents
Relational Query Languages. Relational Algebra and SQL. What is an Algebra? Select Operator. The Role of Relational Algebra in a DBMS

Relational Algebra and SQL. Relational Query Languages

Relational Query Languages. What is an Algebra? Roadmap. Relational Algebra. Chapter 5. The Role of Relational Algebra in a DBMS

CSE532 Relational Algebra and SQL

Chapter 5. Relational Algebra and SQL

The Relational Model Constraints and SQL DDL

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Chapter 5 Relational Algebra and SQL

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

CSE532 Theory of Database Systems The Relational a Data Model

Relational Query Languages: Relational Algebra. Juliana Freire

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

SQL - Basics. SQL Overview

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

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

Relational Query Languages

Chapter 8: Relational Algebra

Relational Algebra. Relational Query Languages

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

MIS Database Systems Relational Algebra

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

CMPUT 391 Database Management Systems. An Overview of Query Processing. Textbook: Chapter 11 (first edition: Chapter 14)

CMP-3440 Database Systems

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

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

Chapter 6 Part I The Relational Algebra and Calculus

2.2.2.Relational Database concept

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Relational Algebra and SQL. Basic Operations Algebra of Bags

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

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 Part I. CS 377: Database Systems

Relational Model, Relational Algebra, and SQL

Database Design. Goal: specification of database schema Methodology:

Chapter 6 The Relational Algebra and Relational Calculus

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

CS 377 Database Systems

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

Ian Kenny. November 28, 2017

Database Applications (15-415)

1 Relational Data Model

Database Management System. Relational Algebra and operations

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

Lecture 16. The Relational Model

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Representation in Relational Model.

CSC 261/461 Database Systems Lecture 13. Fall 2017

Introduction to Data Management CSE 344. Lectures 8: Relational Algebra

Relational Algebra. Algebra of Bags

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

Chapter 6 - Part II The Relational Algebra and Calculus

Chapter 2: Intro to Relational Model

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

Relational Algebra and SQL

Chapter 2: Intro to Relational Model

Chapter 6 The Relational Algebra and Calculus

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

Query Processing: The Basics. External Sorting

3. Relational Data Model 3.5 The Tuple Relational Calculus

Database Technology Introduction. Heiko Paulheim

Relational Algebra. Procedural language Six basic operators

Relational Algebra for sets Introduction to relational algebra for bags

Chapter 2 The relational Model of data. Relational algebra

Database Applications (15-415)

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Faloutsos - Pavlo CMU SCS /615

Overview. Carnegie Mellon Univ. School of Computer Science /615 - DB Applications. Concepts - reminder. History

What is an algebra? A formal system of manipulation of symbols to deal with general statements of relations.

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

download instant at The Relational Data Model

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

CSCC43H: Introduction to Databases. Lecture 3

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Relational Model and Relational Algebra

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

CS 582 Database Management Systems II

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

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Relational Databases

Relational Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

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

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Relational Algebra 1. Week 4

EECS 647: Introduction to Database Systems

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Chapter 5. Relational Algebra and Relational Calculus

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

Relational Query Languages

Relational Algebra 1

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra

Chapter 6 Formal Relational Query Languages

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

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

Modeling access. Relational query languages. Queries. Selection σ P (R) Relational model: Relational algebra

Chapter 3: Introduction to SQL

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Advanced Databases. Lecture 4 - Query Optimization. Masood Niazi Torshiz Islamic Azad university- Mashhad Branch

Transcription:

Relational Algebra Davood Rafiei 1 Relational Query Languages Languages for describing queries on a relational database Three variants Relational Algebra Relational Calculus SQL Query languages v.s. programming languages QLs not expected to be Turing complete. QLs support easy, efficient access to large data sets. 2 1

SQL & Relational Algebra Structured Query Language (SQL) Predominant application-level query language Declarative Relational Algebra Intermediate language used within DBMS Procedural 3 What is an Algebra? A language based on operators and a domain of values Operators map values taken from the domain into other domain values Hence, an expression involving operators and arguments produces a value in the domain Consider arithmetic operations +, - and * over integers. Algebra expressions: 2+3, (46 3)+3, (7*x)+(3*x) Relational algebra: Domain: the set of all relations Expression: referred to as a query 4 2

Operations on Tables Id Name Address Hobby 1123 John 123 Main stamps 1144 Joe 125 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps Id Name office 1144 Joe 333 Ath 5556 Mary 222 CSC Employee Person Operations on one table: pick some rows, pick some columns Operations on two tables: join, 5 Relational Algebra Domain: set of relations Basic operators: select, project, union, set difference, Cartesian product Derived operators: set intersection, division, join Procedural: Relational expression specifies query by describing an algorithm (the sequence in which operators are applied) for determining the result of an expression 6 3

The Role of Relational Algebra in a DBMS 7 Select Operator Produce table containing subset of rows of argument table satisfying condition σ condition (relation) Example: Person Id Name Address Hobby 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps σ Hobby= stamps (Person) Id Name Address Hobby 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps 8 4

Selection Condition Operators: <,,, >, =, Simple selection condition: <attribute> operator <constant> <attribute> operator <attribute> <condition> AND <condition> <condition> OR <condition> NOT <condition> 9 Selection Condition - Examples σ Id>3000 OR Hobby= hiking (Person) σ Id>3000 AND Id <3999 (Person) σ NOT(Hobby= hiking ) (Person) σ Hobby hiking (Person) 10 5

Project Operator Produces table containing subset of columns of argument table π attribute list (relation) Example: Person π Name,Hobby (Person) Id Name Address Hobby 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps Name Hobby John stamps John coins Mary hiking Bart stamps 11 Example: Person Project Operator π Name,Address (Person) Id Name Address Hobby 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps Name Address John 123 Main Mary 7 Lake Dr Bart 5 Pine St Result is a relation (no duplicates); can have fewer tuples than the original 12 6

Expressions π Id, Name (σ Hobby= stamps OR Hobby= coins (Person) ) Id Name Address Hobby 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps Person Id Name 1123 John 9876 Bart Result 13 Set Operators Relation is a set of tuples, so set operations should apply:,, (set difference) Result of combining two relations with a set operator is a relation => all its elements must be tuples having same structure Hence, scope of set operations limited to union compatible relations 14 7

Union Compatible Relations Two relations are union compatible if Both have the same number of columns Names of attributes are the same in both Attributes with the same name in both relations have the same domain Union compatible relations can be combined using union, intersection, and set difference 15 Example Tables: Person (SSN, Name, Address, Hobby) Professor (Id, Name, Office, Phone) are not union compatible. However π Name (Person) and π Name (Professor) are union compatible so π Name (Person) - π Name (Professor) makes sense. 16 8

Cartesian Product If R and S are two relations, R S is the set of all concatenated tuples <x,y>, where x is a tuple in R and y is a tuple in S (R and S need not be union compatible) R S is expensive to compute: Factor of two in the size of each row Quadratic in the number of rows a b c d a b c d x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 R S x3 x4 y3 y4 R S 17 Renaming Result of expression evaluation is a relation Attributes of relation must have distinct names. This is not guaranteed with Cartesian product e.g., suppose in previous example a and c have the same name Renaming operator tidies this up. To assign the names A 1, A 2, A n to the attributes of the n column relation produced by expression expr use expr [A 1, A 2, A n ] 18 9

Example Transcript (StudId, CrsCode, Semester, Grade) Teaching (ProfId, CrsCode, Semester) Transcript Teaching is not defined! Solution: π StudId, CrsCode (Transcript)[StudId, CrsCode1] π ProfId, CrsCode (Teaching) [ProfId, Crscode2] The result is a relation with 4 attributes: StudId, CrsCode1, ProfId, CrsCode2 19 Derived Operation: Join A (general or theta) join of R and S is the expression R join-condition S where join-condition is a conjunction of terms: A i oper B i in which A i is an attribute of R; B i is an attribute of S; and oper is one of =, <, >,,. The meaning is: σ join-condition (R S) where join-condition and join-condition are the same, except for possible renamings of attributes (next) 20 10

Join and Renaming Problem: R and S might have attributes with the same name in which case the Cartesian product is not defined Solution: Rename attributes prior to forming the product and use new names in join-condition. Common attribute names are qualified with relation names in the result of the join 21 Join Example Output the names of all employees that earn more than their managers. Tables: Employee(Name, Id, Salary, MngrId) Manager(Name, Id, Salary) π Employee.Name (Employee MngrId=Manager.Id AND Employee.Salary>Manager.Salary Manager) The join yields a table with attributes: Employee.Name, Employee.Id, Employee.Salary, MngrId Manager.Name, Manager.Id, Manager.Salary 22 11

π Name,CrsCode (Student Id Name Addr Status 111 John.... 222 Mary.... 333 Bill.... 444 Joe.... Equijoin - Example Equijoin: Join condition is a conjunction of equalities. Student Mary CSE306 Bill CSE304 Id=StudId σ Grade= A (Transcript)) Transcript StudId CrsCode Sem Grade 111 CSE305 S00 B 222 CSE306 S99 A 333 CSE304 F99 A The equijoin is used very frequently since it combines related data in different relations. 23 Special case of equijoin: Natural Join join condition equates all and only those attributes with the same name (condition doesn t have to be explicitly stated) duplicate columns eliminated from the result Transcript (StudId, CrsCode, Sem, Grade) Teaching (ProfId,( CrsCode, Sem) Transcript Teaching = π StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId ( Transcript Transcript.CrsCode=Teaching.CrsCode AND Transcript.Sem=Teaching.Sem Teaching ) 24 12

Natural Join (con t) More generally: R S = π attr-list (σ join-cond (R S) ) where attr-list = attributes (R) attributes (S) (duplicates are eliminated) and join-cond has the form: A 1 = A 1 AND AND A n = A n where {A 1 A n } = attributes(r) attributes(s) 25 Natural Join Example List all Id s of students who took at least two different courses. Transcript Solution: Transcript(StudId, CrsCode, Sem, Grade) Transcript won t work! Tr = π stuid, CrsCode (Transcript)[stuid, CrsCode2] π StudId ( σ CrsCode CrsCode2 (Transcript Tr) (Don t want to join on CrsCode, Sem, and Grade attributes) 26 13

Division Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s r (A 1, A n, B 1, B m ) s (B 1 B m ) r/s, with attributes A 1, A n, is the set of all tuples <a> such that for every tuple <b> in s, <a,b> is in r Can be expressed in terms of projection, set difference, and cross-product 27 Division (con t) 28 14

Division - Example List the Ids of students who have passed all courses that were taught in spring 2000 Numerator: StudId and CrsCode for every course passed by every student: π StudId, CrsCode (σ Grade F (Transcript) ) Denominator: CrsCode of all courses taught in spring 2000 π CrsCode (σ Semester= S2000 (Teaching) ) Result is numerator/denominator 29 Schema for Student Registration System Student (Id, Name, Addr, Status) Professor (Id, Name, DeptId) Course (DeptId, CrsCode, CrsName, Descr) Transcript (StudId, CrsCode, Semester, Grade) Teaching (ProfId, CrsCode, Semester) Department (DeptId, Name) 30 15