Query-by-Example (QBE) QBE: Intro. `Example Tables in QBE. Reserves sid bid day. Boats bid bname color. Sailors sid sname rating age.

Similar documents
Query-by-Example (QBE)

QUERY-BY-EXAMPLE (QBE)

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

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

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

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

SQL. Chapter 5 FROM WHERE

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

SQL: Queries, Programming, Triggers

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

Database Management Systems. Chapter 5

SQL: Queries, Constraints, Triggers

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

CIS 330: Applied Database Systems

Introduction to Data Management. Lecture 14 (SQL: the Saga Continues...)

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

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

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

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

Database Applications (15-415)

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

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

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

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

Basic form of SQL Queries

Database Applications (15-415)

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

Relational Calculus. Chapter Comp 521 Files and Databases Fall

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

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

Enterprise Database Systems

Evaluation of Relational Operations: Other Techniques

SQL Part 2. Kathleen Durant PhD Northeastern University CS3200 Lesson 6

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

Relational Calculus. Chapter Comp 521 Files and Databases Fall

Experimenting with bags (tables and query answers with duplicate rows):

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL

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

SQL: Queries, Programming, Triggers

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

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

The Database Language SQL (i)

Database Systems. October 12, 2011 Lecture #5. Possessed Hands (U. of Tokyo)

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Database Systems ( 資料庫系統 )

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

HW1 is due tonight HW2 groups are assigned. Outline today: - nested queries and witnesses - We start with a detailed example! - outer joins, nulls?

Keys, SQL, and Views CMPSCI 645

Relational Algebra. Relational Query Languages

SQL: Queries, Constraints, Triggers (iii)

MIS Database Systems Relational Algebra

Relational Query Languages

CSC Web Programming. Introduction to SQL

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

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

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

Relational Query Optimization. Highlights of System R Optimizer

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Database Management Systems. Chapter 3 Part 2

SQL - Lecture 3 (Aggregation, etc.)

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

Examples of Physical Query Plan Alternatives. Selected Material from Chapters 12, 14 and 15

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

Evaluation of Relational Operations

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

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Evaluation of Relational Operations: Other Techniques

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

SQL Lab Query 5. More SQL Aggregate Queries, Null Values. SQL Lab Query 8. SQL Lab Query 5. SQL Lab Query 8. SQL Lab Query 9

Database Architectures

Evaluation of Relational Operations. Relational Operations

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

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

COMP 244 DATABASE CONCEPTS & APPLICATIONS

MySQL Installation Guide (Windows)

Database Management Systems. Chapter 3 Part 1

Database Management System. Relational Algebra and operations

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Database Applications (15-415)

Database Management Systems,

CompSci 516: Database Systems

Indexing. Chapter 8, 10, 11. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Chapter 5: Other Relational Languages

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Principles of Data Management. Lecture #12 (Query Optimization I)

MySQL Installation Guide (Windows)

8. An example row consists of constants and example elements which are really domain variables.

Final Review. Zaki Malik November 20, 2008

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

Operations of Relational Algebra

Midterm Exam #2 (Version A) CS 122A Winter 2017

CSIT5300: Advanced Database Systems

CMP-3440 Database Systems

QUERIES, PROGRAMMING, TRIGGERS

Part I: Structured Data

Relational Algebra 1

Evaluation of relational operations

Evaluation of Relational Operations

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

Transcription:

Query-by-Example (QBE) Online Chapter Example is the school of mankind, and they will learn at no other. -- Edmund Burke (1729-1797) Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 1 QBE: Intro A GUI for expressing queries. Based on the DRC! Actually invented before GUIs. Very convenient for simple queries. Awkward for complex queries. QBE an IBM trademark. But has influenced many projects Especially PC Databases: Paradox, Access, etc. Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 2 `Example Tables in QBE Users specify a query by filling in example tables, or skeletons; we will use these skeletons in our examples. Boats bid bname color Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 3

