SQL: An Implementation of the Relational Algebra

Similar documents
Datalog. P.J. McBrien. Imperial College London. P.J. McBrien (Imperial College London) Datalog 1 / 19

SQL: A Language for Database Applications

Big Data: Pig Latin. P.J. McBrien. Imperial College London. P.J. McBrien (Imperial College London) Big Data: Pig Latin 1 / 44

OPENRULES. Tutorial. Determine Patient Therapy. Decision Model. Open Source Business Decision Management System. Release 6.0

Balancing Authority Ace Limit (BAAL) Proof-of-Concept BAAL Field Trial

INTERMEDIATE LOGIC Glossary of key terms

Excel Lesson 3 page 1 April 15

Grade 6 Math Connects Suggested Course Outline for Schooling at Home

Digital Logic Lecture 5 Boolean Algebra and Logic Gates Part I

Artificial Intelligence Prof. P. Dasgupta Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Lazy Functional Programming for a survey

Math 10 Lesson 1 4 Answers

Artificial Intelligence: Valid Arguments and Proof Systems. Prof. Deepak Khemani. Department of Computer Science and Engineering

HOW TO WRITE AN NDES POLICY MODULE

Rosen, Discrete Mathematics and Its Applications, 6th edition Extra Examples

Quorums. Christian Plattner, Gustavo Alonso Exercises for Verteilte Systeme WS05/06 Swiss Federal Institute of Technology (ETH), Zürich

Grade 7 Math Connects Suggested Course Outline for Schooling at Home 132 lessons

Module 5. Knowledge Representation and Logic (Propositional Logic) Version 2 CSE IIT, Kharagpur

Semantic Entailment and Natural Deduction

Macro Plan

Logic & Proofs. Chapter 3 Content. Sentential Logic Semantics. Contents: Studying this chapter will enable you to:

MATH1061/MATH7861 Discrete Mathematics Semester 2, Lecture 5 Valid and Invalid Arguments. Learning Goals

2.1 Review. 2.2 Inference and justifications

UC Berkeley, Philosophy 142, Spring 2016

It is One Tailed F-test since the variance of treatment is expected to be large if the null hypothesis is rejected.

INFORMATION FOR DVC MATH STUDENTS in Math 75, 110, 120, 121, 124 and 135 Distance Education Hours by Arrangement (HBA) - Summer 2010

LGCS 199DR: Independent Study in Pragmatics

Curriculum Guide for Pre-Algebra

Grade 6 correlated to Illinois Learning Standards for Mathematics

SUMMARY COMPARISON of 6 th grade Math texts approved for 2007 local Texas adoption

ALEKS ANSWER KEY PDF

Russell: On Denoting

Artificial Intelligence. Clause Form and The Resolution Rule. Prof. Deepak Khemani. Department of Computer Science and Engineering

On Truth At Jeffrey C. King Rutgers University

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Gateway Developer Guide

Georgia Quality Core Curriculum

THE MEANING OF OUGHT. Ralph Wedgwood. What does the word ought mean? Strictly speaking, this is an empirical question, about the

Houghton Mifflin MATHEMATICS

9 Knowledge-Based Systems

Williams on Supervaluationism and Logical Revisionism

Formalizing a Deductively Open Belief Space

Verification and Validation

1. Introduction Formal deductive logic Overview

Exercise Sets. KS Philosophical Logic: Modality, Conditionals Vagueness. Dirk Kindermann University of Graz July 2014

(Refer Slide Time 03:00)

Søren Kierk eg aard's Journals and Papers, Volume 1: A-E

Gesture recognition with Kinect. Joakim Larsson

RootsWizard User Guide Version 6.3.0

1 Clarion Logic Notes Chapter 4

STATISTICS FOR MISSION: JANUARY TO DECEMBER 2014

CHAPTER ONE STATEMENTS, CONNECTIVES AND EQUIVALENCES

Artificial Intelligence I

