Chapter 5. SQL: Queries, Constraints, Triggers



Similar documents
SQL: Queries, Programming, Triggers

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

SQL: Queries, Programming, Triggers

Relational Algebra and SQL

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

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

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

Θεµελίωση Βάσεων εδοµένων

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

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

Proposed solutions Project Assignment #1 (SQL)

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

The Relational Model. Why Study the Relational Model?

Introduction to Microsoft Jet SQL

Oracle SQL. Course Summary. Duration. Objectives

Lecture 6. SQL, Logical DB Design

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

Query-by-Example (QBE)

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

Databases and BigData

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

6 QUERY-BY-EXAMPLE (QBE)

IT2304: Database Systems 1 (DBS 1)

Relational Database: Additional Operations on Relations; SQL

IT2305 Database Systems I (Compulsory)

Database Security. Chapter 21

Instant SQL Programming

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

SQL NULL s, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals

DBMS / Business Intelligence, SQL Server

Databases 2011 The Relational Model and SQL

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

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Introduction to SQL: Data Retrieving

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

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Oracle Database 10g: Introduction to SQL

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

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

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

Oracle Database: SQL and PL/SQL Fundamentals NEW

Relational Databases

3. Relational Model and Relational Algebra

INFO/CS 330: Applied Database Systems

Oracle Database: SQL and PL/SQL Fundamentals

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

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

Lecture 4: More SQL and Relational Algebra

SQL Tables, Keys, Views, Indexes

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

Database Management Systems. Chapter 1


Oracle Database 11g SQL

Advance DBMS. Structured Query Language (SQL)

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

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

Information Systems SQL. Nikolaj Popov

Oracle Database 12c: Introduction to SQL Ed 1.1

EECS 647: Introduction to Database Systems

Introduction to database design

How To Create A Table In Sql (Ahem)

1 Structured Query Language: Again. 2 Joining Tables

Normal Form vs. Non-First Normal Form

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

BCA. Database Management System

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

SQL SELECT Query: Intermediate

Lecture 6: Query optimization, query tuning. Rasmus Pagh

Data Modeling. Database Systems: The Complete Book Ch ,

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

Programming with SQL

SQL. Short introduction

Financial Data Access with SQL, Excel & VBA

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

3.GETTING STARTED WITH ORACLE8i

There are five fields or columns, with names and types as shown above.

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

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

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21

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

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

CSC 443 Data Base Management Systems. Basic SQL

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

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

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

Effective Use of SQL in SAS Programming

SQL Server Database Coding Standards and Guidelines

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Procedural Extension to SQL using Triggers. SS Chung

Oracle 10g PL/SQL Training

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

Database Query 1: SQL Basics

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

Chapter 13: Query Optimization

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

CS143 Notes: Views & Authorization

Structured Query Language (SQL)

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

Transcription:

Chapter 5 SQL: Queries, Constraints, Triggers 1

Overview: aspects of SQL DML: Data Management Language. Pose queries (Ch. 5) and insert, delete, modify rows (Ch. 3) DDL: Data Definition Language. Creation, deletion and modification of tables or views (Ch. 3) Triggers and Advanced Integrity Constraints (Ch 5) Triggers are actions executed by DBMS whenever changes to the database meet conditions specified in the trigger Embedded and dynamic SQL (Ch. 6). Allow SQL to be called from a host language such as C Client-server execution and remote database access (Ch. 7) Transaction management (Ch. 21) Security Advanced features OO, recursive, decision support, data mining, spatial data, text, XML 2

Example Instances Sailors(sid: integer, sname: string, rating: integer, age: real) Boats(bid: integer, bname: string, color: string) Reserves(sid: integer, bid: integer, day: date) 3

enter data into MySql create table sailors(sid integer, sname char(20), rating integer, age real, primary key(sid)); create table boats(bid integer, bname char(20), color char(20), primary key(bid)); create table reserves(sid integer, bid integer, day date, primary key(sid,bid,day), foreign key(sid) references sailors, foreign key(bid) references boats); insert into sailors(sid,sname,rating,age) values(22,"dustin",7,45); insert into boats(bid,bname,color) values(101,"interlake","blue"); insert into reserves(sid,bid,day) values(22,101,"1998-10-10"); 4

