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



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

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

Relational Database: Additional Operations on Relations; SQL

3. Relational Model and Relational Algebra

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

Advance DBMS. Structured Query Language (SQL)

CSC 443 Data Base Management Systems. Basic SQL

SQL Nested & Complex Queries. CS 377: Database Systems

Oracle SQL. Course Summary. Duration. Objectives

Information Systems SQL. Nikolaj Popov

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

Introduction to Microsoft Jet SQL

SQL: Queries, Programming, Triggers

IT2305 Database Systems I (Compulsory)

Chapter 5. SQL: Queries, Constraints, Triggers

SQL SELECT Query: Intermediate

Relational Databases

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

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

SQL: Queries, Programming, Triggers

IT2304: Database Systems 1 (DBS 1)

Structured Query Language (SQL)

Oracle Database: SQL and PL/SQL Fundamentals

2. Conceptual Modeling using the Entity-Relationship Model

Databases 2011 The Relational Model and SQL

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

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: SQL and PL/SQL Fundamentals

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

How To Create A Table In Sql (Ahem)

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

Instant SQL Programming

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

Relational Algebra and SQL

Part 4: Database Language - SQL

Oracle Database 10g: Introduction to SQL

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

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

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

DBMS / Business Intelligence, SQL Server

Financial Data Access with SQL, Excel & VBA

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

Oracle Database 12c: Introduction to SQL Ed 1.1

CIS 631 Database Management Systems Sample Final Exam

SQL NULL s, Constraints, Triggers


Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Introduction to database design

5.1 Database Schema Schema Generation in SQL

Oracle 10g PL/SQL Training

1 Structured Query Language: Again. 2 Joining Tables

Programming with SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

SQL. Short introduction

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

Database Query 1: SQL Basics

Oracle Database: SQL and PL/SQL Fundamentals NEW

Summary on Chapter 4 Basic SQL

Netezza SQL Class Outline

Querying Microsoft SQL Server

Mini User's Guide for SQL*Plus T. J. Teorey

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

Chapter 4: SQL. Schema Used in Examples

Introduction to SQL: Data Retrieving

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

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

Chapter 14: Query Optimization

Introduction to SQL and database objects

Oracle Database 11g SQL

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

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

Chapter 6: Integrity Constraints

T-SQL STANDARD ELEMENTS

Course ID#: W 35 Hrs. Course Content

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

3.GETTING STARTED WITH ORACLE8i

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

Querying Microsoft SQL Server 20461C; 5 days

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

SQL Server 2008 Core Skills. Gary Young 2011

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Chapter 13: Query Optimization

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

BCA. Database Management System

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

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

Business Intelligence Extensions for SPARQL

CS 338 Join, Aggregate and Group SQL Queries

Relational Division and SQL

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

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

Normal Form vs. Non-First Normal Form

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

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

MOC QUERYING MICROSOFT SQL SERVER

Oracle/SQL Tutorial 1

Transcription:

ECS-165A WQ 11 66 4. SQL Contents Basic Queries in SQL (select statement) Set Operations on Relations Nested Queries Null Values Aggregate Functions and Grouping Data Definition Language Constructs Insert, Update, and Delete Statements Views (Virtual Tables) Example Database CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain) orders((fname, LName) CUSTOMERS, SName SUPPLIERS, Prodname PRODUCTS, Quantity) offers(sname SUPPLIERS, Prodname PRODUCTS, Price)

ECS-165A WQ 11 67 Basic Structure SQL is based on set and relational operations with certain modifications and enhancements. In this course we focus on SQL ( SQL Standard) but also do some PostgreSQL specifics later A typical SQL query has the form select A 1, A 2,..., A n from r 1, r 2,..., r k where P A i s represent attributes r i s represent relations P is a predicate This query is equivalent to the relational algebra expression π A1,A 2,...,An(σ P (r 1 r 2... r k )) The result of an SQL query is a relation (set of tuples) with a schema defined through the attributes A i s. The select clause corresponds to the projection operation of the relational algebra; it is used to list the attributes to be output in a query result. Find the name of all suppliers. select SName from SUPPLIERS; π SName (SUPPLIERS)

