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

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

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

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

EECS 647: Introduction to Database Systems

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

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

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

Relational Model and Relational Algebra

CSC 261/461 Database Systems Lecture 13. Fall 2017

Lecture 16. The Relational Model

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

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

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

Relational Query Languages

Relational Algebra 1

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

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

2.2.2.Relational Database concept

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

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)

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

Relational Algebra. Relational Query Languages

Relational Algebra 1

EECS 647: Introduction to Database Systems

Announcements (September 21) SQL: Part III. Triggers. Active data. Trigger options. Trigger example

EECS 647: Introduction to Database Systems

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

Relational Algebra 1. Week 4

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

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

Rela+onal Algebra. Rela+onal Query Languages. CISC437/637, Lecture #6 Ben Cartere?e

Relational Algebra for sets Introduction to relational algebra for bags

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

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

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

Database Applications (15-415)

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

Chapter 2 The relational Model of data. Relational algebra

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

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

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

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

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

Chapter 2: The Relational Algebra

MIS Database Systems Relational Algebra

CSCC43H: Introduction to Databases. Lecture 3

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

Introduction to Data Management. Lecture #13 (Relational Calculus, Continued) It s time for another installment of...

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

Chapter 2: The Relational Algebra

Faloutsos - Pavlo CMU SCS /615

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

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

CMP-3440 Database Systems

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

CSEN 501 CSEN501 - Databases I

Chapter 2: Intro to Relational Model

Chapter 6 Formal Relational Query Languages

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

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

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

Database Applications (15-415)

SQL - Data Query language

EECS 647: Introduction to Database Systems

Databases-1 Lecture-01. Introduction, Relational Algebra

Database Theory VU , SS Introduction: Relational Query Languages. Reinhard Pichler

Relational Query Languages

Database Management Systems. Chapter 3 Part 1

CS2300: File Structures and Introduction to Database Systems

Review: Where have we been?

Relational Model 2: Relational Algebra

The Relational Model. Suan Lee

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

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

Informationslogistik Unit 4: The Relational Algebra

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

Relational Calculus. Lecture 4B Kathleen Durant Northeastern University

Data! CS 133: Databases. Goals for Today. So, what is a database? What is a database anyway? From the textbook:

Relational Database Design Part I. Announcement. Relational model: review. CPS 116 Introduction to Database Systems

Databases Lectures 1 and 2

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

Relational Algebra and SQL. Basic Operations Algebra of Bags

Relational Algebra. Algebra of Bags

Plan for today. Query Processing/Optimization. Parsing. A query s trip through the DBMS. Validation. Logical plan

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

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

1 Relational Data Model

Relational Query Languages: Relational Algebra. Juliana Freire

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

Database Technology Introduction. Heiko Paulheim

Database Management System. Relational Algebra and operations

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Introduction to Data Management. Lecture #14 (Relational Languages IV)

ARTICLE RELATIONAL ALGEBRA

Transcription:

Announcements Relational Model & Algebra CPS 216 Advanced Database Systems Lecture notes Notes version (incomplete) available in the morning on the day of lecture Slides version (complete) available after the lecture We are working on installing IBM DB2! Help needed Good learning experience Reminder: check CourseInfo for announcements! 2 Relational data model A database is a collection of relations (or tables) Each relation has a list of attributes (or columns) Set-valued attributes not allowed Each attribute has a domain (or type) Each relation contains a set of tuples (or rows) Duplicates not allowed Simplicity is a virtue! 3 Ordering of rows doesn t matter (even though the output is always in some order) Example Enroll Course CID title CPS 216 Advanced Database Systems CPS 130 Analysis of Algorithms CPS 214 Computer Networks 857 CPS 216 857 CPS 130 456 CPS 214 4 Schema versus instance Schema (metadata) Specification of how data is to be structured logically Defined at set-up Rarely changes Instance Content Changes rapidly, but always conforms to the schema Compare to types and variables in a programming language 5 Example Schema (SID integer, name string, age integer, GPA float) Course (CID string, title string) Enroll (SID integer, CID integer) Instance { <142, Bart, 10, 2.3>, <123, Milhouse, 10, 3.1>,...} { <CPS 216, Advanced Database Systems>,...} { <142, CPS 216>, <142, CPS 214>,...} 6 1

Relational algebra operators RelOp RelOp Core set of operators: Selection, projection, cross product, union, difference, and renaming Additional, derived operators: Selection Input: a table R Notation: σ p ( R ) p is called a selection condition/predicate Purpose: filter rows according to some criteria Output: same columns as R, but only rows of R that satisfy p Join, natural join, intersection, etc. 7 8 Selection example s with GPA higher than 3.0 σ GPA > 3.0 ( ) σ GPA > 3.0 9 More on selection Selection predicate in general can include any column of R, constants, comparisons such as =,, etc., and Boolean connectives,, and Example: straight A students under 18 or over 21 σ GPA 4.0 (age < 18 age > 21) ( ) But you must be able to evaluate the predicate over a single row Example: student with the highest GPA? σ GPA = max(gpa in table) ( ) 10 Projection Projection example Input: a table R Notation: π L ( R ) L is a list of columns in R Purpose: select columns to output Output: same rows, but only the columns in L IDs and names of all students π SID, name ( ) π SID,name 11 12 2