Basic SQL Query SELECT FROM WHERE [DISTINCT] select-list from-list qualification from-list: A list of relation names, specifying a crossproduct of tables select-list: A list of attributes of relations in from-list, specifying columns to be retained in the result qualification: optional. Specifies select conditions on the tables mentioned in the from-list DISTINCT: optional keyword indicating that the answer should not contain duplicates By default duplicates are not eliminated 5

Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: Compute the cross-product of from-list Discard resulting tuples if they fail qualifications Delete attributes that are not in select-list If DISTINCT is specified, eliminate duplicate rows This strategy is probably the least efficient way to compute a query optimizer will find more efficient strategies to compute the same answers 6

Example of Conceptual Evaluation SELECT S.sname, Reserves R WHERE S.sid=R.sid and R.bid=103; (sid) sname rating age (sid) bid day 22 Dustin 7 45.0 22 101 10/10/98 29 Brutus 1 33.0 22 101 10/10/98 22 Dustin 7 45.0 22 103 10/8/98 select * from Sailors, Reserves; select * from Sailors S, Reserves R where S.sid=R.sid and R.bid=103; select S.sname from Sailors S, Reserves R where S.sid=R.sid and R.bid=103; 7

A Note on Range Variables from-list: table name can be followed by a range variable select-list: column name can be prefixed by a range variable When is it necessary? Really needed only if the same relation appears twice in from-list. Previous query can also be written as: SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103; However, as a good style, use range variables always! 8

Some examples Find the sids of sailors who ve reserved a red boat SELECT R.sid FROM Boats B, Reserves R WHERE B.bid=R.bid and B.color= red ; Find the names of sailors who ve reserved a red boat SELECT S.sname, Boats B, Reserves R WHERE S.sid=R.sid and B.bid=R.bid and B.color= red ; Find the colors of boats reserved by Lubber SELECT B.color, Boats B, Reserves R WHERE S.sid=R.sid and B.bid=R.bid and S.sname= Lubber ; 9

Find sailors who ve reserved at least one boat SELECT S.sid, Reserves R WHERE S.sid=R.sid; Would adding DISTINCT to this query make a difference? What is the effect of replacing S.sid by S.sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference? 10

Expressions and Strings SELECT S.age, age1=s.age-5, 2*S.age AS age2 WHERE S.sname LIKE B_%B Illustrates use of arithmetic expressions and string pattern matching: Find triples (of ages of sailors and two fields defined by expressions) for sailors whose names begin and end with B and contain at least three characters. AS and = are two ways to name fields in result LIKE is used for string matching. `_ stands for any one character and `% stands for 0 or more arbitrary characters 11

Find names of sailors who ve reserved a red or a green boat UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries) SELECT S.sname, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color= red OR B.color= green ) SELECT S.sname, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red UNION SELECT S2.sname 2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color= green 12

Find names of sailors who ve reserved a red and a green boat INTERSECT: Can be used to compute the intersection of any two unioncompatible sets of tuples Included in the SQL/92 standard, but some systems don t support it Upper query hard to understand, more intuitive to use intersect Problem? SELECT S.sname, Boats B1, Reserves R1, Boats B2, Reserves R2 WHERE S.sid=R1.sid AND R1.bid=B1.bid AND S.sid=R2.sid AND R2.bid=B2.bid AND (B1.color= red AND B2.color= green ) SELECT S.sname, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red INTERSECT SELECT S2.sname 2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color= green 13

Find sid s of sailors who ve reserved red but not green boats SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color= red EXCEPT SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green 14

Find all sid s of sailors who have a rating of 10 or reserved boat 104 any two union-compatible sets of tuples For UNION, INTERSECTION and EXCEPT, by default duplicates are eliminated SELECT S.sid WHERE S.rating = 10 UNION SELECT R.sid FROM Reserves R WHERE R.bid=104 15

Nested Queries Find names of sailors who ve reserved boat #103: SELECT S.sname WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid=103) Powerful feature of SQL: a WHERE clause can itself contain an SQL query! (Actually, so can FROM and HAVING clauses.) Why can be nested? IN tests whether a value is in a set To find sailors who ve not reserved #103, use NOT IN. 16