ECS-165A WQ 11 68 Basic Structure (cont.) An asterisk in the select clause denotes all attributes select from SUPPLIERS; SQL allows duplicate tuples in a relation as well as in query results. Duplicates can be removed from query result using keyword distinct select distinct Account from CUSTOMERS; select clause can contain arithmetic expressions as well as functions on attributes including attributes and constants. select substr(sname,1,10) [as] Name, Prodname, Price 100 from offers; The where clause corresponds to the selection operation of the relational algebra. It consists of a predicate involving attributes of the relations that appear in the from clause. List the first and last name of customers having a negative account. select FName, LName from CUSTOMERS where Account < 0;

ECS-165A WQ 11 69 Basic Structure (cont.) Logical connectives and, or, and not can be used to formulate complex condition in where clause. Which suppliers (SName) offer a MegaPC or a TinyMac? select SName from offers where Prodname = MegaPC or Prodname = TinyMac ; ˆ=... where Prodname in ( MegaPC, TinyMac ) List the name of products that cost more than $10,000 and less than $20,000. select Prodname, Price from offers where Price >= 10000 and Price <= 20000; ˆ=... where Price between 10000 and 20000 The from clause corresponds to the Cartesian Product of the relational algebra. List all customer with the products they can order. select from CUSTOMERS, PRODUCTS;

ECS-165A WQ 11 70 Basic Structure (cont.) List all customers who are living in Davis and who have ordered at least 10 MegaPCs. select CUSTOMERS.FName, CUSTOMERS.LName, Quantity from CUSTOMERS, orders where CAddress like %Davis% and CUSTOMERS.FName = orders.fname and CUSTOMERS.LName = orders.lname and Prodname = MegaPC and Quantity > 10; π CUSTOMERS.FName, CUSTOMERS.LName, Quantity (σ CAddress like %Davis% Quantity>10 Prodname= MegaPC (σ CUSTOMERS.FName=orders.FName CUSTOMERS.LName=orders.LName (CUSTOMERS orders))) Replace the last selection condition σ... by a natural join (CUSTOMERS orders) List the name and address of suppliers that offer products. Remove duplicates from the result and list the result ordered by the supplier s address. select distinct SUPPLIERS.SName, SAddress from SUPPLIERS, offers where SUPPLIERS.SName = offers.sname order by SAddress;

ECS-165A WQ 11 71 Basic Structure (cont.) Using the rename operator (aliasing) select distinct S.SName, SAddress from SUPPLIERS S, offers O where S.SName = O.SName; List all information about customers together with information about the suppliers they have ordered products from. select C., S., O. from CUSTOMERS C, orders O, SUPPLIERS S where C.LName = O.LName and C.FName = O.FName and O.SName=S.SName; Equivalent expression in relational algebra: ((CUSTOMERS orders) SUPPLIERS) List the name of customers who have an account greater or equal than (some) other customers. select C1.FName, C1.LName from CUSTOMERS C1, CUSTOMERS C2 where (C1.FName <> C2.FName or C1.LName <> C2.LName) and C1.Account >= C2.Account; query realizes a condition join!

ECS-165A WQ 11 72 Set Operations The Oracle/SQL set operations union, minus (except), and intersect correspond to the relational algebra operations,, and. Each of the above operations automatically eliminates duplicates. To retain duplicates for the union operator, one has to use the corresponding multiset version union all. Examples: Find all suppliers that offer a MegaPC or TinyMac. (select SName from offers where Prodname = MegaPC ) union (select SName from offers where Prodname = TinyMac ); Find all suppliers that offer both a MegaPC and a TinyMac. (select SName from offers where Prodname = MegaPC ) intersect (select SName from offers where Prodname = TinyMac ); Find all suppliers that offer a MegaPC but not a TinyMac. (select SName from offers where Prodname = MegaPC ) minus (select SName from offers where Prodname = TinyMac );

