COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Similar documents
COMP 244 DATABASE CONCEPTS & APPLICATIONS

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

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

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

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

Chapter 2: Intro to Relational Model

Relational Algebra 1

EECS 647: Introduction to Database Systems

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

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

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 Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

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

Relational Query Languages: Relational Algebra. Juliana Freire

Relational Query Languages

Relational Algebra 1. Week 4

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

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

CSEN 501 CSEN501 - Databases I

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

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

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

Relational Algebra. Relational Query Languages

Relational Model and Relational Algebra

Chapter 2: Intro to Relational Model

CS2300: File Structures and Introduction to Database Systems

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

Relational Algebra for sets Introduction to relational algebra for bags

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 Calculus. Chapter Comp 521 Files and Databases Fall

Relational Algebra 1

CSCC43H: Introduction to Databases. Lecture 3

Relational Model, Relational Algebra, and SQL

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

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

Database Management System. Relational Algebra and operations

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

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

Relational Algebra. Procedural language Six basic operators

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

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

SQL: The Query Language Part 1. Relational Query Languages

Relational Calculus. Lecture 4B Kathleen Durant Northeastern University

RELATIONAL ALGEBRA AND CALCULUS

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

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

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

2.2.2.Relational Database concept

Relational Algebra and SQL

Lecture 16. The Relational Model

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

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

Review: Where have we been?

CMP-3440 Database Systems

Chapter 6 Part I The Relational Algebra and Calculus

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

MIS Database Systems Relational Algebra

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

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

Chapter 6 The Relational Algebra and Calculus

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Suan Lee

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

CSC 261/461 Database Systems Lecture 13. Fall 2017

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

Introduction Relational Algebra Operations

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

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

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

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

SQL. Chapter 5 FROM WHERE

Ian Kenny. November 28, 2017

پوهنتون کابل پوهنځی كمپيوترساینس

SQL - Data Query language

A l Ain University Of Science and Technology

The Relational Data Model. Data Model

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

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

CS 377 Database Systems

Informationslogistik Unit 4: The Relational Algebra

Database Applications (15-415)

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

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

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

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

Relational Query Languages

3. Relational Data Model 3.5 The Tuple Relational Calculus

Faloutsos - Pavlo CMU SCS /615

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

SQL: Queries, Programming, Triggers

EECS 647: Introduction to Database Systems

Databases Lectures 1 and 2

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

RELATIONAL DATA MODEL: Relational Algebra

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

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

Chapter 6: Formal Relational Query Languages

Transcription:

COMP 244 DATABASE CONCEPTS AND APPLICATIONS Relational Algebra And Calculus 1

Relational Algebra A formal query language associated with the relational model. Queries in ALGEBRA are composed using a collection of operators. 2

Selection in Relational Algebra A selection is an operation that selects a subset of the rows of a relation based on certain criteria. The σ (Sigma) operator is used as a selection operator. The σ operator uses a selection condition to specify the tuples of a relation to retain. 3

Selection in Relational Algebra Selection conditions are Boolean expressions that use logical connectives ^ (AND) and v (OR) Each ^ and v joins terms of the following forms: (attribute op constant) (attribute1 op attribute2) op is one of the comparison operators (<, <=, =,, >=, >) 4

Selection in Relational Algebra Example: Display all students whose gpa below 3.0 σ gpa < 3.0 (Students) Students sid 12345 32717 name Khalsa Ahmed age 22 21 gpa 2.9 3.2 sid 12345 77883 name Khalsa Muna age 22 22 gpa 2.9 2.5 77883 Muna 22 2.5 54900 Fatma 20 3.7 21827 Rashid 19 3.3 5

Selection Using SQL Example: Display all students whose gpa below 3.0 SELECT * FROM Students WHERE gpa < 3.0 Students sid name age gpa sid name age gpa 12345 Khalsa 22 2.9 12345 Khalsa 22 2.9 32717 Ahmed 21 3.2 77883 Muna 22 2.5 77883 Muna 22 2.5 54900 Fatma 20 3.7 21827 Rashid 19 3.3 6

Projection in Relational Algebra A projection is an operation that selects a subset of the attributes of a relation. The π (Phi) operator is used as a projection operator. 7