Nested Queries Find names of sailors who ve reserved a red boat SELECT S.sname WHERE S.sid IN (SELECT R.sid FROM Reserves R WHERE R.bid IN (SELECT B.bid FROM Boats B WHERE B.color= red ) Find names of sailors who have not reserved a red boat How about replace both to NOT IN 17

Nested Queries with Correlation Find names of sailors who ve reserved boat #103: SELECT S.sname WHERE EXISTS (SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) EXISTS is another set comparison operator, like IN Tests whether a set is non-empty NOT EXISTS: tests whether a set is empty If UNIQUE is used, and * is replaced by R.bid, finds sailors with at most one reservation for boat #103 UNIQUE checks for duplicate tuples Why do we have to replace * by R.bid? 18

Correlated vs. Non-correlated Two types of sub query: correlated non-correlated. Difference: A correlated subquery is evaluated once per row processed by the parent query A non-correlated subquery is only executed once and the result set is kept in memory (if the result set is small) 19

More on Set-Comparison Operators We ve already seen IN, EXISTS and UNIQUE. Can also use NOT IN, NOT EXISTS and NOT UNIQUE Also available: op ANY, op ALL op :,,,,, Find sailors whose rating is greater than some sailor called Horatio: SELECT * WHERE S.rating > ANY (SELECT S2.rating 2 WHERE S2.sname= Horatio ) Find sailors whose rating is greater than every sailor called Horatio 20

Find the sailors with the highest rating SELECT * WHERE S.rating >= ALL (SELECT S2.rating 2) IN equivalent to = ANY NOT IN equivalent to <> ALL 21

Rewriting INTERSECT Queries Using IN Find the names of sailors who ve reserved both a red and a green boat: SELECT S.sname, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color= red AND S.sid IN (SELECT S2.sid 2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color= green ) Similarly, EXCEPT queries re-written using NOT IN. 22

Using INTERSECT Find names of sailors who ve reserved both a red and a green boat SELECT S.sname WHERE S.sid IN (( SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color= red ) INTERSECT (SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid=B2.bid AND B2.color= green )) 23

Division in SQL Find sailors who ve reserved all boats SELECT S.sname WHERE NOT EXISTS ((SELECT B.bid FROM Boats B) EXCEPT (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) 24

Aggregate Operators Significant extension of relational algebra In addition to simply retrieving data, we often want to perform some computation and summarization COUNT (*) : num of tuples COUNT ( [DISTINCT] A): num of (unique) values in column A SUM ( [DISTINCT] A) : sum of all (unique) values in column A AVG ( [DISTINCT] A) : average of (unique) values in column A MAX (A) : max value in column A MIN (A) : min value in column A 25

Aggregate Examples SELECT COUNT (*) SELECT COUNT (DISTINCT S.sname) SELECT AVG (S.age) WHERE S.rating=10 SELECT S.sname, S.age WHERE S.age= (SELECT MAX(S2.age) 2) SELECT S.sname, MAX (S.age) Illegal! If the SELECT clause uses an aggregate, then it must use only aggregate unless the query contains GROUP BY 26

Find the sailors with the highest rating SELECT * WHERE S.rating >= ALL (SELECT S2.rating 2) SELECT * WHERE S.rating >= (SELECT MAX (S2.rating) 2) 27

GROUP BY and HAVING So far, we ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples Find the age of the youngest sailor for each rating level In general, we don t know how many rating levels exist, and what the rating values for these levels are Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this: For i = 1, 2,..., 10: SELECT MIN (S.age) WHERE S.rating = i 28

Queries With GROUP BY and HAVING SELECT [DISTINCT] select-list FROM from-list WHERE qualification GROUP BY grouping-list HAVING group-qualification SELECT S.rating, MIN (S.age) GROUP BY S.rating The select-list contains (i) column names (ii) terms with aggregate operations (e.g., MIN (S.age)) The column names in (i) must appear in the grouping-list Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group Aggregates always return a single value A group is a set of tuples having the same value for all attributes in grouping-list If not subset, i.e., a column appears in list (i) but not grouping list, there can be multiple rows within a group that have different values in this column, and it is not clear what value should be assigned to this column in an answer row If GROUP BY is omitted, entire table is considered as a single group 29

Conceptual Evaluation The cross-product of from-list is computed, tuples that fail qualification are discarded, `unnecessary fields are deleted, and the remaining tuples are partitioned into groups by the value of attributes in grouping-list The group-qualification is then applied to eliminate some groups. Expressions in group-qualification must have a single value per group! One answer tuple is generated per qualifying group If DISTINCT, duplicates are eliminated 30

Find the age of the youngest sailor with age 18, for each rating with at least 2 such sailors SELECT S.rating, MIN (S.age) WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes `unnecessary Use AS to rename 2nd column SELECT S.rating, MIN (S.age) AS minage 31

More examples of aggregate queries Find the age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) SELECT S.rating, MIN (S.age) WHERE S.age > 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT (*) 2 WHERE S.rating=S2.rating) Shows HAVING clause can also contain a subquery Compare this with the query where we considered only ratings with at least 2 sailors over 18 32