ECS-165A WQ 11 73 Nested Subqueries So far, where clauses in examples only consist of simple attribute and/or constant comparisons. SQL provides language constructs for the nesting of queries using subqueries. A subquery is a select-from-where expression that is nested within another query. Most common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality. Set valued subqueries in a where condition: <expression> [not] in (<subquery>) <expression> <comparison operator> any (<subquery>) <expression> <comparison operator> all (<subquery>) Set cardinality or test for (non-)existence: [not] exists (<subquery>) Subqueries in a where clause can be combined arbitrarily using logical connectives.

ECS-165A WQ 11 74 Examples of Set Valued Subqueries Give the name and chain of all suppliers located in Davis that offer a MegaPC for less than $1,000. select SName, Chain from SUPPLIERS where SName in (select SName from offers where Prodname = MegaPC and Price < 1000) and SAddress like %Davis% ; This query can also be formulated using a join! Give the name and address of suppliers that don t offer a MegaPC. select SName, SAddress from SUPPLIERS where SName not in (select SName from offers where Prodname = MegaPC ); If it is know that a subquery returns at most one value, then one can use = instead of in.

ECS-165A WQ 11 75 Find the name and address of customers who have ordered a product from Davis Lumber. select from CUSTOMERS where (FName, LName) in (select FName, LName from orders where SName = Davis Lumber ); Find all customers from Woodland who have an account greater than any (some) customer in Davis. select from CUSTOMERS where Account > any (select Account from CUSTOMERS where CAddress like %Davis% ) and CAddress like %Woodland% ; Find customers who have ordered more than one MegaPC from a supplier. select from CUSTOMERS where (FName, LName) = any (select FName, LName from orders where Prodname = MegaPC and Quantity > 1);

ECS-165A WQ 11 76 Note that = any is equivalent to in. List all customers who have an account greater than all customers from Davis. select from CUSTOMERS where Account > all (select Account from CUSTOMERS where CAddress like %Davis% ); Note that <> all or!= all is equivalent to not in. Give all suppliers (SName) who offer at least one product cheaper than all other suppliers. select SName from offers O1 where Price < all (select Price from offers O2 where O1.Prodname = O2.Prodname and O1.SName <> O2.SName); If a subquery refers to attributes of an outer query, the subquery is called a correlated subquery. References to outer relations and attributes typically occur through using aliases.

ECS-165A WQ 11 77 Test for (non-)existence List all customers who have ordered a product from a supplier in Davis. select from CUSTOMERS C where exists (select from orders O, SUPPLIERS S where O.SName = S.SName and O.FName = C.FName and O.LName = C.LName and SAddress like %Davis% ); This query can also be formulated using a natural join select distinct C. from CUSTOMERS C, orders O, SUPPLIERS S where O.SName = S.SName and O.FName = C.FName and O.LName = C.LName and SAddress like %Davis% ;

ECS-165A WQ 11 78 Give all products (Prodname, Category) for which no offer exists. select * from PRODUCTS P where not exists (select from offers where P.Prodname = Prodname); attributes without preceding alias refer to relations listed in the from clause of the subquery where the attributes occur. Find all suppliers that offer a MegaPC, but no TinyMac. select from SUPPLIERS S where exists (select from offers where SName=S.SName and Prodname= MegaPC ) and not exists (select from offers where SName=S.SName and Prodname= TinyMac );

ECS-165A WQ 11 79 Examples (cont.) Give all pairs of suppliers that offer exactly the same products. select distinct O1.SName, O2.SName from offers O1, offers O2 where O1.SName < O2.SName and not exists (( (select Prodname from offers where SName = O1.SName) minus (select Prodname from offers where SName = O2.SName) ) union ( (select Prodname from offers where SName = O2.SName) minus (select Prodname from offers where SName = O1.SName) )) order by O1.SName, O2.SName;

ECS-165A WQ 11 80 Null Values If permitted by the schema definition for a table (i.e., no not null constraints), attributes can have null values. null ˆ= unknown, non-existent, or non-applicable value Result of any arithmetic expression involving null is null Result of where clause condition is false if it evaluates to null. and true false null true true false null null null false null false false false false or true false null true true true true null true null null false true false null not true null false false null true Give all suppliers that are not associated with a chain. select from SUPPLIERS where Chain is null; List all customers who have a known account. select from CUSTOMERS where Account is not null; All aggregate functions except count( ) ignore tuples with null values on the aggregate attribute(s).