Basics To print names and ages of all sailors: P._N P._A Print all fields for sailors with rating > 8, in ascending order by (rating, age): P. AO(1). >8 AO(2). QBE puts unique new variables in blank columns. Above query in DRC (no ordering): { I, N, T, A I, N, T, A Sailors T > 8} Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 4 Note: MiniQBE And/Or Queries uses a slightly different syntax! Names of sailors younger than 30 or older than 20: P. < 30 P. > 20 Names of sailors younger than 30 and older than 20: _Id P. < 30 _Id P. > 20 Names of sailors younger than 30 and rating > 4: _Id P. > 4 < 30 Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 5 Duplicates Single row with P: Duplicates not eliminated by default; can force elimination by using UNQ. UNQ. P. < 30 Multiple rows with P: Duplicates eliminated by default! Can avoid elimination by using ALL. ALL. _Id P. < 30 _Id P. > 20 Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 6

Join Queries Names of sailors who ve reserved a boat for 8/24/96 and are older than 25 (note that dates and strings with blanks/special chars are quoted): > 25 _Id 8/24/96 Joins accomplished by repeating variables. Note: MiniQBE uses double quotes Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 7 Join Queries (Contd.) Colors of boats reserved by sailors who ve reserved a boat for 8/24/96 and are older than 25 : _Id _S > 25 _Id _B 8/24/96 Boats bid bname color _B Interlake P. Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 8 Join Queries (Contd.) Names and ages of sailors who ve reserved some boat that is also reserved by the sailor with sid = 22: _Id P. P. 22 _B _Id _B Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 9

Unnamed Columns MiniQBE allows P. in multiple tables Useful if we want to print the result of an expression, or print fields from 2 or more relations. QBE allows P. to appear in at most one table! _Id P. _R _A P._D P.(_R/_A) _Id _D Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 10 Negative Tables Can place a negation marker in the relation column: _Id _B Note: Variables appearing in a negated table must also appear in a positive table! MiniQBE uses NOT or ~. Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 11 Aggregates QBE supports AVG, COUNT, MIN, MAX, SUM None of these eliminate duplicates, except COUNT Also have AVG.UNQ. etc. to force duplicate elimination _Id G. G.P.AO _A P.AVG._A The columns with G. are the group-by fields; all tuples in a group have the same values in these fields. The (optional) use of.ao orders the answers. Every column with P. must include G. or an aggregate operator. Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 12

Conditions Box Used to express conditions involving 2 or more columns, e.g., _R/_A > 0.2. Can express a condition that involves a group, similar to the HAVING clause in SQL: G.P. _A AVG._A > 30 Express conditions involving AND and OR: P. _A 20 < _A AND _A < 30 Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 13 Find sailors who ve reserved all boats A division query; need aggregates (or update operations, as we will see later) to do this in QBE. P.G._Id _Id _B1 Boats bid bname color _B2 COUNT._B1= COUNT._B2 How can we modify this query to print the names of sailors who ve reserved all boats? Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 14 Inserting Tuples Single-tuple insertion: I. 74 Janice 7 14 Inserting multiple tuples (rating is null in tuples inserted below): I. _Id _N _A Students sid name login age _Id _N _A _A > 18 OR _N LIKE C% Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 15

Delete and Update Delete all reservations for sailors with rating < 4 _Id < 4 D. _Id Increment the age of the sailor with sid = 74 74 U._A+1 Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 16 Restrictions on Update Commands Cannot mix I., D. and U. in a single example table, or combine them with P. or G. Cannot insert, update or modify tuples using values from fields of other tuples in the same table. Example of an update that violates this rule: john _A joe U._A+1 Should we update every Joe s age? Which John s age should we use? Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 17 Find sailors who ve reserved all boats (Again!) We want to find sailors _Id such that there is no boat _B that is not reserved by _Id: Boats bid bname color _B _Id _B Illegal query! Variable _B does not appear in a positive row. In what order should the two negative rows be considered? (Meaning changes!) Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 18

A Solution Using Views Find sailors who ve not reserved some boat _B: BadSids sid I. _Id Boats bid bname color _B _Id _B Next, find sailors not in this `bad set: BadSids sid _Id Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 19 A Peek at MS Access Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 20 Summary QBE is an elegant, user-friendly query language based on DRC. It is quite expressive (relationally complete, if the update features are taken into account). Simple queries are especially easy to write in QBE, and there is a minimum of syntax to learn. Has influenced the graphical query facilities offered in many products, including Borland s Paradox and Microsoft s Access. Database Management Systems 3ed, Online chapter, R. Ramakrishnan and J. Gehrke 21