Examples of DIVISION RELATIONAL ALGEBRA and SQL. r s is used when we wish to express queries with all :



Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Relational Division and SQL

1 Structured Query Language: Again. 2 Joining Tables

ICAB4136B Use structured query language to create database structures and manipulate data

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

3. Relational Model and Relational Algebra

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?


Introduction to database design

Introduction to SQL: Data Retrieving

Relational Database: Additional Operations on Relations; SQL

Performance Tuning for the Teradata Database

Relational Databases

SQL: Queries, Programming, Triggers

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

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

SQL: Queries, Programming, Triggers

Chapter 5. SQL: Queries, Constraints, Triggers

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Advance DBMS. Structured Query Language (SQL)

CSC 443 Data Base Management Systems. Basic SQL

A Brief Introduction to MySQL

The process of database development. Logical model: relational DBMS. Relation

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

What is Null and Why is it Important for Crystal Reports

Advanced Query for Query Developers

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

WRITING EFFICIENT SQL. By Selene Bainum

In this Lecture SQL SELECT. Example Tables. SQL SELECT Overview. WHERE Clauses. DISTINCT and ALL SQL SELECT. For more information

The Query Builder: The Swiss Army Knife of SAS Enterprise Guide

CHAPTER 6 DATABASE MANAGEMENT SYSTEMS. Learning Objectives

COLLEGE ALGEBRA 10 TH EDITION LIAL HORNSBY SCHNEIDER 1.1-1

SQL JOINs and VIEWs. Chapter 3.3 V3.0. Napier University Dr Gordon Russell

Introduction to Microsoft Jet SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Effective Use of SQL in SAS Programming

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

The Relational Algebra

MySQL for Beginners Ed 3

Unit 10: Microsoft Access Queries

Relational Data Analysis I

Infinite Campus Ad Hoc Reporting Basics

Introduction to SQL C H A P T E R3. Exercises

Structured Query Language (SQL)

IT2304: Database Systems 1 (DBS 1)

Talking to Databases: SQL for Designers

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

IT2305 Database Systems I (Compulsory)

Click to create a query in Design View. and click the Query Design button in the Queries group to create a new table in Design View.

BCA. Database Management System

Handling Missing Values in the SQL Procedure


Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

STATISTICS 8, FINAL EXAM. Last six digits of Student ID#: Circle your Discussion Section:

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Tutorial on Relational Database Design

A Closer Look at PROC SQL s FEEDBACK Option Kenneth W. Borowiak, PPD, Inc., Morrisville, NC

Partitioning under the hood in MySQL 5.5

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Introduction to tuple calculus Tore Risch

Introduction to Querying & Reporting with SQL Server

Querying Microsoft SQL Server (20461) H8N61S

Introduction to SQL for Data Scientists

Where? Originating Table Employees Departments

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Querying Microsoft SQL Server 2012

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Normal forms and normalization

PROC SQL for SQL Die-hards Jessica Bennett, Advance America, Spartanburg, SC Barbara Ross, Flexshopper LLC, Boca Raton, FL

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

DIPLOMA IN WEBDEVELOPMENT

2009 Braton Groupe sarl, All rights reserved.

Using Excel As A Database

ProExtra eclaiming User Guide

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Performing Queries Using PROC SQL (1)

Displaying Data from Multiple Tables

Search help. More on Office.com: images templates

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

TECHNIQUES FOR OPTIMIZING THE RELATIONSHIP BETWEEN DATA STORAGE SPACE AND DATA RETRIEVAL TIME FOR LARGE DATABASES

Using Microsoft Access Front End to NIMSP MySQL Database

Lecture 4: More SQL and Relational Algebra

WORKING WITH SUBQUERY IN THE SQL PROCEDURE

2012 Teklynx Newco SAS, All rights reserved.

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

Oracle Database 10g: Introduction to SQL

6.830 Lecture PS1 Due Next Time (Tuesday!) Lab 1 Out today start early! Relational Model Continued, and Schema Design and Normalization

TM SysAid Chat Guide Document Updated: 10 November 2009

Execution Strategies for SQL Subqueries

Databases and Database management systems (DBMS) Contents

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Transcription:

