What is an Algebra. Core Relational Algebra. What is Relational Algebra? Operação de Seleção. Álgebra Relacional: Resumo

Similar documents
Relational Algebra. Algebra of Bags

Relational Algebra and SQL. Basic Operations Algebra of Bags

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

Chapter 2 The relational Model of data. Relational algebra

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Relational Query Languages

1 Relational Data Model

Databases-1 Lecture-01. Introduction, Relational Algebra

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

CS411 Database Systems. 04: Relational Algebra Ch 2.4, 5.1

Relational Model, Relational Algebra, and SQL

Chapter 8: The Relational Algebra and The Relational Calculus

CS 377 Database Systems

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

Relational Algebra 1

Relational Query Languages: Relational Algebra. Juliana Freire

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

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

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

2.2.2.Relational Database concept

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

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

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Chapter 6 The database Language SQL as a tutorial

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

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

MIS Database Systems Relational Algebra

Chapter 6 Part I The Relational Algebra and Calculus

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Subqueries. Must use a tuple-variable to name tuples of the result

Relational Algebra Part I. CS 377: Database Systems

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

Database Management System. Relational Algebra and operations

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

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

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

Chapter 6 The Relational Algebra and Calculus

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

RELATIONAL ALGEBRA. CS 564- Fall ACKs: Dan Suciu, Jignesh Patel, AnHai Doan

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

Relational Algebra. Relational Query Languages

CMP-3440 Database Systems

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

Relational Algebra and SQL

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

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

They like one or more beers and frequent one. Bars have unique names and addresses. They. serve one or more beers and are frequented

Database Design and Programming

Relational Algebra 1

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

Chapter 6 The database Language SQL as a tutorial

Chapter 6 5/2/2008. Chapter Outline. Database State for COMPANY. The Relational Algebra and Calculus

RELATIONAL DATA MODEL: Relational Algebra

Relational Algebra. Procedural language Six basic operators

Chapter 6 - Part II The Relational Algebra and Calculus

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

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

Relational Query Languages

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

Chapter 8: Relational Algebra

Relational Model and Relational Algebra

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

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

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

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

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

Databases Relational algebra Lectures for mathematics students

Relational Algebra & Calculus. CS 377: Database Systems

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

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

CSC 261/461 Database Systems Lecture 13. Fall 2017

Ian Kenny. November 28, 2017

Chapter 3. Algorithms for Query Processing and Optimization

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

Chapter 2: Intro to Relational Model

Chapter 2: The Relational Algebra

I Relational Database Modeling how to define

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

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

I Relational Database Modeling how to define

Grouping Operator. Applying γ L (R) Recall: Outerjoin. Example: Grouping/Aggregation. γ A,B,AVG(C)->X (R) =??

Chapter 2: The Relational Algebra

The Relational Model and Relational Algebra

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Relational Databases

Querying Microsoft SQL Server 2014 (20461)

Lecture 16. The Relational Model

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

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

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

CompSci 516: Database Systems

Relational Algebra 1

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

ECE 650 Systems Programming & Engineering. Spring 2018

Relational Model and Relational Algebra. Rose-Hulman Institute of Technology Curt Clifton

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

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

Transcription:

What is an Algebra Bancos de Dados Avançados Revisão: Álgebra Relacional DCC030 - TCC: Bancos de Dados Avançados (Ciência Computação) DCC049 - TSI: Bancos de Dados Avançados (Sistemas Informação) DCC842 - Bancos de Dados (Pós-Graduação) Mathematical system consisting of: Operands---variablesor valuesfrom which new values can be constructed. Operators---symbols denoting proceduresthat construct new values from given values. MIRELLA M. MORO mirella@dcc.ufmg.br http://www.dcc.ufmg.br/~mirella 2 What is Relational Algebra? An algebra whose operands are relations or variables that represent relations. Operators are designed to do the most common things that we need to do with relations in a database. The result is an algebra that can be used as a query language for relations. Core Relational Algebra Union, intersection, and difference. Usual set operations, but require both operands have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 3 4 Álgebra Relacional: Resumo Dadas duas relações R1 e R2 Operações Básicas Selection (σ) Seleciona em sub-conjunto de fileiras da relação Projection (π) Deleta colunas indesejadas da relação Cross-product ( ) Permite-nos combinar R1 e R2 Set-difference ( ) Tuplas em R1, mas não em R2 Union ( ) Tuplas em R1 e em R2 Operações Adicionais: Intersecção, junção, divisão, renomear Base de Dados: Operação de Seleção????? Desde que cada operação retorna uma relação, operações podem ser compostas! Database Management Systems, R. Ramakrishnan (tradução, autorizada, de Anna & Mario Nascimento) 5R 6R

