Relational Model and Relational Algebra

Similar documents
CSC 261/461 Database Systems Lecture 13. Fall 2017

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

Lecture 16. The Relational Model

CMPSCI 645 Database Design & Implementation

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

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

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

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

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

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

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

Relational Query Languages: Relational Algebra. Juliana Freire

Relational Model & Algebra. Announcements (Tue. Sep. 3) Relational data model. CompSci 316 Introduction to Database Systems

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

Relational Model 2: Relational Algebra

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

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

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

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

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

Relational Algebra. Relational Query Languages

CMP-3440 Database Systems

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

EECS 647: Introduction to Database Systems

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

Chapter 2: The Relational Algebra

Relational Query Languages

2.2.2.Relational Database concept

Chapter 2: The Relational Algebra

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

Chapter 6 Formal Relational Query Languages

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

CSCC43H: Introduction to Databases. Lecture 3

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

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

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

Relational Algebra for sets Introduction to relational algebra for bags

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

Relational Algebra 1. Week 4

1 Relational Data Model

Relational Algebra and SQL. Basic Operations Algebra of Bags

CS 377 Database Systems

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

MIS Database Systems Relational Algebra

Optimization Overview

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 Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Faloutsos - Pavlo CMU SCS /615

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

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Chapter 2 The relational Model of data. Relational algebra

Ian Kenny. November 28, 2017

Relational Algebra. Procedural language Six basic operators

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

ECE 650 Systems Programming & Engineering. Spring 2018

Relational Algebra. Algebra of Bags

Introduction to Data Management. Lecture #11 (Relational Languages I)

Chapter 2: Intro to Relational Model

CompSci 516: Database Systems

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

Informatics 1: Data & Analysis

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

Database Applications (15-415)

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

Databases Relational algebra Lectures for mathematics students

Relational Query Languages

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

Chapter 6 Part I The Relational Algebra and Calculus

CS2300: File Structures and Introduction to Database Systems

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

Relational Model, Relational Algebra, and SQL

CS 2451 Database Systems: Relational Algebra & Relational Calculus

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

Chapter 2: Intro to Relational Model

Relational Algebra 1

Databases Lectures 1 and 2

Relational Model and Algebra. Introduction to Databases CompSci 316 Fall 2018

Database Technology Introduction. Heiko Paulheim

Databases-1 Lecture-01. Introduction, Relational Algebra

Informationslogistik Unit 4: The Relational Algebra

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

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

Relational Algebra Part I. CS 377: Database Systems

Database Systems. Course Administration. 10/13/2010 Lecture #4

Relational Databases

1. Introduction; Selection, Projection. 2. Cartesian Product, Join. 5. Formal Definitions, A Bit of Theory

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

What s a database system? Review of Basic Database Concepts. Entity-relationship (E/R) diagram. Two important questions. Physical data independence

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

RELATIONAL DATA MODEL: Relational Algebra

Selection and Projection

Relational Model Introduction

Relational Algebra 1

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

Relational Algebra and Calculus

CMPT 354: Database System I. Lecture 3. SQL Basics

Lecture #8 (Still More Relational Theory...!)

Databases 1. Daniel POP

Transcription:

Relational Model and Relational Algebra CMPSCI 445 Database Systems Fall 2008 Some slide content courtesy of Zack Ives, Ramakrishnan & Gehrke, Dan Suciu, Ullman & Widom

Next lectures: Querying relational data Today Relational model, Relational algebra Wednesday SQL Homework 1 assigned Friday Next Week More SQL

The Relational Model The relational data model (Codd, 1970): Data independence: details of physical storage are hidden from users High-level declarative query language say what you want, not how to compute it. mathematical foundation

Relational Database: Definitions

Relational Database: Definitions Relational database: a set of relations

Relational Database: Definitions Relational database: a set of relations Relation: made up of 2 parts: Schema : specifies name of relation, plus name and type/domain of each column. Instance : a table, with rows and columns.

Relational Database: Definitions Relational database: a set of relations Relation: made up of 2 parts: Schema : specifies name of relation, plus name and type/domain of each column. Instance : a table, with rows and columns. Students(sid: string, name: string, login: string, age: integer, gpa: real). Restriction: all attributes are of atomic type, no nested tables

Relational instances: tables Students

Relational instances: tables Students column, attribute, field

Relational instances: tables Students column, attribute, field row, tuple

Relational instances: tables Students column, attribute, field row, tuple Attribute value

Relational instances: tables Students column, attribute, field row, tuple Attribute value A relation is a set of tuples: no tuple can occur more than once Real systems may allow duplicates for efficiency or other reasons we ll come back to this.

Example Database STUDENT Takes COURSE sid name 1 Jill 2 Bo 3 Maya sid cid 1 445 1 483 3 435 PROFESSOR fid name 1 Diao 2 Saul 8 Weems cid title sem 445 DB F08 483 AI S08 435 Arch F08 Teaches fid cid 1 445 2 483 8 435

Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. DB query languages!= programming languages not expected to be Turing complete. not intended to be used for complex calculations. support easy, efficient access to large data sets.

Query language preliminaries Query Q: R1..Rn R A query is applied to one or more relation instances The result of a query is a relation instance. Input and output schema: Schema of input relations for a query are fixed The schema for the result of a given query is also fixed: determined by definition of query language constructs.

What is an Algebra?

What is an Algebra? Mathematical system consisting of:

What is an Algebra? Mathematical system consisting of: Operands --- variables or values from which new values can be constructed.

What is an Algebra? Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols denoting procedures that construct new values from given values.

What is the 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.