Examples of DIVISION RELATIONAL ALGEBRA and SQL r s is used when we wish to express queries with all : Ex. Which persons have a loyal customer's card at ALL the clothing boutiques in town X? Which persons have a bank account at ALL the banks in the country? Which students are registered on ALL the courses given by Soini? Which students are registered on ALL the courses that are taught in period 1? Which boys are registered on those courses that are taken by ALL the girls? Which girls are registered on ALL the courses taken by student nr. 40101? In all these queries the description after ALL defines a set with a number of elements. The result is composed of those data units (persons, students etc.) who satisfy these requirements. The logic these sentences express is that of implication: for which persons is it true that IF there is a clothing boutique in town X THEN the person has a loyal customer's card there, IF Soini gives a course THEN the student is registered on it or IF 40101 is registered on a course THEN the girl is registered on the same course. We shall study the last example in detail here. On the one hand we have a list (a relation) with girls and the courses they are registered on: this will be called the relation r. On the other hand we have a list (a relation) of ALL the courses taken by 40101. This will be the relation s. Now we wish to know which of the girls take ALL these courses. Suppose that 40101 takes the following three courses: G555, and. To be a part of the answer the girl in question must be registered on ALL of these three. (In addition to that she may be registered on other courses not taken by 40101, these will not affect the result.) In relational algebra the query will be expressed as follows (the example database kursdatabas is found on the home page of this course. matr = student number, namn = name, kurskod = course code, kön = sex, kursanmälan = course registration): Π matr, namn, kurskod ( kön = 'K' (student) x kursanmälan) Π kurskod ( matr = 40101 (kursanmälan)) The result will be a relation with the attributes namn and matr. The attribute kurskod that we are dividing by will disappear in the division. NOTE! Which persons have passed ALL the courses they have registered on? has the same surface form, but a different logic: the set of courses that is tested will vary from one person to another, depending on which courses the current person has registered on. There is no common set of courses that should be tested for each person. The formulation in SQL will often be easier than that using division; to express the example query it is enough to create

the set of persons registered on courses and then subtract those who have NULL as the value of some course mark (vitsord, one of the attributes in kursanmalan in the sql code of the example database). Which female students take ALL the courses that 40101 is taking? Π matr, namn, kurskod ( kön = 'K' (student) x kursanmälan) Π kurskod ( matr = matr namn up kön kurskod matr kurskod ================================ =========== 40112 Brita DT K 40101 G555 40112 Brita DT K 40101 40113 Ann Helen DT K 456304 40101 40113 Ann Helen DT K 40113 Ann Helen DT K 40128 Siru DT K G555 40128 Siru DT K 40128 Siru DT K 40240 Sara IS K 456304 40240 Sara IS K 40240 Sara IS K 40101 (kursanmälan)) Π matr, namn, kurskod ( kön = 'K' (student) x kursanmälan) Π kurskod ( matr = 40101 (kursanmälan)) matr namn up kön kurskod matr kurskod ================================ =========== 40112 Brita DT K 40101 G555 40112 Brita DT K 40101 40113 Ann Helen DT K 456304 40101 40113 Ann Helen DT K 40113 Ann Helen DT K 40128 Siru DT K G555 40128 Siru DT K 40128 Siru DT K 40240 Sara IS K 456304 40240 Sara IS K 40240 Sara IS K We can see that only 40128, Siru has all the required courses.

r s is defined as Π R S (r) Π R S ((Π R S (r) s) Π R S,S (r)) r < Π matr, namn, kurskod ( kön = 'K' (student) x kursanmälan) s < Π kurskod ( matr = 40101 (kursanmälan)) Π R S (r) is here Π matr, namn (r) (No duplicate elements in sets!) Π R S,S (r) is here Π matr, namn, kurskod (r), in this case = r. The projection will guarantee that the attributes come in the right order for the subtraction. Π R S (r) s is here Π matr, namn (r) x {G555,, }, i. e. all the possible combinations of a girl registered on some course and the courses taken by 40101. From this set we shall subtract those tuples that represent real elements (actual course registrations). From what is left (the combinations representing false, unactualized information, we shall project the student number (matr) and the name (namn), the attributes represented by (Π R S ). Finally these will be subtracted from Π matr, namn (r). The answer is what is left. Π R S (r) Π R S (r) s (tänkbara) Π R S,S (r) (verkliga) ============== ===================== ===================== 40112 Brita 40112 Brita G555 40112 Brita 40113 Ann Helen 40112 Brita 40112 Brita 40128 Siru 40112 Brita 40113 Ann Helen 456304 40240 Sara 40113 Ann Helen G555 40113 Ann Helen 40113 Ann Helen 40113 Ann Helen SVAR: 40113 Ann Helen 40128 Siru G555 ===== 40128 Siru G555 40128 Siru 40128 Siru 40128 Siru 40128 Siru 40128 Siru 40240 Sara 456304 40240 Sara G555 40240 Sara 40240 Sara 40240 Sara 40240 Sara The false The tuples representing (Tuples that are not marked with candidates true information have green represent real have been stricken been stricken through. information about other courses through. than the relevant ones.)

How to translate this in MySQL? Problem: There is neither division ( ) nor set difference ( ) i MySQL, so you are obliged to find other ways around this problem. There are (at least) three ways to express these queries: 1) Not exists ( ) combined with not in ( ): ( There may not be a course that 40101 takes that is not among the courses taken by the girl in question ) select distinct matr, namn from student as R where kon = 'K' and not exists (select kurskod where matr = 40101 and kurskod not in (select kurskod as R2 where R.matr = R2.matr)); matr namn 40128 Siru 1 row in set (0.00 sec) The outmost selection will decide which columns (matr, namn) we shall see in the result. The outmost select from where will create a table including all the female students and their course registrations. These students will now be tested one by one: for the female student to be part of the result, the not exists clause for this student must be true (the list after the not exists must remain empty). What does this list consist of? First we put there all the courses that the student 40101 is registered on (3 items: G555, and ). These are represented by kurskod. Then these courses are tested one by one against the courses the current female student is registered on: a set of her courses is created in the innermost select. These are represented by kurskod. This set is thus composed of all the courses that the female student is registered on. As soon as we find a kurskod among 40101's courses that is not among the course codes of the current girl, this kurskod will part of the select list in the middle, which accordingly will no longer be empty. This in turn leads to the not exists becoming false, and that means that the female student in question does not satisfy the requirements (she does not have all the courses required) and she will not be part of the result. On the other hand, if all the course codes in the select in the middle are found in the set of the course codes taken by the current female student, this select will stay empty and the not exists will accordingly be true. Now this student will be part of the result.