Extended Legacy Format (ELF): Date, Age and Time Microformats

Epistemic Logic I. An introduction to the course

Biometrics Prof. Phalguni Gupta Department of Computer Science and Engineering Indian Institute of Technology, Kanpur. Lecture No.

PHILOSOPHY OF LOGIC AND LANGUAGE OVERVIEW LOGICAL CONSTANTS WEEK 5: MODEL-THEORETIC CONSEQUENCE JONNY MCINTOSH

INTRODUCTION TO HYPOTHESIS TESTING. Unit 4A - Statistical Inference Part 1

SEVENTH GRADE RELIGION

St. Matthew Catholic School. 6 th Grade Curriculum Overview

Semantic Foundations for Deductive Methods

Tamer Özsu Speaks Out On journals, conferences, encyclopedias and technology

The way we convince people is generally to refer to sufficiently many things that they already know are correct.

Necessity and Truth Makers

Network-based. Visual Analysis of Tabular Data. Zhicheng Liu, Shamkant Navathe, John Stasko

Facts and Free Logic. R. M. Sainsbury

Facts and Free Logic R. M. Sainsbury

Your use of the JSTOR archive indicates your acceptance of the Terms & Conditions of Use, available at

Prompt: Explain van Inwagen s consequence argument. Describe what you think is the best response

Qualitative versus Quantitative Notions of Speaker and Hearer Belief: Implementation and Theoretical Extensions

Presuppositions (Ch. 6, pp )

FUZZY EXPERT SYSTEM IN DETERMINING HADITH 1 VALIDITY. 1. Introduction

A Quranic Quote Verification Algorithm for Verses Authentication

Summary. Background. Individual Contribution For consideration by the UTC. Date:

Where to get help. There are many ways you can get help as you gather family history information

Logicola Truth Evaluation Exercises

LDS Church Resources by Brett W. Smith

Intro Viewed from a certain angle, philosophy is about what, if anything, we ought to believe.

Basic Algorithms Overview

Josh Parsons MWF 10:00-10:50a.m., 194 Chemistry CRNs: Introduction to Philosophy, (eds.) Perry and Bratman

Contradictory Information Can Be Better than Nothing The Example of the Two Firemen

HP-35s Calculator Program Cantilever Retaining Wall Design

Homework: read in the book pgs and do "You Try It" (to use Submit); Read for lecture. C. Anthony Anderson

Is the law of excluded middle a law of logic?

Chapter 3: Basic Propositional Logic. Based on Harry Gensler s book For CS2209A/B By Dr. Charles Ling;

Intersubstitutivity Principles and the Generalization Function of Truth. Anil Gupta University of Pittsburgh. Shawn Standefer University of Melbourne

Brief Remarks on Putnam and Realism in Mathematics * Charles Parsons. Hilary Putnam has through much of his philosophical life meditated on

PROSPECTIVE TEACHERS UNDERSTANDING OF PROOF: WHAT IF THE TRUTH SET OF AN OPEN SENTENCE IS BROADER THAN THAT COVERED BY THE PROOF?

Holy Spirit. A Bible Study Course for Adults. by John F. Vogt SAMPLE. Leader s Guide

This Magic Moment: Horwich on the Boundaries of Vague Terms

1)Asher: create a handout for the week summing up LOGIC

Assignment Assignment for Lesson 3.1

COS 226 Algorithms and Data Structures Fall Midterm

GURU TEG BAHADUR PUBLIC SCHOOL SYLLABUS FOR SESSION ( ) CLASS - VII English

A New Parameter for Maintaining Consistency in an Agent's Knowledge Base Using Truth Maintenance System

CS 2104 Intro Problem Solving in Computer Science Test 1 READ THIS NOW!

Truthier Than Thou: Truth, Supertruth and Probability of Truth

Empty Names and Two-Valued Positive Free Logic

Durham Research Online

