The Relational Algebra

Similar documents
Relational Algebra Part I. CS 377: Database Systems

Relational Algebra & Calculus. CS 377: Database Systems

Relational Databases Overview

KINGDOM OF SAUDI ARABIA-JAZAN UNIVERSITY COLLEGE OF COMPUTER SCIENCE & INFORMATION SYSTEMS 221 INFS 3 DATABASE SYSTEMS-1 REVIEW QUESTIONS

CMP-3440 Database Systems

NORMALISATION (Relational Database Schema Design Revisited)

Faloutsos - Pavlo CMU SCS /615

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

Chapter 6 The Relational Algebra and Relational Calculus

Course Notes on Relational Algebra

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

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Relational Algebra 1

Chapter 8: Relational Algebra

CS 377 Database Systems

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

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

Relational Query Languages: Relational Algebra. Juliana Freire

Relational Algebra. Ron McFadyen ACS

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Relational Algebra for sets Introduction to relational algebra for bags

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Relational Algebra. Procedural language Six basic operators

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

DATABASE DESIGN I - 1DL300

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

QUERY PROCESSING & OPTIMIZATION CHAPTER 19 (6/E) CHAPTER 15 (5/E)

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

MIS Database Systems Relational Algebra

Chapter 3. The Relational Model. Database Systems p. 61/569

Chapter 2: Intro to Relational Model

Chapter 3B Objectives. Relational Set Operators. Relational Set Operators. Relational Algebra Operations

ECE 650 Systems Programming & Engineering. Spring 2018

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

RELATIONAL DATA MODEL: Relational Algebra

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Database Technology. Topic 3: SQL. Olaf Hartig.

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Database design process

Relational Model, Relational Algebra, and SQL

Chapter 6 - Part II The Relational Algebra and Calculus

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

CSC 261/461 Database Systems Lecture 13. Fall 2017

Chapter 8: The Relational Algebra and The Relational Calculus

Lecture 16. The Relational Model

Relational Databases

CS317 File and Database Systems

Chapter 19 Query Optimization

Informationslogistik Unit 4: The Relational Algebra

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Relational Model: History

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

Chapter 3. Algorithms for Query Processing and Optimization

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Chapter 5. Relational Algebra and Relational Calculus

2.2.2.Relational Database concept

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

Chapter 2: Intro to Relational Model

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

RELATIONAL DATA MODEL

Relational Query Languages

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

RDBMS- Day 4. Grouped results Relational algebra Joins Sub queries. In today s session we will discuss about the concept of sub queries.

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

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

Chapter 2: Relational Model

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

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

DATABASE DESIGN I - 1DL300

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Chapter 6: Formal Relational Query Languages

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

SQL STRUCTURED QUERY LANGUAGE

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

1 Relational Data Model

Basant Group of Institution

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Slides by: Ms. Shree Jaswal

Mahathma Gandhi University

Insertions, Deletions, and Updates

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

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

CSCC43H: Introduction to Databases. Lecture 3

Structured Query Language (SQL)

Simple SQL Queries (contd.)

Relational Algebra. Relational Query Languages

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

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

This lecture. Projection. Relational Algebra. Suppose we have a relation

CS2300: File Structures and Introduction to Database Systems

Relational Databases. Relational Databases. Extended Functional view of Information Manager. e.g. Schema & Example instance of student Relation

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

CS

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

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Transcription:

Querying a Relational Database So far we have seen how to design a relational database and now we turn to mechanisms to interrogate a database We wish to extract from the database a subset of the information that answers some question. Here are some typical questions we might ask: What are the department names? Tell me all the data held about male employees. What are the names of the employees in the R&D department? Procedural Languages The procedural approach builds programs as sequences of sub-setting and combining operations; The base tables in the database are relations Each of the operators returns a relation which is derived from the base tables (i.e. a view not a copy) the result of one operator can be fed into another The Relational Algebra is a formal (i.e. not one you can use in practice) procedural language which is what the computer uses inside The questions are called queries and consists of programs built out of: (i) retrieving data as a subset of some relation and (ii) combining two relations together in a meaningful way We will see two kinds of language for describing queries procedural languages describe step-by-step what is to be done declarative language only describe what is to be returned R1 R2 R3 R4 op1 V1 op2 V2 result 337 338 Declarative Languages The declarative approach describes the desired results and lets the DBMS work out the best sequence of operations The Relational Calculus (not covered) is a formal declarative language SQL is a concrete language (i.e. you can use it) which his declarative and is the standard language with which databases are communicated both by humans and by other programs Choose particular columns from R2 and R3 R1 R2 result R3 and R4 rows from R2 and R3 which have related values. 339 The Relational Algebra is a formal language for querying relations an internal language, NOT a language for the user has the basic operations required of any Data Manipulation Language all DBMS should provide these capabilities they can be used as the basis of a usable Data Manipulation Language they are used in query implementation and optimization SQL is turned into these operations relations can be manipulated in limited but useful ways many query languages also allow additional operations such as calculations, summary and ordering A query in the relational algebra is a series of assignments to variables which hold views, e.g.: Males σ sex = M ( Employee ) View Name Assignment Symbol Operation Table(s) Used 340