The example query: 40101's courses: G555 Brita's (40112) courses: G555 Ann Helen's (40113) courses: 456304 G555 Siru's (40128) courses: G555 G555 Sara's (40240) courses: 456304 G555 Shows which course(s) are lacking in the girls' selections. These courses will be part of the selection in the middle and make not exists false. If all the courses taken by 40101 can be found among the courses chosen by the girl, she will be part of the result.

2) Two times not exists: ( There may not be a course that 40101 takes that is not taken by the current girl ) select distinct matr, namn from student as R where kon = 'K' and not exists (select kurskod as S where matr = 40101 and not exists (select kurskod as R2 where R.matr = R2.matr and S.kurskod = R2.kurskod )); matr namn 40128 Siru 1 row in set (0.01 sec) This solution is very similar to the last one, but now the test with set membership is replaced by another not exists. The outer not exists will choose the courses taken by 40101. The inner not exists will control these courses one by one; will we find the same course for the girl in question? If there is a kurskod (among the courses taken by the girl) equal to the kurskod (of 40101's courses) that we are testing at the moment, this kurskod will be the result of the innermost select. In this case this selection will not be empty, and the inner not exists will thus be false. Now the kurskod we just tested will not be a part of the selection in the middle. Each kurskod for 40101 will be tested like this. If we can find them all among the courses taken by the girl, none of them will be selected in the middle and the outer not exists will thus be true. This means that the girl in question had all the specified courses and that she will accordingly be part of the result. On the other hand, if there are one or several course codes that will not find their counterpart in the innermost selection, this selection will be empty for them, making the inner not exists true. These course codes will be a part of the select in the middle, making the outer not exists false, the girl in question (who did not have these courses) will not be a part of the result. See the figure on next page!

40101's courses Brita's (40112) courses G555 Can't be found chosen by the middle selection Ann Helen's (40113) courses G555 Can't be found chosen by the middle selection 456304 Siru's (40128) courses G555 G555 Sara's (40240) courses G555 Can't be found chosen by the middle selection 456304 Next page!

3) Count and compare The idea behind this method is to count how many different values there are in the divisor (i. e. all the values that one should have to be a part of the result). When this is applied to our example we first count how many courses 40101 takes. Then we count how many of these courses our female students take. If we get the same result, the girl in question has all the courses necessary and will appear in the result. mysql> select matr, namn natural join student where kon = 'K' and kurskod in (select kurskod where matr = 40101) group by matr having count(kurskod) = (select count(*) from (select kurskod where matr = 40101) as tab); matr namn 40128 Siru 1 row in set (0.00 sec) Here is the result of kursanmalan natural join student where kon = 'K' and kurskod in (select kurskod where matr = 40101): matr kurskod vitsord namn up kon 40112 NULL Brita DT K 40112 NULL Brita DT K 40113 NULL Ann Helen DT K 40113 NULL Ann Helen DT K 40128 NULL Siru DT K 40128 NULL Siru DT K 40128 G555 NULL Siru DT K 40240 NULL Sara IS K 40240 NULL Sara IS K 9 rows in set (0.00 sec) Next page!

These tuples are now grouped according to the student number: matr kurskod vitsord namn up kon 40112 NULL Brita DT K 40112 NULL Brita DT K 40113 NULL Ann Helen DT K 40113 NULL Ann Helen DT K 40128 NULL Siru DT K 40128 NULL Siru DT K 40128 G555 NULL Siru DT K 40240 NULL Sara IS K 40240 NULL Sara IS K having count(kurskod) counts the number of course codes/group, i. e. the number of courses each of these girls has registered on (note that in this selection we are only concerned with those courses that 40101 takes; it is quite possible that the girls are also registered on other courses in addition to these). having count(kurskod) = (select count(*) from (select kurskod where matr = 40101) as tab); compares the number of courses/group (the number of the relevant courses taken by the girl in question) with the number of courses taken by 40101 (3 courses). If we get the same number, we know that we have the same courses, and then the girl will appear in the result. This happens in the selection in the beginning of the query: mysql> select matr, namn natural join student where kon = 'K' and kurskod in (select kurskod where matr = 40101) group by matr having count(kurskod) = (select count(*) from (select kurskod where matr = 40101) as tab); OBS! Here we can only have one registration/course. In other cases we might need to use count distinct to eliminate duplicates. OBS! It would be a natural solution to use a temporary table to represent the courses taken by

40101. Unfortunately, the same temporary table may not be opened twice in MySQL 5.1, so that solution will only give you error messages.