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



Similar documents
SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

SQL JOINs and VIEWs. Chapter 3.3 V3.0. Napier University Dr Gordon Russell

Relational Databases

Relational Algebra. Basic Operations Algebra of Bags

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

Translating SQL into the Relational Algebra

Introduction to tuple calculus Tore Risch

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

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

Relational Algebra and SQL

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

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

Review: Participation Constraints

Relational Algebra and SQL Query Visualisation

Schema Mappings and Data Exchange

MapReduce examples. CSE 344 section 8 worksheet. May 19, 2011

SQL Nested & Complex Queries. CS 377: Database Systems

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

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

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

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

The Relational Algebra

1.4 Compound Inequalities

Comp 5311 Database Management Systems. 3. Structured Query Language 1

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

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

Absolute Value Equations and Inequalities

Chapter 5: Overview of Query Processing

Displaying Data from Multiple Tables. Chapter 4

Chapter 14: Query Optimization

Solving Rational Equations and Inequalities

SQL: Queries, Programming, Triggers

Introduction to database design

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

The Entity-Relationship Model

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

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

Relational Database: Additional Operations on Relations; SQL

Week 4 & 5: SQL. SQL as a Query Language

Displaying Data from Multiple Tables

Evaluation of Expressions

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

Relational Division and SQL

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

Applying Constraint Logic Programming to SQL Test Case Generation

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

Exercises. a. Find the total number of people who owned cars that were involved in accidents

3. Relational Model and Relational Algebra

Chapter 13: Query Optimization

Lecture 4: More SQL and Relational Algebra

Database Constraints and Design

Method To Solve Linear, Polynomial, or Absolute Value Inequalities:

Lecture 6: Query optimization, query tuning. Rasmus Pagh

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

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Chapter 5. SQL: Queries, Constraints, Triggers

Temporal Database System

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

>

Database Management Systems. Chapter 1

CSC 443 Data Base Management Systems. Basic SQL

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

SQL NULL s, Constraints, Triggers

SQL: Queries, Programming, Triggers

Chapter 6: Integrity Constraints

Databases What the Specification Says

SQL Query Evaluation. Winter Lecture 23

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

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

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

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

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

Comp 3311 Database Management Systems. 2. Relational Model Exercises

The MonetDB Architecture. Martin Kersten CWI Amsterdam. M.Kersten


Oracle Database 11g: SQL Tuning Workshop

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Lecture 6. SQL, Logical DB Design

Algebra I Notes Relations and Functions Unit 03a

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Karolinska Institutet, Stockholm, Sweden INSTRUCTIONS HOW TO USE THE THESIS TEMPLATE IN WORD 2010/2013 FOR WINDOWS

Chapter 13: Query Processing. Basic Steps in Query Processing

Advance DBMS. Structured Query Language (SQL)

CS 338 Join, Aggregate and Group SQL Queries

BCA. Database Management System

Copyright 2013 wolfssl Inc. All rights reserved. 2

From Databases to Natural Language: The Unusual Direction

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

The Relational Data Model and Relational Database Constraints

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

Introduction to Triggers using SQL

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Transcription:

Relational Algebra 2 Chapter 5.2 V3.0 Copyright @ 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 becomes: σ depno = 1 ^ surname = `Smith (employee) (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) A B A B 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 joinconditions. 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.