Operação de Projeção Base de Dados:??? Base de Dados: Seqüência de Operações??? 7R 8R Building Complex Expressions Algebras allow us to express sequences of operations in a natural way. Example: in arithmetic ---(x + 4)*(y -3). Relational algebra allows the same. Three notations, just as in arithmetic: 1. Sequences of assignment statements. 2. Expressions with several operators. 3. Expression trees. 1. Sequences of Assignments Create temporary relation names. Renamingcan be implied by giving relations a list of attributes. Example: R3 R1 CR2 can be written: R4 R1 *R2 R3 σ C (R4) 9 10 2. Expressions in a Single Assignment Example: the theta-join R3 R1 CR2 can be written: R3 σ C (R1 *R2) Precedence of relational operators: 1. Unary operators ---select, project, rename ---have highest precedence, bind first. 2. Then come products and joins. 3. Then intersection. 4. Finally, union and set difference bind last. But you can always insert parentheses to force the order you desire. 3. Expression Trees Leaves are operands ---either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children. 11 12

Exemplos Bars(name, addr) Sells(bar, beer, price) As a Tree: UNION Bars(name, addr) Sells(bar, beer, price) 1. Find the names of all the bars that are either on Maple St. or sell Bud for less than $3. 2. Using Sells, find the bars that sell two different beers at the same price. Strategy: by renaming, define a copy of Sells, called S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price. PROJECT name SELECT addr = Maple St. Bars RENAME R(name) PROJECT bar SELECT price<3 AND beer= Bud Sells 13M 14 The Tree Bars(name, addr) Sells(bar, beer, price) Schemas for Interior Nodes PROJECT bar SELECT beer!= beer1 JOIN An expression tree defines a schema for the relation associated with each interior node. Similarly, a sequence of assignments defines a schema for each relation on the left of the sign. RENAME S(bar, beer1, price) Sells Sells 15 16 Schema-Defining Rules 1 For union, intersection, and difference, the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes tells us the schema. Schema-Defining Rules 2 Product: the schema is the attributes of both relations. Use R.A, etc., to distinguish two attributes named A. Theta-join: same as product. Natural join: use attributes of both relations. Shared attribute names are merged. Renaming: the operator tells the schema. 17 18

Relational Algebra on Bags A bagis like a set, but an element may appear more than once. Multiset is another name for bag. Example: {1,2,1,3} is a bag. {1,2,3} is also a bag that happens to be a set. Bags also resemble lists, but order in a bag is unimportant. Example: {1,2,1} = {1,1,2} as bags, but [1,2,1]!= [1,1,2] as lists. Why Bags? SQL, the most important query language for relational databases is actually a bag language. SQL will eliminate duplicates, but usually only if you ask it to do so explicitly. Some operations, like projection, are much more efficient on bags than sets. 19 20 Operations on Bags SELECTIONapplies to each tuple, so its effect on bags is like its effect on sets. PROJECTIONalso applies to each tuple, but as a bag operator, we do not eliminate duplicates. PRODUCTSand JOINSare done on each pair of tuples, so duplicates in bags have no effect on how we operate. Example: Bag Selection R( A, B ) S( B, C ) SELECT A+B<5 (R) = A B 21 22 Example: Bag Projection Example: Bag Product R( A, B ) S( B, C ) R( A, B ) S( B, C ) PROJECT A (R) = A 1 5 1 23 R * S = A R.B S.B C 7 8 5 6 3 4 7 8 24

