Lecture 05. Spring 2018 Borough of Manhattan Community College

Similar documents
Institute of Southern Punjab, Multan

Relational Algebra and Relational Calculus. Pearson Education Limited 1995,

CS317 File and Database Systems

Chapter 5. Relational Algebra and Relational Calculus

CS317 File and Database Systems

CMP-3440 Database Systems

Chapter 6. SQL: SubQueries

STRUCTURED QUERY LANGUAGE (SQL)

Lecture 03. Spring 2018 Borough of Manhattan Community College

Chapter 6. SQL Data Manipulation

Lecture 03. Fall 2017 Borough of Manhattan Community College

RELATIONAL DATA MODEL

Objective. The goal is to review material covered in Chapters 1-5. Do the following questions from the book.

CMP-3440 Database Systems

Section 2.2: Introduction to the Logic of Quantified Statements

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

2.2.2.Relational Database concept

Operations of Relational Algebra

3ISY402 DATABASE SYSTEMS

CS317 File and Database Systems

ADVANCED QUERY AND VIEWS

Lecture 6 Structured Query Language (SQL)

Database Technologies. Madalina CROITORU IUT Montpellier

Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

Lecture 07. Spring 2018 Borough of Manhattan Community College

Chapter 8: The Relational Algebra and The Relational Calculus

Lecture 5 Data Definition Language (DDL)

Lecture 01. Fall 2018 Borough of Manhattan Community College

DATABASE DESIGN II - 1DL400

Lecture 09. Spring 2018 Borough of Manhattan Community College


Lecture 1: Conjunctive Queries

Relational Data Model ( 관계형데이터모델 )

Notes. Notes. Introduction. Notes. Propositional Functions. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry.

7. Relational Calculus (Part I) 7.1 Introduction

Logic and its Applications

The Relational Algebra

CMPS 277 Principles of Database Systems. Lecture #3

CMPS 277 Principles of Database Systems. Lecture #4

Chapter 3: Relational Model

Introductory logic and sets for Computer scientists

Relational Calculus. Lecture 4B Kathleen Durant Northeastern University

Computation Club: Gödel s theorem

3. Relational Data Model 3.5 The Tuple Relational Calculus

THE RELATIONAL MODEL. University of Waterloo

Database Systems. A Practical Approach to Design, Implementation, and Management. Database Systems. Thomas Connolly Carolyn Begg

An Evolution of Mathematical Tools

Cheltenham Courseware Microsoft Access 2003 Manual - Advanced Level SAMPLE

Chapter 6: Formal Relational Query Languages

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Back to the knights and knaves island

Database Technologies. Madalina CROITORU IUT Montpellier

Chapter 6 Formal Relational Query Languages

begin [atomic] operation, operation, { commit rollback} end

CSC Discrete Math I, Spring Sets

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Textbook: Chapter 6! CS425 Fall 2013 Boris Glavic! Chapter 3: Formal Relational Query. Relational Algebra! Select Operation Example! Select Operation!

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

SWEN-220 Mathematical Models of Software

CS317 File and Database Systems

Standard Query Language. SQL: Data Definition Transparencies

CS34800 Information Systems. The Relational Model Prof. Walid Aref 29 August, 2016

Efficient Testing of Database Applications

Relational Calculus: 1

UNIT 2 RELATIONAL MODEL

Chapter 5 Relational Algebra. Nguyen Thi Ai Thao

logic with quantifiers (informally)

S emistructured Data & XML

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Logic (or Declarative) Programming Foundations: Prolog. Overview [1]

8. Relational Calculus (Part II)

Propositional Calculus

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

Relational Algebra & Calculus. CS 377: Database Systems

Knowledge Representation

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Chapter 7 Relational Calculus

Normalization. { Ronak Panchal }

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

Query formalisms for relational model relational calculus

Chapter 19 Query Optimization

Th(N, +) is decidable

DATABASE THEORY. Lecture 18: Dependencies. TU Dresden, 3rd July Markus Krötzsch Knowledge-Based Systems

Relational Algebra 1

SQL Data Querying and Views

Relational Algebra and Calculus

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Discrete Mathematics Lecture 4. Harper Langston New York University

Physical Database Design

CITS2211 Discrete Structures Logic

First-Order Predicate Logic. CSCI 5582, Fall 2007

CS317 File and Database Systems

XQuery. Announcements (March 21) XQuery. CPS 216 Advanced Database Systems

Relational Algebra Part I. CS 377: Database Systems

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

== is a decent equivalence

CS 186, Fall 2002, Lecture 8 R&G, Chapter 4. Ronald Graham Elements of Ramsey Theory

Solutions to Exercises

DATABASE MANAGEMENT SYSTEMS. UNIT I Introduction to Database Systems

Transcription:

Lecture 05 Spring 2018 Borough of Manhattan Community College 1

The Relational Calculus The relational algebra is a procedural language: a certain order is always explicitly specified in a relational algebra expression and a strategy for evaluating the query is implied. The relational calculus is a nonprocedural language: there is no description of how to evaluate a query; a relational calculus query specifies what is to be retrieved rather than how to retrieve it. The relational calculus can be used to formulate the definition of a relation in terms of one or more database relations. The relational algebra and relational calculus are formally equivalent to one another: for every expression in the algebra, there is an equivalent expression in the calculus (and vice versa). 2

The Relational Calculus The relational calculus takes its name from a branch of symbolic logic called predicate calculus. A predicate is a truth-valued function with arguments. When we substitute values for the arguments, the function yields an expression, called a proposition, which can be either true or false. For example, the sentences, John White is a member of staff and John White earns more than Ann Beech are both propositions. When applied to databases, it is found in two forms: tuple relational calculus and domain relational calculus. 3