Relational Algebra Operates on relations, i.e. sets Later: we discuss how to extend this to bags Five basic operators: Union: Difference: - Selection: σ Projection: Π Cartesian Product: Derived or auxiliary operators: Intersection, complement Joins (natural,equi-join, theta join) Renaming: ρ

1. Union and 2. Difference R 1 sid name R 2 sid name 1 Jill 1 Jill 2 Bo 4 Bob 3 Maya R 1 R 2 sid name 1 Jill 2 Bo R 1 R 2 sid name 2 Bo 3 Maya 3 Maya 4 Bob

What about Intersection? It is a derived operator R 1 R 2 = R 1 (R 1 R 2 ) Also expressed as a join (will see later) R 1 R 2 R 1 R 2

3. Selection Returns all tuples which satisfy a condition Course Notation: σ c (R) Examples σ CID > 400 (Course) σ title = AI (Course) cid title sem 445 DB F08 483 AI S08 435 Arch F08 The condition c can be =, <,, >,, <>

4. Projection Eliminates columns, then removes duplicates Notation: Π A1,,An (R) Example: project cid and name Π cid, name (Course) Output schema: Answer(cid, name)

4. Projection Eliminates columns, then removes duplicates Notation: Π A1,,An (R) Example: project cid and name Π cid, name (Course) Output schema: Answer(cid, name) Course cid name sem 445 DB F08 483 AI S08 445 DB S08 Π Answer cid name 445 DB 483 AI

5. Cartesian Product Each tuple in R 1 with each tuple in R 2 Notation: R 1 R 2 Very rare in practice; mainly used to express joins Also called Cross Product

Cartesian Product Student sid name 1 Jill 2 Bo Takes sid cid 1 445 1 483 3 435 17

Cartesian Product Student sid name 1 Jill 2 Bo Takes sid cid 1 445 1 483 3 435 Student Takes sid name sid cid 1 Jill 1 445 1 Jill 1 483 1 Jill 3 435 2 Bo 1 445 2 Bo 1 483 2 Bo 3 435 17

Renaming Changes the schema, not the instance Notation: ρ B1,,Bn (R) Example: ρ courseid, cname, term (Course)

Renaming Changes the schema, not the instance Notation: ρ B1,,Bn (R) Example: ρ courseid, cname, term (Course) Course cid name sem 445 DB F08 483 AI S08 445 DB S08 ρ courseid cname term 445 DB F08 483 AI S08 445 DB S08

Natural Join Notation: R 1 R 2 Meaning: R 1 R 2 = Π A (σ C (R 1 R 2 )) Where: The selection σ C checks equality of all common attributes The projection eliminates the duplicate common attributes

Natural join example Student sid name 1 Jill 2 Bo 3 Maya Takes sid cid 1 445 1 483 3 435 Calculate: Student Takes 20

Natural join example Student sid name 1 Jill 2 Bo 3 Maya Takes sid cid 1 445 1 483 3 435 Calculate: Student Takes sid name cid 1 Jill 445 1 Jill 483 3 Maya 435 20

Theta Join A join that involves a predicate R1 θ R2 = σ θ (R1 R2) Here θ can be any condition: =, <,,, >, Example: Student age>age Prof

Equi-join A theta join where θ is an equality R 1 A=B R 2 = σ A=B (R 1 R 2 ) Very useful join in practice Example: Student sid=sid Takes

Review Five basic operators of the Relational Algebra: Union: Difference: - Selection: σ Projection: Π Cartesian Product: Derived or auxiliary operators: Intersection, complement Joins (natural,equi-join, theta join) Renaming: ρ

Combining operators: complex expressions Π name,sid (σ name= DB (Course ( Students Takes ))) Π name,sid σ name= DB Course Students Takes

In-class exercise Please calculate: Π name,sid (σ title= DB (Course ( Students Takes ))) Course Students Takes cid title sem 445 DB F08 483 AI S08 435 Arch F08 sid name 1 Jill 2 Bo 3 Maya sid cid 1 445 1 483 3 435 25

Query equivalence Definition: Query Equivalence Two queries Q and Qʼ are equivalent if: for all databases D, Q(D) = Qʼ(D) 26

Query equivalence 26

Query Optimization Is Based on Algebraic Equivalences Relational algebra has laws of commutativity, associativity, etc. that imply certain expressions are equivalent. They may be different in cost of evaluation! σ c d (R) σ c ( σ d (R) ) R (S T) (R S) T) σ c (R S) σ c (R) S cascading selection join associativity pushing selections Query optimization finds the most efficient representation to evaluate (or one thatʼs not bad)

Relational calculus What is a calculus? The term "calculus" means a system of computation The relational calculus is a system of computing with relations 28

Relational calculus (in 1 slide) English: Name and sid of students who are taking the course DB

Relational calculus (in 1 slide) English: Name and sid of students who are taking the course DB RA: Π name,sid (Students Takes σ name= DB (Course)

Relational calculus (in 1 slide) English: Name and sid of students who are taking the course DB RA: Π name,sid (Students Takes σ name= DB (Course) RC: {xname, xsid xcid xterm Students(xsid,xname) Takes(xsid,xcid) Course(xcid, DB, xterm) }

Relational calculus (in 1 slide) English: Name and sid of students who are taking the course DB RA: Π name,sid (Students Takes σ name= DB (Course) RC: {xname, xsid xcid xterm Students(xsid,xname) Takes(xsid,xcid) Course(xcid, DB, xterm) } Where are the joins?

Algebra v. Calculus Relational Algebra: More operational; very useful for representing execution plans. Relational Calculus: More declarative, basis of SQL The calculus and algebra have equivalent expressive power (Codd) A language that can express this core class of queries is called Relationally Complete