Example: Bag Theta-Join R( A, B ) S( B, C ) R JOIN R.B<S.B S = A R.B S.B C 7 8 7 8 Bag Union Union, intersection, and difference need new definitions for bags. An element appears in the union of two bags the sum of the number of times it appears in each bag. Example: {1,2,1} UNION {1,1,2,3,1} = {1,1,1,1,1,2,2,3} 25 26 Bag Intersection An element appears in the intersection of two bags the minimum of the number of times it appears in either. Example: {1,2,1} INTER {1,2,3} = {1,2}. Bag Difference An element appears in the difference A B of bags as many times as it appears in A, minus the number of times it appears in B. But never less than 0 times. Example: {1,2,1} {1,2,3} = {1}. 27 28 Beware: Bag Laws!= Set Laws Not all algebraic laws that hold for sets also hold for bags. For one example, the commutative law for union (R UNION S= S UNION R ) doeshold for bags. Since addition is commutative, adding the number of times xappears in Rand Sdoesn t depend on the order of Rand S. An Example of Inequivalence Set union is idempotent, meaning that S UNION S= S. However, for bags, if xappears n times in S, then it appears 2n times in S UNION S. Thus S UNION S!= S in general. 29 30

Outerjoin Example: Outerjoin Suppose we join RJOIN C S. A tuple of R that has no tuple of S with which it joins is said to be dangling. Similarly for a tuple of S. Outerjoin preserves dangling tuples by padding them with a special NULL symbol in the result. R = A B S = B C 2 3 4 5 6 7 (1,2) joins with (2,3), but the other two tuples are dangling. R OUTERJOIN S = A B C 3 4 5 NULL NULL 6 7 31 32 Operação de Divisão Operação de Divisão Seja a consulta: recupere os nomes dos empregados que trabalham em todosos projetos em que o empregado John Smith trabalha Esta consulta pode ser expressa usando a operação de divisão ( ) da seguinte forma: SMITH σ FNAME = John AND LNAME = Smith (EMPLOYEE) SMITH_PNOS π PNO (WORKS_ON ESSN = SSNSMITH) SSN_PNOS π ESSN,PNO (WORKS_ON) SSNS(SSN) SSN_PNOS SMITH_PNOS RESULT π FNAME,LNAME (SSNS EMPLOYEE) SSN_PNOS π ESSN,PNO (WORKS_ON) SMITH_PNOS π PNO (WORKS_ON ESSN = SSN SMITH) SSNS(SSN) SSN_PNOS SMITH_PNOS Database Management Systems, R. Ramakrishnan (tradução, autorizada, de Anna & Mario Nascimento) 33R 34R Operação de Divisão T R S Exemplos de A/B 35R 36R

Expressando A/B Usando Operadores Básicos Divisão não é operador essencial; só uma útil taquigrafia Também é o caso de joins, mas joins são tão comuns que muito sistemas o implementam Idéia: Para A/B, calcule todos valores x que não são disqualificados por algum y em B x é disqualificado se ao juntar um y de B, obtemos uma tupla xy que não está em A Outros Exemplos 1. Encontre nomes dos sailors que reservaram um barco vermelho 2. Encontre o sid dos sailors com idade acima de 20 anos que não reservaram um barco vermelho 3. Encontre nomes dos sailors que reservaram um barco vermelho ou verde 4. Encontre sailors que reservaram todos os barcos 37R 38R 1. Encontre nomes dos sailors que reservaram um barco vermelho 2. Encontre o sid dos sailors com idade acima de 20 anos que não reservaram um barco vermelho π sid (σ age > 20 Sailors) π sid ((σ color= red Boats) Reserves Sailors ) 39R 40R 3. Encontre nomes dos sailors que reservaram um barco vermelho ou verde Encontre nomes dos sailors que reservaram um barco vermelho ou verde 41R 42R

4. Encontre sailors que reservaram todos os barcos 43R