More on projection Duplicate output rows must be removed Example: age distribution of students π age ( ) π age Cross product Notation: R S Purpose: pairs rows from two tables Output: for each row r in R and each row s in S, output a row rs (concatenation of r and s) 13 14 Cross product example Derived operator: join Enroll 15 Notation: R p S p is called a join condition/predicate Purpose: related rows from two tables according to some criteria Output: for each row r in R and each row s in S, output a row rs (concatenation of r and s) if r and s satisfy p Shorthand for σ p (R S) 16 Join example Derived operator: natural join Info about students, plus CIDs of their courses.sid = Enroll.SID Enroll.SID = Enroll.SID 17 Notation: R S Purpose: related rows from two tables, and Enforce equality on all common attributes Eliminate one copy of common attributes Shorthand for π L (R p S) L is the union of the attributes from R and S, with duplicates removed p matches all attributes common to R and S 18 3

Natural join example Enroll = π? (? Enroll) = π SID, name, age, GPA, CID (.SID = Enroll.SID Enroll).SID = Enroll.SID 19 Union Notation: R S R and S must have identical schema Output: Has the same schema as R and S Contains all rows in R and all rows in S, with duplicates eliminated 20 Difference Notation: R S R and S must have identical schema Output: Has the same schema as R and S Contains all rows in R that are not found in S Derived operator: intersection Notation: R S R and S must have identical schema Output: Has the same schema as R and S Contains all rows that are in both R and S Shorthand for R (R S) Also equivalent to S (S R) and R a` S 21 22 Renaming Renaming example Input: a table R Notation: ρ S ( R ), or ρ S ( A1, A 2,...) ( R ) Purpose: rename a table and/or its columns Output: a renamed table with the same rows as R Used to Avoid confusion caused by identical column names Create identical columns names for natural joins All pairs of (different) students?.sid < >.SID SID1 < > SID2 ρ 1 (SID1, name1, age1, GPA1) ρ 2 (SID2, name2, age2, GPA2) 23 24 4

Summary of core operators Selection: σ p ( R ) Projection: π L ( R ) Cross product: R S Union: R S Difference: R S Renaming: ρ S ( A1, A 2,...) ( R ) Doesn t really add to expressive power Summary of derived operators Join: R ba p S Natural join: R ba S Intersection: R S Many more Semi-join, anti-semi-join, quotient,... 25 26 An exercise CIDs of the courses that Lisa isn t taking All CIDs π CID Course Who s Lisa? σ name = Lisa" π CID CIDs of the courses that Lisa is taking Enroll 27 A trickier exercise Who has the highest GPA? Who does not have the highest GPA? Whose GPA is lower than somebody else s? A deeper question: When (and why) is required? π 1.SID ρ 1 π 1.SID 1.GPA < 2.GPA ρ 2 28 Add more rows to the input... Monotone operators RelOp What happens to the output? If some old output rows must be removed Then the operator is non-monotone Otherwise the operator is monotone That is, old output rows remain correct when more rows are added to the input Formally, R R RelOp( R ) RelOp( R ) 29 Classification of relational operators Monotone Non-monotone Selection: σ p ( R ) Projection: π L ( R ) Cross product: R S Union: R S Difference: R S (Not with respect to S) 30 5

Why is needed for highest GPA? Composition of monotone operators produces a monotone query Old output rows remain correct when more rows are added to the input Highest-GPA query is non-monotone Current highest GPA is 4.3 Add another GPA 4.5 Old answer is invalidated So it must use difference! 31 Why do we need core operator X? Difference The only non-monotone operator Cross product The only operator that allows you to add columns Union The only operator that allows you to add rows? A more rigorous proof? Selection? Projection? Homework problem :-) 32 Why is r.a. a good query language? Declarative? Yes, compared to older languages like CODASYL But operators are inherently procedural Simple A small set of core operators whose semantics are easy to grasp Complete? With respect to what? 33 Relational calculus { s.sid (s) ( s : (s ) s.gpa < s.gpa) } Relational algebra = safe relational calculus Every query expressible in relational algebra is also expressive as a safe relational calculus formula And vice versa Example of an unsafe relational calculus query { s.name (s) } Can t evaluate this query just by looking at the database 34 Turing machine? Relational algebra has no recursion Example of something not expressible in relational algebra: Given relation Parent ( parent, child ), who are Bart s ancestors? Why not recursion? Optimization becomes undecidable You can always implement it at the application level Recursion is added to SQL nonetheless Next time How to design a relational database (and the theory behind it) No required reading, but for new comers to the field, reading related sections in a textbook is recommended See Tentative Syllabus on course Web page 35 36 6