Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
FEN More about SELECT, Nested selects GROUP BY, HAVING, ORDER BY Other joins Aggregate functions Views More about SQL.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1.
Database Systems More SQL Database Design -- More SQL1.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Advanced Queries.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Slide Chapter 8 SQL: SchemaDefinition, Constraints, and Queries and Views.
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Structured Query Language (2) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
Faculty of Information Science and Technology Mahanakorn University of Technology Topic 8 SQL-99: Schema Definition, Basic Constraints, and Queries.
SQL SQL-99: a. Schema Definition b. Basic Constraints c. Queries.
Chapter 11 SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not.
1 Chapter 5 The SQL Language Data Definition Language The Data Definition Language (DDL) is used to create and destroy databases and database.
Chapter SQL DML. Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL-99: SchemaDefinition, Constraints, and Queries and Views.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 20 A First Course in Database Systems.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 7 More SQL: Complex Queries, Triggers, Views, and Schema Modification Slide 7- 1.
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Lecture # 25 Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views Muhammad Emran Database Systems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS580 Advanced Database Topics
6/22/2018.
Retrieval Queries in SQL(DML)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
11/9/2018.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL-99: Schema Definition, Constraints, and Queries and Views
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Outline More Complex SQL Retrieval Queries Specifying Additional Constraints and Actions in SQL – CREATE ASSERTION – CREATE TRIGGER Views (virtual tables) in SQL – CREATE VIEW Schema Modification in SQL – ADD, DROP statements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline of Topics for More Complex SQL Retrieval Queries Handling NULLs, 3-valued Logic in SQL Nested Queries – Correlated vs. uncorrelated – EXISTS function Joined Tables, Inner Joins, and Outer Joins Aggregate Functions and Grouping in SQL – COUNT, AVG, SUM, MIN, MAX functions – GROUP BY, HAVING clauses

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparisons Involving NULL and Three-Valued Logic  Meanings of NULL  Unknown value  Unavailable or withheld value  Not applicable attribute  Each individual NULL value considered to be different from every other NULL value  SQL uses a three-valued logic:  TRUE, FALSE, and UNKNOWN

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-valued Logic in SQL Standard 2-valued logic assumes a condition can evaluate to either TRUE or FALSE With NULLs a condition can evaluate to UNKNOWN, leading to 3-valued logic Example: Consider a condition EMPLOYEE.DNO = 5; this evaluates for individual tuples in EMPLOYEE as follows: – TRUE for tuples with DNO=5 – UNKNOWN for tuples where DNO is NULL – FALSE for other tuples in EMPLOYEE

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-valued Logic in SQL (cont.) Combining individual conditions using AND, OR, NOT logical connectives must consider UNKNOWN in addition to TRUE and FALSE Next slide (Table 5.1) shows the truth tables for 3-valued logic

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Comparisons Involving NULL and Three-Valued Logic (cont’d.)  SQL allows queries that check whether an attribute value is NULL  IS or IS NOT NULL

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Queries, Tuples, and Set/Multiset Comparisons  Nested queries  Complete select-from-where blocks within WHERE clause of another query  Outer query  Comparison operator IN  Compares value v with a set (or multiset) of values V  Evaluates to TRUE if v is one of the elements in V

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Queries (cont’d.)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Queries (cont’d.)  Use tuples of values in comparisons  Place them within parentheses

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley  Use other comparison operators to compare a single value v  = ANY (or = SOME ) operator Returns TRUE if the value v is equal to some value in the set V and is hence equivalent to IN  Other operators that can be combined with ANY (or SOME ): >, >=, Nested Queries (cont’d.)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Another Example Query 1: Retrieve the name and address of all employees who work for the 'Research' department. Q1:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE WHEREDNO IN (SELECT DNUMBER FROMDEPARTMENT WHEREDNAME='Research' ) ; In Q1, the nested query selects the DNUMBER of the 'Research' department The outer query select an EMPLOYEE tuple if its DNO value is in the result of the nested query

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nesting of Queries (cont.) In general, can have several levels of nested queries A reference to an unqualified attribute refers to the relation declared in the innermost nested query In Q1, the nested query is not correlated with the outer query

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Correlated Nested Queries If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query, the two queries are said to be correlated –The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query Query 16: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q16: SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT D.ESSN FROMDEPENDENT AS D WHEREE.FNAME=D.DEPENDENT_NAME) ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Correlated Nested Queries (cont.) In Q16, the nested query has a different result for each tuple in the outer query (because it refers to E.FNAME) A query written with nested SELECT... FROM... WHERE... blocks and using the = or IN comparison operators can always be expressed as a single block query. For example, Q16 may be written as in Q16A Q16A:SELECT E.FNAME, E.LNAME FROMEMPLOYEE E, DEPENDENT D WHEREE.SSN=D.ESSN ANDE.FNAME=D.DEPENDENT_NAME ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley CAUTION Are these 2 queries equivalent? Q16: SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT D.ESSN FROMDEPENDENT AS D WHEREE.FNAME=D.DEPENDENT_NAME) ; Q16A:SELECT E.FNAME, E.LNAME FROMEMPLOYEE E, DEPENDENT D WHEREE.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME ; They are not. (If an employee and two dependents all have the same first name, Q16 will report the employee once, Q16A will report it twice. This is due to bag semantics.)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley CAUTION Are these 2 queries equivalent? Q16: SELECT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT D.ESSN FROMDEPENDENT AS D WHEREE.FNAME=D.DEPENDENT_NAME) ; Q16A-1:SELECT DISTINCT E.FNAME, E.LNAME FROMEMPLOYEE E, DEPENDENT D WHEREE.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME ; They are not. (If there are two employees with the same first name and last name, and their dependents also have the same first name, Q16 will report both employees (same first name/last name twice, Q16A will report only one result tuple from them. This is also due to bag semantics.)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley CAUTION Are these 2 queries equivalent? Q16-1: SELECT DISTINCT E.FNAME, E.LNAME FROMEMPLOYEE AS E WHEREE.SSN IN (SELECT D.ESSN FROMDEPENDENT AS D WHEREE.FNAME=D.DEPENDENT_NAME) ; Q16A-1:SELECT DISTINCT E.FNAME, E.LNAME FROMEMPLOYEE E, DEPENDENT D WHEREE.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME ; Yes, they are.

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The EXISTS Function in SQL EXISTS is used to check whether the result of a query is empty (contains no tuples) or not (contains one or more tuples) –Applied to a query, but returns a boolean result (TRUE or FALSE) –Can be used in the WHERE-clause as a condition –EXISTS (Q) evaluates to TRUE if the result of Q has one or more tuple; evaluates to FALSE if the result of Q has no tuples

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The EXISTS Function (cont.) Query 7: Retrieve the names of employees who are department managers and have at least one dependent. Q7: SELECT M.FNAME, M.LNAME FROMEMPLOYEE AS M WHEREEXISTS (SELECT* FROMDEPENDENT WHEREM.SSN=ESSN) AND EXISTS (SELECT * FROMDEPARTMENT WHERE M.SSN=MGRSSN) ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The EXISTS Function (cont.) Query 6: Retrieve the names of employees who have no dependents. Q6:SELECT E.FNAME, E.LNAME FROMEMPLOYEE E WHERENOT EXISTS (SELECT* FROM DEPENDENT D WHERE E.SSN=D.ESSN) ; In Q6, the correlated nested query retrieves all DEPENDENT tuples related to an EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected –EXISTS is necessary for the expressive power of SQL

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Explicit (Literal) Sets in SQL An explicit (enumerated) set of values is enclosed in parentheses Query 13: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. Q13:SELECT DISTINCT ESSN FROMWORKS_ON WHEREPNO IN (1, 2, 3) ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Joined Tables (Relations) in SQL Can specify a "joined relation" in the FROM- clause –Looks like any other relation but is the result of a join –Allows the user to specify different types of joins (INNER JOIN, NATURAL JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, CROSS JOIN, etc) – see the next slides –Each join type can specify a different query and produce a different result

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – INNER JOIN This is the regular join operation Joined tuples must satisfy all join conditions Example: Query QJ1: Retrieve the employee names with the names of the department they work for SELECTE.FNAME, E.LNAME, D.DNAME FROM DEPARTMENT AS D, EMPLOYEE AS E WHERE D.DNUMBER=E.DNO ; This can be written using joined tables as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM (DEPARTMENT AS D JOIN EMPLOYEE AS E ON D.DNUMBER=E.DNO) ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – OUTER JOIN In QJ1, an EMPLOYEE record is joined only if it has a matching DEPARTMENT with D.DNUMBER=E.DNO Hence, an EMPLOYEE with NULL for E.DNO will not appear in the query result Also, a DEPARTMENT that has no matching EMPLOYEE records (i.e. currently has no employees) does not appear in the query result OUTER JOINs gives the options to include every EMPLOYEE record or every DEPARTMENT record in the query results A record that does not have a matching joined record will be “padded” with an imaginary “NULL record” from the other table (all its attributes will be NULL)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – LEFT OUTER JOIN Example: Query QJ2: Retrieve the employee names with the names of the department they work for; every department must appear in the result even if it has no employees This can be written using joined tables as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM (DEPARTMENT AS D LEFT OUTER JOIN EMPLOYEE AS E ON D.DNUMBER=E.DNO) ; Note: An earlier left outer join syntax in ORACLE is as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM DEPARTMENT AS D, EMPLOYEE AS E WHERE D.DNUMBER += E.DNO ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – RIGHT OUTER JOIN Example: Query QJ3: Retrieve the employee names with the names of the department they work for; every employee must appear in the result even they are not currently assigned to a department This can be written using joined tables as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM (DEPARTMENT AS D RIGHT OUTER JOIN EMPLOYEE AS E ON D.DNUMBER=E.DNO) ; Note: An earlier right outer join syntax in ORACLE is as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM DEPARTMENT AS D, EMPLOYEE AS E WHERE D.DNUMBER =+ E.DNO ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – FULL OUTER JOIN Example: Query QJ4: Retrieve the employee names with the names of the department they work for; every employee and every department must appear in the result This can be written using joined tables as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM (DEPARTMENT AS D FULL OUTER JOIN EMPLOYEE AS E ON D.DNUMBER=E.DNO) ; Note: An earlier full outer join syntax in ORACLE is as follows: SELECTE.FNAME, E.LNAME, D.DNAME FROM DEPARTMENT AS D, EMPLOYEE AS E WHERE D.DNUMBER +=+ E.DNO ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Types of join – NATURAL JOIN If the join attributes in both tables have the same name, the join condition can be left out (it is automatically added by the system) NATURAL JOIN is a form of inner join Example: QJ5: We rename DNUMBER in DEPARTMENT to DNO to match the join attribute name (DNO) in EMPLOYEE (we also rename other attributes) Implicit join condition is E.DNO = D.DNO SELECTE.FN, E.LN, E. ADR FROM (DEPARTMENT AS D(DNM, DNO, MSSN, STRDATE) NATURAL JOIN EMPLOYEE AS E(FN,MI,LN,S,BD,ADR,SX,SAL,SU,DNO)) ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Joined Tables – Other Examples Query 8: Retrieve the employee names, and the names of their direct supervisor Q8:SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE AS E INNER JOIN EMPLOYEE AS S ON E.SUPERSSN=S.SSN); In Q8, an employee with no supervisor will not appear in the result; if we want every employee to appear, we write: Q8':SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN=S.SSN)

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Joined Tables – Other Examples Examples: Q1:SELECTFNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHEREDNAME='Research' AND DNUMBER=DNO ; could be written as: Q1:SELECTFNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNUMBER=DNO) WHEREDNAME='Research’ ; or as: Q1:SELECTFNAME, LNAME, ADDRESS FROM (EMPLOYEE NATURAL JOIN DEPARTMENT AS DEPT(DNAME, DNO, MSSN, MSDATE)) WHEREDNAME='Research’ ;

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Joined Tables – Other Examples Another Example: Q2 could be written as follows; this illustrates multiple joins in the joined tables Q2:SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM( (PROJECT JOIN DEPARTMENT ONDNUM=DNUMBER) JOINEMPLOYEE ON MGRSSN=SSN) ) WHERE PLOCATION='Stafford’ ;