Leveraging mobile Esri technology at VDOT to move into the 21st Century. Michelle Fults, GISP Virginia DOT Matthew Kabak Esri Transportation Practice

Transcription:

: An Implementation of the Relational Algebra P.J. McBrien Imperial College London P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 1 / 40

SQL Relation Model and Algebra proposed by C.J.Codd in 1970 IBM deleveped a prototype relational database called System R with a query language Structured English Query Language (SEQUEL) SEQUEL later renamed SQL Various commericial versions of SQL launched in late 1970 s/early 1980s DB2 Oracle P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 2 / 40

SQL Relation Model and Algebra proposed by C.J.Codd in 1970 IBM deleveped a prototype relational database called System R with a query language Structured English Query Language (SEQUEL) SEQUEL later renamed SQL Various commericial versions of SQL launched in late 1970 s/early 1980s DB2 Oracle SQL Language Components Data Definition Language (DDL): a relational schema with data Data Manipulation Language (DML): a relational query and update language P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 2 / 40

RM to SQL DDL SQL DML: Definition of Tables CREATE TABLE branch ( sortcode INTEGER NOT NULL, bname VARCHAR(20) NOT NULL, cash DECIMAL(10,2) NOT NULL ) branch sortcode bname cash CREATE TABLE account ( INTEGER NOT NULL, type VARCHAR(8) NOT NULL, cname VARCHAR(20) NOT NULL, rate DECIMAL(4,2) NULL, sortcode INTEGER NOT NULL ) account type cname rate sortcode P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 3 / 40

RM to SQL DDL SQL DML: SQL Data Types SQL Data Types Keyword Semantics BOOLEAN A logical value (TRUE, FALSE, or UNKNOWN) BIT 1 bit integer (0, 1, or NULL) INTEGER 32 bit integer REAL 32 bit floating point number FLOAT(n) An n bit mantissa floating point number DECIMAL(p,s) A p digit number with s digits after the decimal point CHAR(n) A fixed length string of upto n characters VARCHAR(n) A varying length string of upto n characters DATE A calendar date (day, month and year) TIME A time of day (seconds, minutes, hours) TIMESTAMP time and day together ARRAY A fixed length list of a certain datatype MULTISET A variable sized bag of a certain datatype XML XML text P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 4 / 40

RM to SQL DDL SQL DML: Definition of Keys branch CREATE TABLE branch sortcode bname cash ( sortcode INTEGER NOT NULL, bname VARCHAR(20) NOT NULL, cash DECIMAL(10,2) NOT NULL, CONSTRAINT branch pk PRIMARY KEY (sortcode ) ) CREATE TABLE account ( INTEGER NOT NULL, type VARCHAR(8) NOT NULL, cname VARCHAR(20) NOT NULL, rate DECIMAL(4,2) NULL, sortcode INTEGER NOT NULL, CONSTRAINT account pk PRIMARY KEY (), ) CONSTRAINT account fk FOREIGN KEY ( sortcode ) REFERENCES branch account type cname rate sortcode account(sortcode) fk branch(sortcode) Primary Key Choose the key most often used to access a table as the primary key P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 5 / 40

