Other Query Languages. Winter Lecture 11

Similar documents
Other Query Languages II. Winter Lecture 12

Chapter 3: SQL. Chapter 3: SQL

Chapter 3: SQL. Database System Concepts, 5th Ed. Silberschatz, Korth and Sudarshan See for conditions on re-use

Silberschatz, Korth and Sudarshan See for conditions on re-use

CSIE30600/CSIEB0290 Database Systems Relational Algebra and Calculus 2

Chapter 5: Other Relational Languages.! Query-by-Example (QBE)! Datalog

Outline. CSIE30600 Database Systems Relational Algebra and Calculus 2

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Chapter 5: Other Relational Languages

DATABASE DESIGN I - 1DL300

DATABASE TECHNOLOGY. Spring An introduction to database systems

Chapter 2: Relational Model

Database System Concepts, 5 th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Chapter 3: Relational Model

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

Other Relational Query Languages

Chapter 6: Formal Relational Query Languages

Simple SQL Queries (2)

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

Upon completion of this Unit, students will be introduced to the following

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

UNIT 2 RELATIONAL MODEL

CMSC 424 Database design Lecture 18 Query optimization. Mihai Pop

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

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Contents Contents Introduction Basic Steps in Query Processing Introduction Transformation of Relational Expressions...

Chapter 14: Query Optimization

INF1383 -Bancos de Dados

The SQL database language Parts of the SQL language

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

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

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

CS352 - DATABASE SYSTEMS. To give you experience with developing a database to model a real domain

CSCC43H: Introduction to Databases. Lecture 3

Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations Views Modification of the Database Data Definition

CS352 - DATABASE SYSTEMS

UNIT- II (Relational Data Model & Introduction to SQL)

Database System Concepts

Outline. Note 1. CSIE30600 Database Systems More SQL 2

CS122 Lecture 5 Winter Term,

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Chapter 14 Query Optimization

Database Systems SQL SL03

SQL QUERIES. CS121: Relational Databases Fall 2017 Lecture 5

CPS 510 Data Base I. Query: find all SNRs whose scodes are greater than some SNRs whose name is CB.

Lecture 1: Conjunctive Queries

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

Database Systems SQL SL03

Chapter 2: Intro to Relational Model

Chapter 4: SQL. Basic Structure

Other Relational Languages

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

Ch 5 : Query Processing & Optimization

QQ Group

DATABASE DESIGN II - 1DL400

Database System Concepts

Data Integration: Logic Query Languages

Comp 5311 Database Management Systems. 2. Relational Model and Algebra

Relational Calculus. Lecture 4B Kathleen Durant Northeastern University

1. Considering functional dependency, one in which removal from some attributes must affect dependency is called

1. The rst database systems were based on the network and hierarchical models. These are covered briey

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Database Systems. Answers

3. Relational Data Model 3.5 The Tuple Relational Calculus

Chapter 12: Query Processing

1. (a) Explain the Transaction management in a database. (b) Discuss the Query Processor of Database system structure. [8+8]

Informationslogistik Unit 4: The Relational Algebra

Operations of Relational Algebra

Query Optimization. Shuigeng Zhou. December 9, 2009 School of Computer Science Fudan University

DATABASE TECHNOLOGY - 1MB025

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

CSCC43H: Introduction to Databases. Lecture 4

DATABASTEKNIK - 1DL116

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

Chapter 2: Intro to Relational Model

Relational Model. Prepared by: Ms. Pankti Dharwa ( Asst. Prof, SVBIT)

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Views, assertions, and triggers

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

2.2.2.Relational Database concept

Chapter 8: Relational Algebra

DATABASE TECHNOLOGY - 1MB025

ADVANCED SQL DDL. CS121: Relational Databases Fall 2017 Lecture 10

Relational Databases

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

Database Management System 11

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Chapter 2: Relational Model

Database Systems CSE Comprehensive Exam Spring 2005

DBMS: AN INTERACTIVE TUTORIAL

DATABASE TECHNOLOGY - 1MB025 (also 1DL029, 1DL300+1DL400)