Projection in Relational Algebra Example: Display the name and age of all students π name, age (Students) Students sid name age gpa name age 12345 Khalsa 22 2.9 Khalsa 22 32717 Ahmed 21 3.2 Ahmed 21 77883 Muna 22 2.5 Muna 22 54900 Fatma 20 3.7 Fatma 20 21827 Rashid 19 3.3 Rashid 19 8

Projection in Relational Algebra Projection eliminates duplicate tuples Example: Display the age of all students π age (Students) Students sid name age gpa age 12345 Khalsa 22 2.9 22 32717 Ahmed 21 3.2 21 77883 Muna 22 2.5 20 54900 Fatma 20 3.7 19 21827 Rashid 19 3.3 9

Projection Using SQL Example: Display the name and age of all students SELECT FROM Students name, gpa Students sid name age gpa name gpa 12345 Khalsa 22 2.9 Khalsa 2.9 32717 Ahmed 21 3.2 Ahmed 3.2 77883 Muna 22 2.5 Muna 2.5 54900 Fatma 20 3.7 Fatma 3.7 21827 Rashid 19 3.3 Rashid 3.3 10

Combining Selection and Projection Since selection and projection in relational algebra results in a relation, we can form expressions containing any combination of them by substituting any relation name with another selection or projection 11

Combining Selection and Projection Example: Display the name and age of all students whose gpa >= 3.0 π name, age (σ gpa >= 3.0 (Students)) Using SQL: SELECT name, gpa FROM Students WHERE gpa >= 3.0 12

Sequence of Operations GPA_R σ gpa >= 3.0 (Students) Result π name, age (GPA_R) 13

Students sid name age gpa 12345 Khalsa 22 2.9 32717 Ahmed 21 3.2 name age 77883 Muna 22 2.5 Ahmed 21 54900 Fatma 20 3.7 Fatma 20 21827 Rashid 19 3.3 Rashid 19 σ gpa >= 3.0 (Students) sid 32717 54900 name Ahmed Fatma age 21 20 gpa 3.2 3.7 π name, age (σ gpa >= 3.0 (Students)) 21827 Rashid 19 3.3 14

Set Operations Set operations, such as union ( ), intersection ( ), difference ( ), and product (x) are also available in relational algebra 15

Unions Union: R S returns a new relation containing all tuples that are in relation R or relation S or both R and S must be union-compatible They have the same number of fields Corresponding fields, taken from left to right, have the same domains Field names are not important for unioncompatibility The schema of the result will be identical to the schema of R Note: Duplicate tuples will be removed 16

Union Compatibility STUDENT StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Union Compatible ADVISOR AdvisorID Name Phone 150 Yousif 369 387 Khalid 777 COURSE Union Incompatible Course- Number Title Credit- Hours COMP2101 Introduction to Computer Science 4 COMP2103 Introduction to Object Orientation 3 COMP4414 Database Management 3 17

Unions in Relational Algebra Union: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Union of Students and Advisors StudentID OR Name Phone AdvisorID 150 Yousif 369 270 Khalid 741 Advisors AdvisorID Name Phone 300 Arwa 210 387 Khalid 777 150 Yousif 369 387 Khalid 777 18

Union Operation in SQL SQL supports the Union operation using either the OR operator or the UNION operator Example: to represent Union: R shown in the previous slide: S SELECT StudentID, name, phone FROM Students UNION SELECT StudentID, name, phone FROM Students ; 19

Union Operation in SQL Example: Find the student name and course number of all students who are enrolled in either COMP151 or COMP244: SELECT FROM WHERE AND S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid (E.cid = COMP151 OR E.cid = COMP244 ) ; 20

Union Operation in SQL Another solution: SELECT FROM WHERE UNION SELECT FROM WHERE S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND E.cid = COMP151 S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND E.cid = COMP244 ; 21

Intersection: R S returns a relation containing all tuples that are in both R and S Intersection R and S must be union-compatible The schema of the result will be identical to the schema of R 22

Intersection Intersection: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Advisors AdvisorID Name Phone Intersection of Students and Advisors StudentID OR Name Phone AdvisorID 150 Yousif 369 150 Yousif 369 387 Khalid 777 23

Intersection Operation in SQL SQL supports the Intersection operation using the AND or the INTERSECTION operators 24