SUMMARY : Relational Algebra Operations The principal relational operations are: σ select* - pick rows from a relation by some condition project* - pick columns by name join - connect two relations usually by a Foreign Key The main set operations include: union* - make the table containing all the rows of two relations intersection - pick the rows which are common to two relations difference* - pick the rows which are in one table but not another X Cartesian product* - pair off each of the tuples in one relation with those in another - creating a double sized row for each pair All the other operations can be defined in terms of the five marked with a star All of the operations return relations Selection (or Restriction) σ Selection extracts the tuples of a relation which satisfy some condition on the values of their rows and return these as a relation Example: return all the employees who work in the city of Glasgow i.e. get all of the records from the Employee table for which the city column holds the value Glasgow Locals σ city = "Glasgow ( Employee ) Locals is then the name of the query which could be used as a view in subsequent queries The condition is similar to a Java boolean expression except that it uses the words NOT, OR and AND instead of!, and && and it contain: Literals i.e. constants comparison operators ( =, >, etc. ) column names boolean operators ( and, not, or ) YoungOrNear σ city = "Glasgow OR (city = Stirling AND bdate > 1/1/80 ) (Employee) 341 342 Projection Projection extracts some of the fields from a relation, by giving the names of the fields GenderSalary gender, salary ( Employee ) In the result: No attribute may occur more than once Duplicate entries will be removed Thus if we want to retain the number of times each is used, we must include the Primary Key Projection and selection can be combined. It is usually more appropriate to reduce the number of rows first before reducing the columns For instance, to determine the names of employees working in Glasgow Answer name (σ city = "Glasgow (Employee) ) This does a selection followed by a projection the inner operation is performed first 343 Union The union operator produces a relation which combines two relations by containing all of the tuples from each - removing duplicates See examples on next slide People Students Staff The two relations must be "union compatible i.e. have the same number of attributes drawn from the same domains (but maybe having different names) If attribute names differ, the names from the first one are taken 344

Two Union Compatible Relations STUDENT STAFF STUDENT STAFF FN LN FN LN FN LN Susan Yao John Smith Susan Yao Ramesh Shah Ricardo Browne Ramesh Shah Johnny Kohler Susan Yao Johnny Kohler Barbara Jones Francis Johns Barbara Jones Amy Ford Ramesh Shah Amy Ford Jimmy Wang Jimmy Wang Ernest Gilbert Ernest Gilbert John Smith STUDENT STAFF - STUDENT STAFF Ricardo Browne FN LN Francis Johns FN LN John Smith Susan Yao Ramesh Shah Ricardo Browne Francis Johns Example We would like a list of names and emails of everyone living in Glasgow We can use union if we reduce the staff and student tables to a common set of columns Staff ( ni#, sname, city,, phone, email, room ) Student ( ni#, name, city,, email, course, year ) GStaff sname, email ( σ city = "Glasgow ( Staff) ) GStud name, email ( σ city = "Glasgow ( Student ) ) GlasgowEmails GStudents GStaff 345 346 Intersection This is similar to union but returns tuples that are in both relations Example the female students living in Glasgow: FemalesInGlasgow σ city = "Glasgow ( Employee ) σ gender = F ( Employee ) How else can this particular query be written? Why can it be rewritten? Difference - Similar to union but returns tuples that are in the first relation but not the second Example non-local lemployees: NonLocals Employee - Locals Intersection and difference both require union compatibility Both use column names from the first relation Only operations based on the same relation can be rewritten 347 348

Cartesian Product X Every row from the first relation is paired with every row in the second relation rarely useful on its own Employee Dependent eni# dpdname relship name ednum ni# 444 Tim Child Jim Grey 5 444 444 Tom Father Jo White 5 555 555 June Child Employee x Dependent name ednum ni# eni#name relship * Jim Grey 5 444 444 Tim Child * Jim Grey 5 444 444 Tom Father Jim Grey 5 444 555 June Child Jo White 5 555 444 Tim Child Jo White 5 555 444 Tom Father * Jo White 5 555 555 June Child 349 Defining the Cartesian Product The Cartesian Product of two relations A and B, which have attributes A 1... A m and B 1... B n, is the relation with m + n attributes containing a row for every pair of rows, one from A and one from B Thus if A has a tuples and B has b tuples then the result has a x b tuples 350 Equi-Join Natural or Inner Join The rows marked with a star on Slide 349 are the ones we really want This could be created with the following selection, which essentially makes use of the foreign key σ ni# = eni# ( Employee X Dependent ) Cartesian product followed by this kind of selection is called a JOIN because it joins together two relations There are a wide variety of join operators, as we shall see This one is called an equi-join It has the weakness that it keeps both of the columns which are now identical In its simplest form, the join of relations A and B pairs off the tuples of A and B so that named attributes from the relations have the same value Now we have two columns holding the same value, so we eliminate the duplicated column to form the natural or inner join Employee ni# = eni# Dependent name ednum ni# dpdname relship * Jim Grey 5 444 Tim Child * Jim Grey 5 444 Tom Father * Jo White 5 555 June Child When we use the term join without further comment, this is what is meant! 351 352

Employee Department ni# Name ednum etc dnum dname mgrni# etc 222 Joe Brown 4. 4R & D 222. 333 Kay Lee 4. 5 Admin 555. 555 Tom Low 5.. 6 Finance.. These relations can be joined in two ways Here they are joined on department number, so that we have the department details for an employee Unmatched tuples disappear (no employees in Finance) EmpAndDept Employee ednum = dnum Department ni# Name ednum etc dname mgrni# etc 222 Joe Brown 4. R&D 222. 333 Kay Lee 4. R & D 222. 555 Tom Low 5.. Admin 555.. They can also be joined by matching the employee national insurance number with the manager national insurance number, to give the department details together with the employee who manages it Unmatched tuples disappear (no manager for Finance, and Kay Lee is not a manager ) MgrAndDept Employee ni# = mgrni# Department ni# Name ednum etc dnum dname etc 222 Joe Brown 4. 4 R & D. 555 Tom Low 5. 5 Admin. 353 354 Theta Join θ The most general form of join is called a theta-join. This allows the connection of tuples by other comparison components The operator, θ, can be any comparison operator e.g. < > >= <= if it is = this is an equi-join Employee X ni# θ mgrni# Department Outer Join This includes all of the unmatched data so as to preserve all the data Employee outerjoin ni# = mgrni# Department ni# Name ednum etc dnum dname etc 222 Joe Brown 4. 4 R & D. 555 Tom Low 5. 5 Admin. 333 Kay Lee 4.. null null null null null null null 6 Finance.. 355 Right and Left Outer Joins Left and right outer joins include unmatched data from only one of the two relations To get a list of all departments t and their employees, and to include the departments without employees a right outer join Employee rightouterjoin ni# = mgrni# Department ni# Name ednum etc dnum dname etc 222 Joe Brown 4. 4 R & D. 555 Tom Low 5. 5 Admin. null null null null 6 Finance.. 356

Semi Join A semi-join ji is one in which hihonly the columns of one of fh the two tables is returned : E.g. to get the details of the managers, a left semi-join version of the join on the manager number would give: Because the only employees in the result are those which join with departments on the manager foreign key, all non-managers are eliminated ni# Name ednum etc 222 Joe Brown 4. 555 Tom Low 5. Division Which employees work on all projects that John Smith works on These are the most difficult kinds of query to describe in relational algebra (and in SQL) Start by finding all the numbers of the projects that John Smith works on (maybe these are 3 & 4) EMPWO Emp ni#=wni# WorksOn JSPNos σ ename= JS ( EMPWO ) JSPNos pnum ( JSproj ) Then make a list of who works on which project ProjEmps wni#, wpnum ( WorksOn ) Dividing the JSPNos into ProjEmps gives us the employee numbers of anyone who works on all the entries in the JSPNos relation: Result ProjEmps JSPNos JSPNos pnum 3 ProjEmps 4 wni# wpnum 123 1 145 3 145 4 269 1 169 3 172 2 172 3 172 4 Result wni# 145 172 357 358 Some Examples Note the words in the question which alert us to a division query are all and every 1. Give me the name and salaries of all employees who work for the R & D department List the project name, controlling department, and the department manager s name for every project in Stafford ResDept σ Dname= R&D ( DEPARTMENT ) ResDeptEmps Emp dnum=dept ResDept Result name, salary ( ResDeptEmps ) Common Pattern: select join - project 2. List the project name, controlling department, and the department manager s name for every project in Stafford StaffordProjects σ Location= Stafford ( PROJECT ) StaffProjDepts StaffordProjects condept = dnum Department StaffProjMgrs StaffProjDepts mgrni# = ni# Employee Result Pname,Dname, name ( StaffProjMgrs ) 3. List the names of employees who work on all the projects controlled by department 5. D5projects σ condept=5 ( Project ) D5projNums pnum ( D5Projects ) EmpProj emp, project ( WorksOn ) // just the numbers // get rid of hours EmpNIs EmpProj D5ProjNums // get emp numbers EmpsWanted EmpNIs emp=ni# Employee Result name ( EmpsWanted ) General lstrategy: Identify which h tables you need. Usually start by reducing number of rows. Join. Then reduce number of columns to what you are asked for. 359 360