Lecture Notes for 3 rd August Lecture topic : Introduction to Relational Model. Rishi Barua Shubham Tripathi

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

First lecture of this chapter is in slides (PPT file)

Relational Calculus. Chapter Comp 521 Files and Databases Fall

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Relational Databases Fall 2017 Lecture 1

COURSE OVERVIEW THE RELATIONAL MODEL. CS121: Introduction to Relational Database Systems Fall 2016 Lecture 1

Introduction Alternative ways of evaluating a given query using

Domain Constraints Referential Integrity Assertions Triggers. Authorization Authorization in SQL

Transcription:

Other Query Languages Winter 2006-2007 Lecture 11

Query Languages Previously covered relational algebra A procedural query language Foundation of the Structured Query Language (SQL) Supports wide variety of queries, inserts, updates, deletes Two other formal query languages: Tuple relational calculus Domain relational calculus Two language implementations based on relational calculus Nowhere near the popularity of SQL, though

Tuple Relational Calculus Entirely declarative! Queries have the form: { t P(t) } The set of all tuples t, such that P(t) is true As before, t[a] refers to t s value for attribute A Example query: Find the branch name, loan number, and amount of all loans over $1200. { t t loan t[amount] > 1200 } The set of all tuples t, such that t appears in the loan relation, and t s amount is over $1200. Schema has same attributes that loan relation has.

Selecting Loan Numbers If we only want loan numbers: Schema of t should be (loan_number) But, now t doesn t appear in loan relation Introduce another tuple variable: s loan Updated query: { t s loan ( t[loan_number] = s[loan_number] s[amount] > 1200 ) } The set of all tuples t, such that there exists a tuple s in the loan relation with the same loan number as t, and a balance of more than $1200. No relation associated with t, and only loan_number attribute is referenced.

Joining Relations Another query: Find names of all customers with a loan from the Perryridge branch. Relations: loan(loan_number, branch_name, amount) borrower(customer_name, loan_number) Need to join relations together to compute the result Join loan and borrower using loan_number Use tuple variables to specify join constraints

Joining Relations (2) Query: { t s borrower ( t[customer_name] = s[customer_name] u loan ( u[loan_number] = s[loan_number] u[branch_name] = Perryridge )) } The set of all tuples t, such that t refers to a valid borrower name, and the borrower has at least one loan at the Perryridge branch. Use var relation to perform join and subquery kinds of operations Tuple variables s, u allow the borrower and loan relations to be correlated with each other, and with t

Set Operations Set operations are easy in the tuple relational calculus Example: Find all customers that have an account or a loan. In relational algebra, used set-union operation: ( In tuple relational calculus: { t s borrower ( t[customer_name] = s[customer_name] ) u depositor ( t[customer_name] = u[customer_name] ) } This defines a relation, so if customer is in borrower and depositor, they only appear once in result.

Set Operations (2) Find customers with both an account and a loan. Just change to : { t s borrower ( t[customer_name] = s[customer_name] ) u depositor ( t[customer_name] = u[customer_name] ) } Find customers with an account but not a loan. Again, very simple to construct: { t s depositor (t[customer_name] = s[customer_name]) Ÿ u borrower (t[customer_name] = u[customer_name]) } Result contains customer names that appear in depositor relation, but not in borrower relation

For All Queries Another query: Find all customers who have an account at all branches located in Brooklyn. In relational algebra, used divide operation In tuple relational calculus, can use for this t r (Q(t)) Q is true for all tuples t in relation r. Can also use implication: P Q means Q ŸP

For All Queries (2) Query: { t r customer ( r[customer_name] = t[customer_name] ) u branch ( u[branch_city] = Brooklyn s depositor ( t[customer_name] = s[customer_name] w account ( w[account_number] = s[account_number] w[branch_name] = u[branch_name] )))) } The set of all customer names, such that for all branches located in Brooklyn, there exists a tuple in depositor for that customer with a corresponding account located at that branch. What if there are no branches in Brooklyn? Must restrict result to only valid customer names

Formal Definitions Expressions are of the form { t P(t) } P is some formula Can contain tuple variables A tuple variable is said to be free if not quantified by or Tuple variables constrained by or are bound Example: t loan s branch ( t[branch_name] = s[branch_name] ) t is a free tuple variable s is a bound tuple variable