Intersection Operation in SQL Example: Find the name of all students who are enrolled in both COMP151 and COMP244: SELECT FROM WHERE S.name, E.cid AS course Students S, Enrollments E S.sid = E.sid AND (E.cid = COMP151 AND E.cid = COMP244 ); The above SELECT will ALWAYS return an empty answer 25

Intersection Operation in SQL The correct solution: SELECT S.name FROM Students S, Enrollments E1, Enrollments E2 WHERE (S.sid = E1.sid AND E1.cid = COMP151 ) AND (S.sid = E2.sid AND E2.cid = COMP244 ) 26

Intersection Operation in SQL Another solution: SELECT FROM WHERE INTERSECT SELECT FROM WHERE S1.name Students S1, Enrollments E1 S1.sid = E1.sid AND E1.cid = COMP150 S2.name Students S2, Enrollments E2 S2.sid = E2.sid AND E2.cid = COMP224 ; 27

Set Operations Set-Difference: R S returns a relation containing all tuples that are in R but NOT in S R and S must be union-compatible The schema of the result will be identical to the schema of R 28

Set Operations Set-Difference: R S Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Difference of Students and Advisors StudentID OR Name Phone AdvisorID 270 Khalid 741 300 Arwa 210 Advisors AdvisorID Name Phone 150 Yousif 369 387 Khalid 777 29

Cross-Product: R x S returns a relation containing All fields of R (in the same order as they appear in R), followed by All fields of S (in the same order as they appear in S), followed by A combination of the tuples of the tuples (r, s) for each pair of tuples r R, s S Set Operations 30

Set Operations Cross-Product: R x S The cross-product operation is sometimes called Cartesian product Since R and S could contain fields with the same name, the corresponding fields in R x S are unnamed. They are referred to by position 31

Set Operations Cross-Product: R x S Relations A and B do not need to be union compatible The resulting relation might contain meaningless information A X B is the same as B X A 32

Cross-Product Students StudentID Name Phone 150 Yousif 369 270 Khalid 741 300 Arwa 210 Grades Student- Number Course- Number Sem Grade 150 COMP2101 F02 B 387 COMP4414 S03 A Product of Students and Grades StudentID Name Phone Student- Number Course- Number Sem Grade 150 Yousif 369 150 COMP2101 F02 B 150 Yousif 369 387 COMP4414 S03 A 270 Khalid 741 150 COMP2101 F02 B 270 Khalid 741 387 COMP4414 S03 A 300 Arwa 210 150 COMP2101 F02 B 300 Arwa 210 387 COMP4414 S03 A 33

Relational Calculus Relational Calculus allows us to describe how data are retrieved from a database. Relational Calculus is used in many commercial query languages, such as SQL, and Query-by-Example (QBE). We will use a variant of the calculus called the Tuple Relational Calculus (TRC). TRC has influence on SQL There is another calculus variant called Domain Relational Calculus (DRC). DRC has influence on QBE 34

Syntax of TRC Queries We need the following definitions: Rel is a relation name R and S are tuple variables op is an operator in the set { <, >, =, <=, >=, } 35

Syntax of TRC Queries We define an atomic formula to take any of the following forms: R Rel R.a op S.b R.a op constant, or constant op R.a 36

Syntax of TRC Queries A TRC formula can be any of the following forms: Any atomic formula p, R ( R ( p q, p( R)), p( R)), p q, or p q where Ris atuple where Ris atuple var var iable iable 37

Syntax of TRC Queries A TRC Query is an expression in the following form: { T p(t) } T is a tuple variable p(t) is a formula 38

Syntax of TRC Queries Example: Find all students who are enrolled in COMP244: { P S Students, E Enrollments ( S. sid E. sid ^ cid ' COMP244')} 39

Syntax of DRC Queries DRC queries have similar syntax to that of TRC. Example: Find the name and gpa of all students who are enrolled in COMP244: {( N, G) S, A(( S, N, A, G) Students ^ ( Se, Ce, Ge) Enrollments ( S Se ^Ce ' COMP244'))} 40

Syntax of DRC Queries Example: Find all students who are enrolled in COMP244: { P S Students, E Enrollments ( S. sid E. sid ^ cid ' COMP244')} 41