For each red boat, find the number of reservations for this boat SELECT B.bid, COUNT (*) AS reservationcount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color= red GROUP BY R.bid 33

Find those ratings for which the average age is the minimum over all ratings SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age) AS avgage GROUP BY S.rating) AS Temp WHERE Temp.avgage = (SELECT MIN (Temp.avgage) FROM Temp) Not supported by MySql! How to solve? How about Oracle? SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age) AS avgage GROUP BY S.rating) AS Temp WHERE Temp.avgage = (SELECT MIN (Temp2.avgage2) FROM (SELECT S2.rating, AVG (S2.age) AS avgage2 2 GROUP BY S2.rating) AS Temp2) 34

Null Values Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) or inapplicable (e.g., no spouse s name) SQL provides a special value null for such situations The presence of null complicates many issues Special operators needed to check if value is/is not null Is rating>8 true or false when rating is equal to null? We need a 3-valued logic (true, false and unknown) Meaning of constructs must be defined carefully (e.g., WHERE clause eliminates rows that don t evaluate to true) NOT NULL Implicit for each field in PRIMARY KEY constraint 35

Integrity Constraints (Review) An IC describes conditions that every legal instance of a relation must satisfy Inserts/deletes/updates that violate IC s are disallowed Can be used to ensure application semantics (e.g., sid is a key), or prevent inconsistencies (e.g., sname has to be a string, age must be < 200) Types of IC s: Domain constraints, primary key constraints, foreign key constraints, general constraints (table constraints, assertions) 36

Table Constraints Interlake boats cannot be reserved Can use queries to express constraint Can be named CREATE TABLE Sailors ( sid INTEGER, sname CHAR(10), rating INTEGER, age REAL, PRIMARY KEY (sid), CHECK ( rating >= 1 AND rating <= 10 ) CREATE TABLE Reserves ( sid INTEGER bid INTEGER, day DATE, PRIMARY KEY (sid,bid,day), FOREIGN KEY (sid) REFERENCES Sailors Rating must be in the range of 1 to 10 FOREIGN KEY (bid) REFERENCES Boats CONSTRAINT nointerlakeres CHECK (`Interlake <> ( SELECT B.bname FROM Boats B WHERE B.bid=Reserves.bid))) 37

Assertions Constraints over multiple tables Not associated with a single table as table constraints CREATE ASSERTION smallclub CHECK ( (SELECT COUNT (S.sid) ) + (SELECT COUNT (B.bid) FROM Boats B) < 100 ) Number of boats plus number of sailors is < 100 38

Triggers Trigger: procedure that starts automatically if specified changes occur to the DBMS Three parts: Event (activates the trigger) Condition (tests whether the triggers should run) Action (what happens if the trigger runs) Useful in practice MySql 5.0 new feature 39

Summary SQL was an important factor in the early acceptance of the relational model; more natural than earlier, procedural query languages Relationally complete; in fact, significantly more expressive power than relational algebra Many alternative ways to write a query; optimizer should look for most efficient evaluation plan In practice, users need to be aware of how queries are optimized and evaluated for best results 40

Summary (Contd.) NULL for unknown field values brings many complications SQL allows specification of rich integrity constraints Triggers respond to changes in the database 41