ECS-165A WQ 11 81 Aggregate Functions Aggregate functions operate on a multiset of values and return a single value. Typical aggregate functions are min, max, sum, count, and avg. For aggregate functions (and the following grouping), an extension of relational algebra exists. Examples: What is the total number of suppliers? select count(sname) from SUPPLIERS; How many different products are offered? select count(distinct Prodname) from offers; What is the minimum and maximum price for products offered by Davis Lumber? select min(price), max(price) from offers where SName = Davis Lumber ; What is the average price for a MegaPC? select avg(price) from offers where Prodname = MegaPC ;

ECS-165A WQ 11 82 Aggregate Functions (cont.) What is the total price for the products ordered by the customer Scott Tiger? select sum(price * Quantity) from CUSTOMERS C, orders O, offers F where C.FName=O.FName and C.LName = O.LName and O.Prodname = F.Prodname and O.SName = F.SName and C.FName = Scott and C.LName = Tiger ; Grouping Idea: Group tuples that have the same properties into groups, and apply aggregate function to each group. Optionally, consider only groups for the query result that satisfy a certain group condition. Syntax in SQL: select <attribute(s) [with aggregate function]> from R 1, R 2,..., R m [where P ] group by <grouping attribute(s)> [having <condition on group>];

ECS-165A WQ 11 83 Grouping Examples: For each supplier, list the name of the supplier and the total number of products the supplier offers. select SName, count(prodname) from offers group by SName; For each customer, list the total quantity of orders. select FName, LName, sum(quantity) from orders group by FName, LName; Note: attributes that appear in the select clause outside of an aggregate function must appear in the group by clause! List products that are offered by more than one supplier, together with the minimum and maximum price of these offers. select Prodname, min(price), max(price) from offers group by Prodname having count( ) > 1;

ECS-165A WQ 11 84 Grouping (cont.) A query containing a group by clause is processed in the following way: 1. Select all rows that satisfy the condition specified in the where clause. 2. From these rows form groups according to the group by clause. 3. Discard all groups that do not satisfy the condition in the having clause. 4. Apply aggregate function(s) to each group. 5. Retrieve values for the columns and aggregations listed in the select clause. More examples: List all suppliers from Davis that offer more than 10 products. select O.SName, count(prodname) from SUPPLIERS S, offers O where S.SName = O.SName and SAddress like %Davis% group by O.SName having count(prodname) > 10;

ECS-165A WQ 11 85 Grouping (cont.) List the names of customers who have ordered products for more than $10,000. select C.FName, C.LName, sum(quantity Price) from CUSTOMERS C, orders O, offers F where C.FName=O.FName and C.LName = O.LName and O.Prodname = F.Prodname and O.SName = F.SName group by C.FName, C.LName having sum(quantity*price) > 10000; What is the minimum total quantity of all orders for a product? select min(sum(quantity)) from orders group by Prodname;

ECS-165A WQ 11 86 Data Definition Language (DDL) Allows the specification of not only a set of relations but also information about each relation, including The schema of a relation The domain of attributes Integrity constraints The set of indexes associated with a relation (later) The physical storage structure of a relation (later) Data Types in SQL char(n), varchar2(n) (in SQL standard only varchar(n)) number(m, n), real, int, smallint,... long, date Creating a Table Syntax: create table <name> ( <attribute 1> <data type> [not null] [unique] [<attribute constraint>],......... <attribute n> <data type> [not null] [unique] [<attribute constraint>], [<table constraint(s)>] );

ECS-165A WQ 11 87 Integrity Constraints not null (do not allow null values) primary key <attribute> (as attribute constraint) primary key (<list of attributes>) (as table constraint) unique <attribute> unique (<list of attributes>) (as attribute constraint) (as table constraint) check <condition> If <condition> only refers to one attribute attribute constraint; if <condition> includes more than one attribute of the relation table constraint; <condition> must be a simple condition that does not contain queries or references to other relations! Foreign key (or referential integrity) constraints: references <relation>[.<attribute>] attribute constraint foreign key <attributes> references <relation>[.<attributes>] table constraint