The Relational Calculus If a predicate contains a variable, as in x is a member of staff, there must be an associated range for x. When we substitute some values of this range for x, the proposition may be true; for other values, it may be false. If P is a predicate, then we can write the set of all x such that P is true for x, as: {x P(x)}. We may connect predicates by the logical connectives (AND), (OR), and ~ (NOT) to form compound predicates. 4

A) Tuple Relational Calculus In the tuple relational calculus, we are interested in finding tuples for which a predicate is true. A tuple variable is a variable that ranges over a named relation: that is, a variable whose only permitted values are tuples of the relation. For example, to specify the range of a tuple variable S as the Staff relation, we write: Staff(S). To express the query Find the set of all tuples S such that F(S) is true, we can write: {S F(S)} F is called a formula. 5

A) Tuple Relational Calculus To express the query: Find the staffno, fname, IName, position, sex, DOB, salary, and branchno of all staff earning more than 10,000 we can write: {S Staff(S) S.salary > 10000 } S.salary means the value of the salary attribute for the tuple variable S. To retrieve a particular attribute, such as salary, we would write: {S.salary Staff(S) S.salary > 10000 } 6

The existential and universal quantifiers There are two quantifiers we can use with formulae to tell how many instances the predicate applies to: The existential quantifier ( there exists ) is used in formulae that must be true for at least one instance, such as: {S Staff(S) ( B) (Branch(B) (B.branchNo = S.branchNo) B.city = London ) } This means, "There exists a Branch tuple that has the same branchno as the branchno of the current Staff tuple, S, and is located in London. The universal quantifier ( for all ) is used in statements about every instance, such as: ( B) (B.city Paris ) This means, For all Branch tuples, the address is not in Paris. 7

The existential and universal quantifiers We can apply a generalization of De Morgan s laws to the existential and universal quantifiers: ( X) (F(X)) ~( X) (~(F(X))) ( X) (F(X)) ~( X) (~(F(X))) Using these equivalence rules, we can rewrite the formula: ( B) (B.city Paris ) as: ~( B) (B.city = Paris ) which means, There are no branches with an address in Paris. Tuple variables that are qualified by or are called bound variables; the other tuple variables are called free variables. 8

Expressions and Formulae 9

A) Tuple Relational Calculus Note that in this formulation there is no indication of a strategy for executing the query. The DBMS is free to decide the operations required to fulfill the request and the execution order of these operations. On the other hand, the equivalent relational algebra formulation would be: "Select tuples from PropertyForRent such that the city is Glasgow and perform their join with the Staff relation," which has an implied order of execution. 10

A) Tuple Relational Calculus 11

12

A) Tuple Relational Calculus 13

Tuple Relational Calculus - Practice formulate the following queries in the tuple relational calculus: 1. List all book titles. 2. List details of all available books. 3. List the name and age of all users under age 18. 4. List the title and the year of all books published after 1960. 5. List all the books from New York libraries. 6. List the books that have never been rented. 7. List the users who rented any book written by the author Agatha Christie. 14

1. List all book titles. {B.title Book(B)} 2. List details of all available books. {B Book(B) B.available = 'Y'} 3. List the name and age of all users under age 18. {U.name, U.age User(U) U.age < 18} 4. List the title and the year of all books published after 1960. {B.title, B.year Book(B) B.year > 1960} 15

5. List all the books from New York libraries. {B Book(B) ( L) (Library(L) (L.libraryNo = B.libraryNo) L.city = 'New York')} 6. List the books that have never been rented. {B Book(B) ~( R) (RentBook(R) (R.bookNo = B.bookNo))} 7. List the users who rented any book written by the author Agatha Christie. {U User(U) ( R)( B) (RentBook(R) Book(B) (U.userNo = R.userNo) (R.bookNo = B.bookNo) (B.author = 'Agatha Christie'))} 16

B) Domain Relational Calculus 17

B) Domain Relational Calculus 18

B) Domain Relational Calculus 19

B) Domain Relational Calculus 20

B) Domain Relational Calculus 21

B) Domain Relational Calculus 22

Domain Relational Calculus - Practice formulate the following queries in the domain relational calculus: 1. List all book titles. 2. List the title and the library number of all available books. 3. List the names of all users under age 18. 4. List the title and the author of all books published after 1960. 5. List titles, years and library numbers of all books from New York libraries. 6. List titles and library numbers of the books that have never been rented. 7. List the users who rented any book written by the author Agatha Christie. 23

1. List all book titles. {ttl Book(bN, ttl, athr, yr, prc, avlbl, ln)} 2. List the title and the library number of all available books. {ttl, ln ( avlbl) (Book(bN, ttl, athr, yr, prc, avlbl, ln) avlbl = 'Y )} 3. List the names of all users under age 18. {name ( age) (User(uN, name, age) age < 18)} 4. List the title and the author of all books published after 1960. {ttl, athr ( yr) (Book(bN, ttl, athr, yr, prc, avlbl, ln) yr > 1960)} 24

5. List titles, years and library numbers of all books from New York libraries. {ttl, yr, ln ( ln1, cty) (Book(bN, ttl, athr, yr, prc, avlbl, ln) (Library(lN1, addrss, cty, zip) (ln = ln1) cty = 'New York')} 6. List titles and library numbers of the books that have never been rented. {ttl, ln ( bn) (Book(bN, ttl, athr, yr, prc, avlbl, ln) ~( bn1) (RentBook(bN1, dtout, dtdue, un) (bn = bn1)))} 7. List the users who rented any book written by the author Agatha Christie. {name ( uno, uno1, bno, bno1, athr) (User(uNo, name, age) RentBook(bNo, dtout, dtdue, un01) Book(bNo1, ttl, athr, yr, prc, avlbl, ln) (uno = uno1) (bno = bno1) athr = 'Agatha Christie )} 25