CS143: Query and Update in SQL

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

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

SQL NULL s, Constraints, Triggers

Relational Databases

Chapter 5. SQL: Queries, Constraints, Triggers

CS143 Notes: Views & Authorization

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Databases 2011 The Relational Model and SQL

Oracle Database: SQL and PL/SQL Fundamentals

SQL: Queries, Programming, Triggers

Oracle SQL. Course Summary. Duration. Objectives

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

SQL: Queries, Programming, Triggers

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

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Introduction to Microsoft Jet SQL

Instant SQL Programming

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

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

SQL Tables, Keys, Views, Indexes

Relational Algebra and SQL

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

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

EECS 647: Introduction to Database Systems

Relational Database: Additional Operations on Relations; SQL

Week 4 & 5: SQL. SQL as a Query Language

CS143 Notes: Normalization Theory

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

1 Structured Query Language: Again. 2 Joining Tables

Lecture 4: More SQL and Relational Algebra

Introduction to database design

SQL (structured query language)

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

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

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

IT2305 Database Systems I (Compulsory)

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Oracle Database 11g SQL

IT2304: Database Systems 1 (DBS 1)

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

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

CSC 443 Data Base Management Systems. Basic SQL

DBMS / Business Intelligence, SQL Server

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Financial Data Access with SQL, Excel & VBA

Exercise 1: Relational Model

3.GETTING STARTED WITH ORACLE8i

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Information Systems SQL. Nikolaj Popov

SQL. Short introduction

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Translating SQL into the Relational Algebra

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

Oracle 10g PL/SQL Training

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Introduction to SQL ( )

Structured Query Language (SQL)

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Chapter 13: Query Optimization

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

SQL Server 2008 Core Skills. Gary Young 2011

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

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Introduction to SQL: Data Retrieving

Advance DBMS. Structured Query Language (SQL)

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Lecture 6. SQL, Logical DB Design

SQL Nested & Complex Queries. CS 377: Database Systems

Database Management Systems. Chapter 1

Query-by-Example (QBE)

3. Relational Model and Relational Algebra

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database 10g: Introduction to SQL

Database Design and Database Programming with SQL - 5 Day In Class Event Day 1 Activity Start Time Length

Chapter 14: Query Optimization

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Oracle Database: Introduction to SQL

T-SQL STANDARD ELEMENTS

Database Systems. Lecture 1: Introduction

A basic create statement for a simple student table would look like the following.

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


Relational Algebra. Basic Operations Algebra of Bags

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

Database Query 1: SQL Basics

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

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

Course ID#: W 35 Hrs. Course Content

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

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

The Relational Model. Why Study the Relational Model?

Oracle Database: Introduction to SQL

Relational Division and SQL

Querying Microsoft SQL Server 20461C; 5 days

Transcription:

CS143: Query and Update in SQL Book Chapters (4th) Chapter 4.1-6, 4.8-10, 3.3.4 (5th) Chapter 3.1-8, 3.10-11 (6th) Chapter 3.1-9, 4.1, 4.3 Things to Learn SQL DML for SQL Structured Query Language The standard language for all commericial RDBMS SQL has many aspects DDL: schema definition, constraints, index,... DML: query, update,... triggers, transaction, authorization,... In this lecture, we cover the DML aspect of SQL How to query and modify exsiting databases SQL and DBMS SQL is high-level description of user s query No concrete procedure for query execution is given The beauty and success of DBMS The system understands the query and find the best way possible to execute it automatically Example to Use in the Class School information Student(sid, name, age, GPA, address,... ) Class(dept, cnum, sec, unit, title, instructor,... ) Enroll(sid, dept, cnum, sec) 1

Basic SELECT statement Query 1: Find the titles and instructors of all CS courses Semantics Interpret and write FROM WHERE SELECT FROM: the list of tables to look up WHERE: conditions to meet SELECT: the attributes to return Conceptual execution (table cursor diagram) Check condition Return attr General SQL statement SELECT A1,..., An FROM R1,..., Rm WHERE C π A1,...,A n (σ C (R 1 R m )) SELECT *: all attributes SELECT is projection not selection : can be confusing SQL does not remove duplicates: Major difference between SQL and relational algebra SQL join More examples will follow Query 2: Find the names and GPAs of all students taking CS classes Conceptually WHERE R, S (Table join diagram) 2