Formal Definitions (2) Formulas are built out of atoms Valid atoms: s r ( not allowed) s[x] Θ u[y] Comparison of two attributes Θ is a comparison operator: < = > s[x] Θ c Comparison to some constant value Any atom is also a formula

Formal Definitions (3) Compositions of atoms: If P 1 is a formula, then so are ŸP 1 and (P 1 ) If P 1 and P 2 are formulae, then so are: P 1 P 2 P 1 P 2 P 1 P 2 If P 1 (s) is a formula containing a free tuple variable s, and r is a relation, then so are: s r (P 1 (s)) s r (P 1 (s))

Safety of Expressions Does tuple relational calculus have any issues? Yes: Can write an expression that generates an infinite relation! { t Ÿ(t loan) } Result contains an infinite set of tuples Such expressions are said to be unsafe Must constrain ourselves to safe expressions Define rules to indicate what expressions are safe

Safety of Expressions (2) Every tuple relational formula P has a domain The set of all values referenced by P Includes values explicitly stated in P Also includes values in relations used in P Denoted as dom(p) Example: dom(t loan t[amount] > 1200) Set containing 1200, and all values appearing in loan An expression { t P(t) } is safe if all values in result are also in dom(p) { t Ÿ(t loan) } is not safe because it contains values outside of loan relation

Tuple Relational Calculus Same expressive power as relational algebra, when limited to safe expressions Doesn t have extended relational operators Purely declarative nature makes it more appealing, in some ways Can unsafe expressions be written in relational algebra? No! Operations generate results only from tuples in existing relations.

Datalog Datalog is a query language based on relational calculus Based on logic-programming language Prolog Syntax is very similar to Prolog Datalog is purely declarative Programs consist of a set of rules Order is irrelevant Specific constraints are enforced to ensure only safe expressions Largely constrained to research project usage

Example Datalog Statements A rule to produce account numbers and balances for accounts at Perryridge branch with a balance over $700 v1(a, B) :- account(a, Perryridge, B), B > 700 account(account_number, branch_name, balance) Can be read as: for all A, B if (A, Perryridge, B) account and B > 700 then (A, B) v1 Defines a view relation v1 is a view that references account relation

Example Datalog Statements (2) Can issue queries against v1 To find balance of account A-217:? v1( A-217, B) Produces ( A-217, 750) To find accounts with a balance over $800? v1(a, B), B > 800 for all A, B if (A, B) v1 and B > 800 then (A, B) is in the result Produces one result: ( A-201, 900) Variables and values are positional Attribute names are omitted

Example Datalog Statements (3) Often need multiple rules to define view relation Example: Define a view relation that specifies the interest rate for every account. interest_rate(a, 5) :- account(a, N, B), B < 10000 interest_rate(a, 6) :- account(a, N, B), B >= 10000 Accounts with a balance of less than $10,000 have a 5% interest rate Accounts with a balance of $10,000 or more have an interest rate of 6%

Relational Operations in Datalog Cartesian product of r 1 and r 2 : query(x 1,, X n, Y 1,, Y m ) :- r 1 (X 1,, X n ), r 2 (Y 1, Y m ) Set union of r 1 and r 2 : query(x 1,, X n ) :- r 1 (X 1,, X n ) query(x 1,, X n ) :- r 2 (X 1,, X n ) Set difference of r 1 and r 2 : query(x 1,, X n ) :- r 1 (X 1,, X n ), not r 2 (X 1,, X n )

Recursive Datalog Datalog also has recursive query capabilities Powerful mechanism for navigating hierarchical data Example: Define a view relation for all employees directly or indirectly managed by a particular manager. empl(x, Y) :- manager(x, Y) empl(x, Y) :- manager(x, Z), empl(z, Y) To find all employees managed by Jones: empl(x, Jones )

Datalog Summary Datalog provides very succinct statement of queries Declarative nature facilitates optimized execution Features like recursive queries are very powerful Similar concept being incorporated into SQL Unfortunately relegated primarily to research use