ECS-165A WQ 11 88 Example create table Students ( StID number(9) constraint Students pk primary key, FName varchar2(50) not null, LName varchar2(50) not null, DOB date constraint dob check check(dob is not null and to char(dob) > 01-JAN-01 ), Major char(5) constraint fk majors references Majors, ZipCode integer constraint check zip check(zipcode is not null and ZipCode between 1 and 99999), City varchar2(50), Street varchar2(50), Started date not null, constraint dates check check(dob < Started), constraint name add unique(fname, LName, DOB) ); As usual, different database systems (PostgreSQL, Oracle, etc.) can differ in syntax and capabilities (cf. reference manual).

ECS-165A WQ 11 89 Modifications of the Database I. Deletions: Syntax: delete from <relation> [where <condition>]; Examples: Delete all suppliers that don t offer any product. delete from SUPPLIERS where SName not in (select SName from offers); Delete all customers having an account less than the average account of all customers. delete from CUSTOMERS where Account < (select avg(account) from CUSTOMERS); Problem: Evaluating the condition after each deletion of a customer tuple leads to a change of the subquery result. In SQL: First compute avg(account) and identify tuples from CUSTOMERS to delete; then delete those tuples without recomputing avg(account).

ECS-165A WQ 11 90 II. Insertions Add the customer Scott Tiger (who is living in Davis). insert into CUSTOMERS values( Scott, Tiger, Davis,null); ˆ= insert into CUSTOMERS(FName, LName, CAddress, Account) values( Scott, Tiger, Davis,null); or insert into CUSTOMERS(FName, LName, CAddress) values( Scott, Tiger, Davis ); All suppliers are also customers. insert into CUSTOMERS(FName, LName, CAddress, Account) select -, SName, SAddress, 0 from SUPPLIERS; III. Updates Increase the Account of the customer Scott Tiger by $5,000, and change his address to Woodland. update CUSTOMERS set Account = Account+5000, CAddress = Woodland where LName= Tiger and FName= Scott ; Set Clark Kent s account to the account of Scott Tiger. update CUSTOMERS set Account = (select Account from CUSTOMERS where LName= Tiger and FName= Scott ) where FName= Clark and LName= Kent ;

ECS-165A WQ 11 91 Views Offer a flexible mechanism to hide certain data from the view of a certain user or application; used to realize external schema definitions in the three level schema architecture Syntax of a view definition: create view <name>[(<list of attribute names>)] as <query>; The result set of a view is materialized only when the view is queried only the definition of a view requires space Examples: create view PC SUPPLS as select SName, SAddress, Chain from SUPPLIERS S where exists (select from offers where SName = S.SName and Prodname = MegaPC ); create view GOOD CUSTS(CName, CFName) as select LName, FName from CUSTOMERS C where 10000 < (select sum(price Quantity) from orders O, offers R where O.SName=R.SName and O.FName=C.FName and O.LName=C.LName and O.Prodname=R.Prodname) ;

ECS-165A WQ 11 92 Modifications of a View Consider the view CUST ORDERS(FName, LName, Prodname, SName, Quantity) defined as select C.FName, C.LName, Prodname, SName, Quantity from CUSTOMERS C, orders O where C.FName=O.FName and C.LName=O.LName; View Update Problem: Insert, delete, and update operations on a view must be translated into respective operations of the underlying relations. No problem if there is only one relation underlying the view definition. Delete the customer Scott Tiger from CUST ORDERS. Possibility A: delete Scott Tiger from CUSTOMERS Possibility B: delete Scott Tiger from orders Rules: In Oracle SQL no insert, update, or delete modifications on views are allowed that use one of the following constructs in the view definition: Joins Aggregate function such as sum, min, max etc. set-valued subqueries (in, any, all) or test for existence (exists) group by clause or distinct clause