R S For every pair of tuples from R and S, we check condition and produce output Notes: S, E: tutple variable renaming operator We can consider that S and E are variables that bind to every pair of tuples Attribues can also be renamed GPA (AS) grade DISTINCT: remove duplicates in the results WHERE conditions Query 3: All student names and GPAs who live on Wilshire %: any length (0 ) string : one character %Wilshire% : Any string containing Wilshire Q: What does % mean? Other useful string functions: UPPER(), LOWER(), CONCAT(),... 3

Set operators : INTERSECT, : UNION, : EXCEPT Can be applied to the result of SELECT statements or to relations Query 4: All names of students and instructors Important points to note Set operators should have the same schema for operands In practice, it is okay to have just compatible types Set operators follow set semantics and remove duplicates Set semantics is well understood for set operations. semantics. Efficiency To keep duplicates, use UNION ALL, INSERSECT ALL, EXCEPT ALL Query 5: Find ids of all students who are not taking any CS courses. Not many people know bag MySQL supports only UNION, not INTERSECT or EXCEPT. 4

Subqueries SELECT statement may appear in WHERE clause Treated the same as regular relations If the result is one-attribute one-tuple relation, the result can be used like a value Scalar-value subqueries Query 6: Find the student ids who live at the same addr as the student with id 301 Q: Can we rewrite it without subquery? Notes: There is a whole theory about whether/how to rewite a subquery to non-subquery SQL The basic result is we can rewrite subqueries as long as we do not have negation. With negation, we need EXCEPT One of the reasons why relational model has been so successful Because it is easy to understand and model, we can design and prove elegant theorems. Many efficient and provable algorithms. Set membership (IN, NOT IN) Query 7: Find all student names who take CS classes. Idea: Find the set of sids that take CS classes first. Then check whether any student s id belong to that set or not. IN is a set membership operator (a IN R) is TRUE if a appears in R 5

Q: Can we write the same query without subqueries? Q: Are the above two queries equivalent? Q: Why we care about duplicates so much? Query 8: Find the names of students who take no CS classes Q: Can we rewrite it without subqueries? Set comparison operator (> ALL, < SOME,... ) Query 9: Find the ids of students whose GPA is greater than all students of age below 18 ALL is the universial quantifier 6

Query 10: Find the names of students whose GPA is better than at least one other student of age 18 SOME is the existential quantifier Other Set comparison operators: > ALL, <= SOME, = SOME,..., etc. (<> ALL) (NOT IN), (= SOME) IN EXISTS and Correlated subqueries Query 11: Find the names of the students who take CS courses EXISTS: WHERE EXISTS(SELECT... FROM... WHERE) True if SELECT.. FROM.. WHERE returns at least one tuple Correlated subquery interpretation: Outer query looks at one tuple at a time and binds the tuple to S For each S, we execute the inner query and check the condition This is just interpretation. DBMS executes it more efficiently but get the same result (but not necessarily MySQL). Subqueries in FROM clause Considered as a regular relation Example: SELECT name FROM (SELECT name, age FROM Student) S WHERE age > 17 A subquery inside FROM MUST be renamed Student names with age > 17 Q: Do subqueries make SQL more expressive than relational algebra? 7

Aggregates The operators so far check the condition tuple-by-tuple They never summarize multiple tuples into one. For example, SUM, AVG of GPA is not possible. Aggregate function (aggregate diagram) tuples Aggregate Function one tuple Query 12: Find the average GPA Common aggregate functions: SUM, AVG, COUNT, MIN, MAX on single attribute or COUNT(*). Problems of Duplicates Query 13: The number of students taking CS classes Query 14: The average GPA of the students taking CS classes GROUP BY clause Sometimes, we want to get separate statistics for each group of tuples Example: Age AVG(GPA) 17 3.7 19 2.1 20 3.1 But AVG() takes average over all tuples. 8

Query 15: Find the average GPA for each age group Q: Is the following query meaningful? SELECT sid, age, AVG(GPA) FROM Student GROUP BY age SELECT can have only attributes that have a single value in each group or aggregates Query 16: Find the number of classes each student is taking Q: What about the students who take no classes? Comments: We will learn about outer join that can address this issue later. HAVING clause Query 17: Find students who take two or more classes Conditions on aggregates should appear in the HAVING clause. Q: Can we rewrite the query without HAVING clause? In general, we can rewrite a query not to have a HAVING clause. 9

ORDER BY clause Sometimes we may want to display tuples in a certain order. For example order all students by their GPA SELECT sid, GPA FROM Student ORDER BY GPA DESC, sid ASC All students and GPAs, in the descending order of their GPAs and the ascending order of sids. Default is ASC if omitted. Does not change SQL semantics. Just makes the display easier to look at and understand General SQL SELECT statement SELECT attributes, aggregates FROM relations WHERE conditions GROUP BY attributes HAVING conditions on aggregates ORDER BY attributes, aggregates Evaluation order: FROM WHERE GROUP BY HAVING ORDER BY SE- LECT 10

