Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.

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

Introduction to Structured Query Language (SQL)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Chapter 4B: More Advanced PL/SQL Programming
© 2007 by Prentice Hall 1 Chapter 8: Advanced SQL Modern Database Management 8 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 8 (Part b): Advanced SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer,
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Introduction to Databases Chapter 7: Data Access and Manipulation.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Modern Database Management
1 Chapter 8: Advanced SQL. Chapter 8 2 Processing Multiple Tables – Joins Join – a relational operation that causes two or more tables with a common domain.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 © Prentice Hall, 2002 Chapter 8: Advanced SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Modern Database Management Chapter 8: Advanced SQL.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Technology Jing Shen.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
implicit and an explicit cursor
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 8 Advanced SQL. Relational Set Operators UNIONINTERSECTMINUS Work properly if relations are union- compatible –Names of relation attributes must.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL Triggers, Functions & Stored Procedures Programming Operations.
 CONACT UC:  Magnific training   
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 8 1 Lecture Advanced SQL. Chapter 8 2 Processing Multiple Tables–Joins Join – a relational operation that causes two or more tables with.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
A Guide to SQL, Seventh Edition
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Database Systems: Design, Implementation, and Management Tenth Edition
Modern Database Management Jeffrey A. Hoffer, Mary B. Prescott,
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Chapter 8 Advanced SQL

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Learning Objectives  In this chapter, the student will learn:  How to use the advanced SQL JOIN operator syntax  About the different types of subqueries and correlated queries  How to use SQL functions to manipulate dates, strings, and other data  About the relational set operators UNION, UNION ALL, INTERSECT, and MINUS 2

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Learning Objectives  In this chapter, the student will learn:  How to create and use views and updatable views  How to create and use triggers and stored procedures  How to create embedded SQL 3

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. SQL Join Operators  Relational join operation merges rows from two tables and returns rows with one of the following  Natural join - Have common values in common columns  Equality or inequality - Meet a given join condition  Outer join: Have common values in common columns or have no matching values  Inner join: Only rows that meet a given criterion are selected 4

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table SQL Join Expression Styles 5

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table SQL Join Expression Styles 6

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Subqueries and Correlated Queries  Subquery is a query inside another query  Subquery can return:  One single value - One column and one row  A list of values - One column and multiple rows  A virtual table - Multicolumn, multirow set of values  No value - Output of the outer query might result in an error or a null empty set 7

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. WHERE Subqueries  Uses inner SELECT subquery on the right side of a WHERE comparison expression  Value generated by the subquery must be of a comparable data type  If the query returns more than a single value, the DBMS will generate an error  Can be used in combination with joins 8

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. IN and HAVING Subqueries  IN subqueries  Used to compare a single attribute to a list of values  HAVING subqueries  HAVING clause restricts the output of a GROUP BY query by applying conditional criteria to the grouped rows 9

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Multirow Subquery Operators: ANY and ALL  ALL operator  Allows comparison of a single value with a list of values returned by the first subquery  Uses a comparison operator other than equals  ANY operator  Allows comparison of a single value to a list of values and selects only the rows for which the value is greater than or less than any value in the list 10

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. FROM Subqueries  FROM clause:  Specifies the tables from which the data will be drawn  Can use SELECT subquery 11

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Attribute List Subqueries  SELECT statement uses attribute list to indicate what columns to project in the resulting set  Inline subquery  Subquery expression included in the attribute list that must return one value  Column alias cannot be used in attribute list computation if alias is defined in the same attribute list 12

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Correlated Subquery  Executes once for each row in the outer query  Inner query references a column of the outer subquery  Can be used with the EXISTS special operator 13

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. SQL Functions  Functions always use a numerical, date, or string value  Value may be part of a command or may be an attribute located in a table  Function may appear anywhere in an SQL statement where a value or an attribute can be used 14

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. SQL Functions Date and time functionsNumeric functionsString functionsConversion functions 15

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Relational Set Operators  SQL data manipulation commands are set-oriented  Set-oriented: Operate over entire sets of rows and columns at once  UNION, INTERSECT, and Except (MINUS) work properly when relations are union-compatible  Union-compatible: Number of attributes are the same and their corresponding data types are alike  UNION  Combines rows from two or more queries without including duplicate rows 16

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Relational Set Operators  Syntax - query UNION query  UNION ALL  Produces a relation that retains duplicate rows  Can be used to unite more than two queries  INTERSECT  Combines rows from two queries, returning only the rows that appear in both sets  Syntax - query INTERSECT query 17

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Relational Set Operators  EXCEPT (MINUS)  Combines rows from two queries and returns only the rows that appear in the first set  Syntax  query EXCEPT query  query MINUS query  Syntax alternatives  IN and NOT IN subqueries can be used in place of INTERSECT 18

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Virtual Tables: Creating a View  View: Virtual table based on a SELECT query  Base tables: Tables on which the view is based  CREATE VIEW statement: Data definition command that stores the subquery specification in the data dictionary  CREATE VIEW command  CREATE VIEW viewname AS SELECT query 19

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Updatable Views  Used to update attributes in any base tables used in the view  Batch update routine: Pools multiple transactions into a single batch to update a master table field in a single operation  Updatable view restrictions  GROUP BY expressions or aggregate functions cannot be used  Set operators cannot be used  JOINs or group operators cannot be used 20

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Oracle Sequences  Independent object in the database  Have a name and can be used anywhere a value expected  Not tied to a table or column  Generate a numeric value that can be assigned to any column in any table  Table attribute with an assigned value can be edited and modified  Can be created and deleted any time 21

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Figure Oracle Sequence 22

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Procedural SQL  Performs a conditional or looping operation by isolating critical code and making all application programs call the shared code  Yields better maintenance and logic control  Persistent stored module (PSM): Block of code containing:  Standard SQL statements  Procedural extensions that is stored and executed at the DBMS server 23

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Procedural SQL  Procedural Language SQL (PL/SQL)  Use and storage of procedural code and SQL statements within the database  Merging of SQL and traditional programming constructs  Procedural code is executed as a unit by DBMS when invoked by end user  End users can use PL/SQL to create:  Anonymous PL/SQL blocks and triggers  Stored procedures and PL/SQL functions 24

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table PL/SQL Basic Data Types 25

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Triggers  Procedural SQL code automatically invoked by RDBMS when given data manipulation event occurs  Parts of a trigger definition  Triggering timing - Indicates when trigger’s PL/SQL code executes  Triggering event - Statement that causes the trigger to execute  Triggering level - Statement- and row-level  Triggering action - PL/SQL code enclosed between the BEGIN and END keywords 26

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Triggers  DROP TRIGGER trigger_name command  Deletes a trigger without deleting the table  Trigger action based on DML predicates  Actions depend on the type of DML statement that fires the trigger 27

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Stored Procedures  Named collection of procedural and SQL statements  Advantages  Reduce network traffic and increase performance  Reduce code duplication by means of code isolation and code sharing 28

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. PL/SQL Processing with Cursors  Cursor: Special construct used to hold data rows returned by a SQL query  Implicit cursor: Automatically created when SQL statement returns only one value  Explicit cursor: Holds the output of a SQL statement that may return two or more rows  Cursor-style processing involves retrieving data from the cursor one row at a time  Current row is copied to PL/SQL variables 29

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table Cursor Processing Commands 30

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table Cursor Attributes 31

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. PL/SQL Stored Functions  Stored function: Named group of procedural and SQL statements that returns a value  As indicated by a RETURN statement in its program code  Can be invoked only from within stored procedures or triggers 32

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Embedded SQL  SQL statements contained within an application programming language  Host language: Any language that contains embedded SQL statements  Differences between SQL and procedural languages  Run-time mismatch  SQL is executed one instruction at a time  Host language runs at client side in its own memory space 33

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Embedded SQL  Processing mismatch  Conventional programming languages process one data element at a time  Newer programming environments manipulate data sets in a cohesive manner  Data type mismatch  Data types provided by SQL might not match data types used in different host languages 34

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Embedded SQL  Embedded SQL framework defines:  Standard syntax to identify embedded SQL code within the host language  Standard syntax to identify host variables  Communication area used to exchange status and error information between SQL and host language 35

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Table SQL Status and Error Reporting Variables 36

©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Embedded SQL  Static SQL: Programmer uses predefined SQL statements and parameters  SQL statements will not change while application is running  Dynamic SQL: SQL statement is generated at run time  Attribute list and condition are not known until end user specifies them  Slower than static SQL  Requires more computer resources 37