RM to SQL DDL SQL DML: Definition of Keys branch CREATE TABLE branch sortcode bname cash ( sortcode INTEGER NOT NULL, bname VARCHAR(20) NOT NULL, cash DECIMAL(10,2) NOT NULL, CONSTRAINT branch pk PRIMARY KEY (sortcode ) ) CREATE TABLE account ( INTEGER NOT NULL, type VARCHAR(8) NOT NULL, cname VARCHAR(20) NOT NULL, rate DECIMAL(4,2) NULL, sortcode INTEGER NOT NULL, CONSTRAINT account pk PRIMARY KEY (), ) CONSTRAINT account fk FOREIGN KEY ( sortcode ) REFERENCES branch account type cname rate sortcode account(sortcode) fk branch(sortcode) Declaring Primary Keys after table creation ALTER TABLE branch ADD CONSTRAINT branch pk PRIMARY KEY ( sortcod P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 5 / 40

RM to SQL DDL SQL DML: Definition of Keys branch CREATE TABLE branch sortcode bname cash ( sortcode INTEGER NOT NULL, bname VARCHAR(20) NOT NULL, cash DECIMAL(10,2) NOT NULL, CONSTRAINT branch pk PRIMARY KEY (sortcode ) ) CREATE TABLE account ( INTEGER NOT NULL, type VARCHAR(8) NOT NULL, cname VARCHAR(20) NOT NULL, rate DECIMAL(4,2) NULL, sortcode INTEGER NOT NULL, CONSTRAINT account pk PRIMARY KEY (), ) CONSTRAINT account fk FOREIGN KEY ( sortcode ) REFERENCES branch account type cname rate sortcode account(sortcode) fk branch(sortcode) Declaring additional keys for a table CREATE UNIQUE INDEX branch bname key ON branch(bname) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 5 / 40

RM to SQL DDL SQL DML: Inserting, Updating and Deleting Data INSERT INTO account VALUES (100, current, McBrien, P.,NULL,67), (101, deposit, McBrien, P.,5.25,67), (103, current, Boyd, M.,NULL,34), (107, current, Poulovassilis, A.,NULL,56), (119, deposit, Poulovassilis, A.,5.50,56), (125, current, Bailey, J.,NULL,56) account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 6 / 40

RM to SQL DDL SQL DML: Inserting, Updating and Deleting Data INSERT INTO account VALUES (100, current, McBrien, P.,NULL,67), (101, deposit, McBrien, P.,5.25,67), (103, current, Boyd, M.,NULL,34), (107, current, Poulovassilis, A.,NULL,56), (119, deposit, Poulovassilis, A.,5.50,56), (125, current, Bailey, J.,NULL,56) UPDATE account SET type= deposit WHERE =100 account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 account type cname rate sortcode 100 deposit McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 DELETE WHERE =100 account type cname rate sortcode 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 6 / 40

RA to SQL DML SQL DML: An Implementation of the RA SQL SELECT statements: Rough Equivalence to RA SELECT A 1,...,A n FROM R 1,...,R m WHERE P 1 AND... AND P k π A1,...,A n σ P1... P k R 1... R m SQL SELECT implements RA π,σ and π bname, σ branch.sortcode=account.sortcode account.type= current (branch account) SELECT branch.bname, account., branch WHERE account. sortcode=branch. sortcode AND account. type= current P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 7 / 40

RA to SQL DML Naming columns in SQL Column naming rules in SQL You must never have an ambiguous column name in an SQL statement You can use SELECT * to indicate all columns (i.e. have projection) You can use tablename.* to imply all columns from a table SELECT branch.bname, account. sortc ode, branch WHERE account. sortc ode= branch. sortcode AND account. type= current SELECT bname, sortc ode, branch WHERE account. sortc ode= branch. sortcode AND type= current SELECT bname, account. sortc ode, branch WHERE account. sortc ode= branch. sortcode AND type= current SELECT branch.,, branch WHERE account. sortc ode= branch. sortcode AND type= current sortcode bname cash 67 Strand 34005.00 100 34 Goodge St 8900.67 103 56 Wimbledon 94340.45 107 56 Wimbledon 94340.45 125 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 8 / 40

RA to SQL DML Quiz 1: Translating RA into SQL Which SQL query implements π bname, σ type= deposit (account branch)? A SELECT, branch WHERE type= deposit B SELECT bname,, branch WHERE type= deposit C SELECT bname, FROM branch, account WHERE branch. sortcode= account. sortcode AND type= deposit D SELECT bname,, branch WHERE branch. sortcode= account. AND type= deposit P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 9 / 40

RA to SQL DML Connectives Between SQL SELECT statements Binary operators between SELECT statements SQL UNION implements RA SQL EXCEPT implements RA SQL INTERSECT implements RA Note that two tables must be union compatible: have the same number and type of columns π account π movement SELECT EXCEPT SELECT FROM movement P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 10 / 40

Joins SQL Joins Classic SQL Join Syntax SELECT branch.,, type, cname, rate FROM branch, account WHERE branch. sortcode=account. sortcode Modern SQL Join Syntax SELECT branch.,, type, cname, rate FROM branch JOIN account ON branch. sortcode=account. sortcode Special Syntax for Natural Join SELECT FROM branch NATURAL JOIN account Ather Special Syntax for Natural Join SELECT branch.,, type, cname, rate FROM branch JOIN account USING (sortcode ) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 11 / 40

Joins Overview of RA and SQL correspondances RA and SQL RA Operator SQL Operator π SELECT σ WHERE R 1 R 2 FROM R 1,R 2 or FROM R 1 CROSS JOIN R 2 R 1 R 2 FROM R 1 NATURAL JOIN R 2 θ R 1 R2 FROM R 1 JOIN R 2 ON θ R 1 R 2 R 1 EXCEPT R 2 R 1 R 2 R 1 UNION R 2 R 1 R 2 R 1 INTERSECT R 2 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 12 / 40

Joins Try some examples yourself... medusa-s2(pjm)-4$ psql -h db -U lab -d lab bank branch -W Password: lab bank branch=> SELECT * lab bank branch-> FROM branch NATURAL JOIN account; sortcode bname cash type cname rate ----------+-----------+----------+-----+----------+-------------------+------ 67 Strand 34005.00 100 current McBrien, P. 67 Strand 34005.00 101 deposit McBrien, P. 5.25 34 Goodge St 8900.67 103 current Boyd, M. 56 Wimbledon 94340.45 107 current Poulovassilis, A. 56 Wimbledon 94340.45 119 deposit Poulovassilis, A. 5.50 56 Wimbledon 94340.45 125 current Bailey, J. P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 13 / 40

Joins...and find out that t all DBMSs are the same medusa-s2(pjm)-4$ sqsh -S sqlserver -X -U lab -D lab bank branch Password: [21] sqlserver.lab bank branch.1> SELECT * [21] sqlserver.lab bank branch.2> FROM branch NATURAL JOIN account [21] sqlserver.lab bank branch.3> \go Msg 102, Level 15, State 1 Server DOWITCHER, Line 2 Line 2: Incorrect syntax near account. P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 14 / 40

Bags v Sets SQL: Bags and Sets SELECT sortcode π sortcode account sortcode 67 67 56 56 56 34 SELECT DISTINCT sortcode π sortcode account sortcode 34 56 67 SQL SELECT: Bag semantics By default, an SQL SELECT (equivalent to an RA π) does t eliminate duplicates, and returns a bag (or multiset) rather than a set. Any SELECT that does t cover a key of the input relation, and requires a set based answer, should use DISTINCT. P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 15 / 40

Bags v Sets SQL: Bags and Sets SELECT ALL sortcode π sortcode account sortcode 67 67 56 56 56 34 SELECT DISTINCT sortcode π sortcode account sortcode 34 56 67 SQL SELECT: Bag semantics By default, an SQL SELECT (equivalent to an RA π) does t eliminate duplicates, and returns a bag (or multiset) rather than a set. Any SELECT that does t cover a key of the input relation, and requires a set based answer, should use DISTINCT. P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 15 / 40

Bags v Sets Quiz 2: Correct use of SELECT DISTINCT (1) branch(sortcode,bname,cash) key branch(sortcode) key branch(bname) Which SQL query requires the use of DISTINCT in order to avoid the possibility of a bag being produced? A SELECT FROM branch WHERE cash >10000 B SELECT sortcode FROM branch WHERE cash >10000 C SELECT bname, cash FROM branch D SELECT cash FROM branch WHERE cash >10000 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 16 / 40

Bags v Sets Quiz 3: Correct use of SELECT DISTINCT (2) branch(sortcode,bname,cash) account(,type,cname,rate,sortcode) key branch(sortcode) key branch(bname) key account() Which SQL query requires the use of DISTINCT in order to avoid the possibility of a bag being produced? A SELECT FROM branch NATURAL JOIN account B SELECT branch. sortcode, type, rate FROM branch NATURAL JOIN account C SELECT branch. sortcode, FROM branch NATURAL JOIN account D SELECT branch. sortcode,, cash FROM branch NATURAL JOIN account P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 17 / 40

Bags v Sets Quiz 4: Operators that might produce bags If R and S are sets, which RA operator could produce a bag result if the implementation did t check for duplicates? A σr B R S C R S D R S P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 18 / 40

Bags v Sets Bag and Set operations in SQL RA Operator Set Based SQL Bag Based SQL π A1,...,A n SELECT DISTINCT A 1,...,A n SELECT ALL A 1,...,A n R 1... R m FROM R 1,...,R m FROM R 1,...,R m σ P1,...,P k WHERE P 1 AND...AND P k WHERE P 1 AND...AND P k R 1 R 2 R 1 UNION DISTINCT R 2 R 1 UNION ALL R 2 R 1 R 2 R 1 EXCEPT DISTINCT R 2 R 1 EXCEPT ALL R 2 R 1 R 2 R 1 INTERSECT DISTINCT R 2 R 1 INTERSECT ALL R 2 Chosing between set and bag semantics If you omit DISTINCT or ALL, then the defaults are: SELECT ALL UNION DISTINCT EXCEPT DISTINCT INTERSECT DISTINCT No FROM DISTINCT or WHERE DISTINCT? There is need for DISTINCT or ALL around FROM ( ) and WHERE (σ) cant introduce any duplicates, and any existing duplicates can be removed in the SELECT P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 19 / 40

Bags v Sets Project-Select-Product Queries SQL SELECT statements: Exact Equivalence to RA SELECT DISTINCT A 1,...,A n FROM R 1,...,R m WHERE P 1 AND... AND P k SQL SELECT implements RA π,σ and Omit DISTINCT when either you kwn A 1,...,A n cover a key you want a bag (rather than set) answer π A1,...,A n σ P1... P k R 1... R m P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 20 / 40

Bags v Sets Quiz 5: SQL EXCEPT SELECT FROM movement EXCEPT SELECT movement mid amount tdate 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1007 107 345.56 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 What is the result of the above SQL query? A B C D 100 101 103 107 119 125 100 101 103 107 119 100 100 101 107 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 21 / 40

Bags v Sets Quiz 6: SQL EXCEPT ALL SELECT FROM movement EXCEPT ALL SELECT movement mid amount tdate 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1007 107 345.56 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 What is the result of the above SQL query? A B C D 100 101 103 107 119 125 100 101 103 107 119 100 100 101 107 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 22 / 40

Bags v Sets Table Aliases Table and Column Aliases The SQL operator AS allows a column or table name to be renamed. Essential when needing to join a table with itself List people with a current and a deposit account SELECT current account.cname, current account. AS current, deposit account. AS deposit AS current account JOIN account AS deposit account ON current account.cname=deposit account.cname AND current account. type= current AND deposit account. type= deposit cname current deposit McBrien, P. 100 101 Poulovassilis, A. 107 119 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 23 / 40

Bags v Sets Worksheet: Translating Between Relational Algebra and SQL account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 movement mid amount tdate 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1007 107 345.56 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 movement() fk account. P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 24 / 40

Set Operations Set Operations: IN IN operator tests for membership of a set SELECT WHERE type= current AND IN (100,101) Can use nested SELECT to generate set SELECT WHERE type= current AND IN (SELECT FROM movement WHERE amount >500) 100 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 25 / 40

Set Operations Set Operations: IN IN operator tests for membership of a set SELECT WHERE type= current AND IN (100,101) Can use nested SELECT to generate set SELECT WHERE type= current AND IN (SELECT FROM movement WHERE amount >500) SELECT DISTINCT account. FROM account JOIN movement ON account. =movement. WHERE type= current AND amount >500 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 25 / 40

Set Operations Quiz 7: SQL Set Membership Testing SELECT WHERE type= current AND NOT IN ( SELECT FROM movement WHERE amount >500) account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 movement mid amount tdate 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1007 107 345.56 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 What is the result of the above SQL query? A B C D 100 103 107 125 100 103 107 103 107 125 103 107 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 26 / 40

Set Operations Quiz 7: SQL Set Membership Testing SELECT SELECT DISTINCT account. WHERE type= current JOIN movement AND NOT IN ON account.=movement. ( SELECT WHERE type= current FROM movement AND NOT amount >500 WHERE amount >500) What is the result of the above SQL query? A B C D 100 103 107 125 100 103 107 103 107 125 103 107 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 26 / 40

Set Operations Set Operations: EXISTS Testing for Existence IN can be used to test if some value is in a relation, either listed, or produced by some SELECT statement EXISTS can be used to test if a SELECT statement returns any rows List people without a deposit account SELECT cname WHERE cname NOT IN ( SELECT cname WHERE type= deposit ) cname Boyd, M. Bailey, J. SELECT cname WHERE NOT EXISTS ( SELECT deposit account WHERE type= deposit AND account.cname=cname) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 27 / 40

Set Operations Set Operations: EXISTS NOT EXISTS and EXCEPT Most queries involving EXCEPT can be also written using NOT EXISTS EXCEPT relatively recent addition to SQL π account π movement SELECT EXCEPT SELECT FROM movement SELECT WHERE NOT EXISTS ( SELECT FROM movement WHERE =account.) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 28 / 40

Set Operations Set Operations: SOME and ALL Can test a value against members of a set V op SOME S is TRUE is there is at least one V s S such that V op V s V op ALL S is TRUE is there are values V s S such that NOT V op V s names of branches that only have current accounts SELECT bname FROM branch WHERE current =ALL (SELECT type WHERE branch. sortcode=account. sortcode ) names of branches that have deposit accounts SELECT bname FROM branch WHERE deposit =SOME (SELECT type WHERE branch. sortcode=account. sortcode ) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 29 / 40

Set Operations Worksheet: Set Operations branch sortcode bname cash 56 Wimbledon 94340.45 34 Goodge St 8900.67 67 Strand 34005.00 movement mid amount tdate 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1007 107 345.56 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 key branch(sortcode) key branch(bname) key movement(mid) key account() movement() fk account() account(sortcode) fk branch(sortcode) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 30 / 40

Set Operations Worksheet: Set Operations (3) Write an SQL query without using any negation (i.e. without the use of NOT or EXCEPT) that list accounts with movements on or before the 11-Jan-1999. SELECT WHERE 11 jan 1999 <ALL (SELECT tdate FROM movement WHERE movement.=account.) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 31 / 40

Set Operations Worksheet: Set Operations (4) Write an SQL query that lists the cname of customers that have every type of account that appears in account SELECT DISTINCT cname AS cust account WHERE NOT EXISTS ( SELECT type EXCEPT SELECT type WHERE account.cname=cust account.cname ) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 32 / 40

Set Operations Set Operations: NOT SOME NOT and ALL In first order classical logic: accounts with all movements less than or equal to 500 SELECT WHERE 500>=ALL ( SELECT amount FROM movement WHERE account.=movement.) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 33 / 40

Set Operations Set Operations: NOT SOME NOT and ALL In first order classical logic: accounts with all movements less than or equal to 500 SELECT WHERE 500>=ALL ( SELECT amount FROM movement WHERE account.=movement.) SELECT WHERE NOT 500<SOME ( SELECT amount FROM movement WHERE account.=movement.) P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 33 / 40

Null Null Several definitions of null have been proposed, including: 1 null represents a something that is t present in the UoD P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 34 / 40

Null Null Several definitions of null have been proposed, including: 1 null represents a something that is t present in the UoD 2 null represents something that might be present in the UoD, but we do t kw its value at present P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 34 / 40

Null Null Several definitions of null have been proposed, including: 1 null represents a something that is t present in the UoD 2 null represents something that might be present in the UoD, but we do t kw its value at present 3 null represents something that is present in the UoD, but we do t kw its value at present P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 34 / 40

Null Null Several definitions of null have been proposed, including: 1 null represents a something that is t present in the UoD 2 null represents something that might be present in the UoD, but we do t kw its value at present 3 null represents something that is present in the UoD, but we do t kw its value at present SQL handling of NULL SQL uses a three valued logic to process WHERE predicate Truth values are TRUE, FALSE, and UNKNOWN SQL standard vague, but handling of NULL is nearest to option 2 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 34 / 40

Null Quiz 8: SQL handling of NULL (1) account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 SELECT WHERE rate=null What is the result of the SQL query above? A B C D 100 101 103 107 119 125 100 103 107 125 101 119 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 35 / 40

Null Quiz 9: SQL handling of NULL (2) account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 SELECT WHERE rate=null OR rate<>null What is the result of the SQL query above? A B C D 100 101 103 107 119 125 100 103 107 125 101 119 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 36 / 40

Null SQL implements three valued logic P 1 OR P 2 P 2 TRUE UNKNOWN FALSE TRUE TRUE TRUE TRUE P 1 UNKNOWN TRUE UNKNOWN UNKNOWN FALSE TRUE UNKNOWN FALSE NOT P 1 TRUE FALSE P 1 UNKNOWN UNKNOWN FALSE TRUE P 1 AND P 2 P 2 TRUE UNKNOWN FALSE TRUE TRUE UNKNOWN FALSE P 1 UNKNOWN UNKNOWN UNKNOWN FALSE FALSE FALSE FALSE FALSE x=null is UNKNOWN (and t TRUE or FALSE) null=null is UNKNOWN x IS NULL returns TRUE if x has a null value x IS NOT NULL returns TRUE if x does t have a null value P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 37 / 40

Null Correct SQL Queries Using null Every nullable attribute can be tested to see if it is null: SELECT WHERE rate IS NULL SELECT WHERE rate IS NULL OR rate IS NOT NULL Can test for logical state by IS TRUE, IS NOT TRUE,... SELECT WHERE (rate=5.50) IS NOT TRUE P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 38 / 40

Null Quiz 10: SQL Might Be SELECT WHERE (rate=5.25) IS NOT FALSE account type cname rate sortcode 100 current McBrien, P. NULL 67 101 deposit McBrien, P. 5.25 67 103 current Boyd, M. NULL 34 107 current Poulovassilis, A. NULL 56 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. NULL 56 What is the result of the above SQL query? A B C D 100 101 103 107 125 100 103 107 119 125 100 103 107 125 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 39 / 40

Null Worksheet: Null values in SQL movement mid amount tdate 0999 119 45.00 null 1000 100 2300.00 5/1/1999 1001 101 4000.00 5/1/1999 1002 100-223.45 8/1/1999 1004 107-100.00 11/1/1999 1005 103 145.50 12/1/1999 1006 100 10.23 15/1/1999 1008 101 1230.00 15/1/1999 1009 119 5600.00 18/1/1999 1010 100 null 20/1/1999 1011 null null 20/1/1999 1012 null 600.00 20/1/1999 1013 null -46.00 20/1/1999 account type cname rate sortcode 100 current McBrien, P. null 67 101 deposit McBrien, P. 5.25 67 119 deposit Poulovassilis, A. 5.50 56 125 current Bailey, J. null 56 P.J. McBrien (Imperial College London) SQL: An Implementation of the Relational Algebra 40 / 40