Data Modification in SQL (INSERT/DELETE/UPDATE) Insertion: INSERT INTO Relation Tuples Query 18: Insert tuple (301, CS, 201, 01) to Enroll? Query 19: Populate Honors table with students of GPA > 3.7? Deletion: DELETE FROM R WHERE Condition Query 20: Delete all students who are not taking classes Update: Update R SET A1 = V1, A2 = V2,..., An = Vn WHERE Condition Query 21: Increase all CS course numbers by 100 11

NULL and Three-valued logic Arithmatic operators and comparison Q: SELECT name FROM Student WHERE GPA * 100/4 > 90 What should we do if GPA is NULL? Q: What should be the value for GPA * 100/4? Rule: Arithmatic operators with NULL input returns NULL Q: What should be NULL > 90? Rule: Arithmatic comparison with NULL value return Unknown SQL is Three-valued logic: True, False, Unknown SQL returns only True tuples GPA * 100/4 > 90 does not return a tuple if GPA is NULL Three-valued logic Q: GPA > 3.7 AND age > 18. What if GPA is NULL and age < 18? Q: GPA > 3.7 OR age > 18. What if GPA is NULL and age < 18? 12

Truth table AND: U AND T = U, U AND F = F, U AND U = U OR: U OR T = T, U OR F = U, U OR U = U NOT Unknwon = Unknown. It s not known SQL returns only True tuples Checking NULL IS NULL or IS NOT NULL to check if the value is null. Set operators (,, ) Q: What should be {2.4, 3.0, NULL} {3.6, NULL}? Rule: NULL is treated like other values in set operators Aggregates Q: ID GPA 1 3.0 2 3.6 3 2.4 4 NULL SELECT AVG(GPA) FROM Student What should be the result? What about COUNT(*)? COUNT(GPA)? Rule: Aggregates are computed ignoring NULL value, except COUNT(*). Too much information is lost otherwise. COUNT(*) considers a NULL tuple as a valid tuple When the input to an aggregate is empty, COUNT returns 0; all others return NULL. 13

SQL and bag semantics What is a bag (multiset)? A set with duplicate elements Order does not matter Example: {a, a, b, c} = {a, c, b, a} {a, b, c} SQL and bag semantics Default SQL statements are based on bag semantics We already learned the bag semantics Except set operators (UNION, INTERSECT, EXCEPT), which use set semantics We can enforce set semantics by using DISTINCT keyword Bag semantics for set operators UNION ALL, INTERSECT ALL, EXCEPT ALL Q: {a, a, b} {a, b, c}? Q: {a, a, a, b, c} {a, a, b}? Q: {a, a, b, b} {a, b, b, c}? What rules still hold for Bag? Q: Under bag semantics, R S = S R? R S = S R? R (S T ) = (R S) (R T )? Under bag semantics, some rules still hold, some do not Consider, R = {a}, S = {a}, T = {a} to check the distributive rule. 14

OUTER join Query 22: How many classes does each student take? Q: What about student 208, Esther? What should we print? What is the problem? Q: Anyway to preserve dangling tuples? OUTER JOIN operator in WHERE clause: R LEFT OUTER JOIN S ON R.A = S.A Keep all dangling tuples from R by padding S attributes with NULL. R RIGHT OUTER JOIN S ON R.A = S.A keep all dangling tuples from S by padding R attributes with NULL R FULL OUTER JOIN S ON R.A = S.A keep all dangling tuples both from R and S with appropriate padding Q: How to rewrite the above query to include Esther? 15

Expressive power of SQL Example: All ancestors child parent Susan John John James James Elaine...... Q: Can we find all ancestors of Susan using SQL? Example: All reachable destination city 1 city 2 A B B D A C E F G H...... Q: Find all cities reachable from A? Comments: SQL92 does not support recursion and thus cannot compute the transitive closure. Recursion is supported in SQL3. WITH RECURSIVE R(A1, A2) AS... e.g., WITH RECURSIVE Ancestor(child, ancestor) AS ( (SELECT * FROM Parent) UNION (SELECT P.child, A.ancestor FROM Parent P, Ancestor A WHERE P.parent = A.child) ) SELECT * FROM Ancestor IBM DB2 supports it, while Oracle does not. Read Book 5.2 for detail 16