Advanced SQL Advanced SQL Complex Queries, Joining Tables.

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

Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
1 Basic SQL Prof. Sin-Min Lee Department of Computer Science.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
S511 Session 10, IU-SLIS 1 Structured Query Language.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
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.
Chapter 7 Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Using Special Operators (LIKE and IN)
Database Systems: Design, Implementation, and Management Tenth Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 7 Introduction to Structured Query Language (SQL)
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 6 Procedural Language SQL and Advanced SQL Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
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 7 Introduction to Structured Query Language (SQL)
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
7 7 SQL Data Modification 4Spread throughout chapter 7.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1 Chapter 1 File Systems and Databases Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel l 工程資料庫專題 –NTUST/ME-2007Q4.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Mining real world data RDBMS and SQL. Index RDBMS introduction SQL (Structured Query language)
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
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.
BTM 382 Database Management Chapter 8 Advanced SQL Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia.
SQL Basics Review Reviewing what we’ve learned so far…….
Advanced SQL Advanced SQL Week 8 Comparison Operators
Structured Query Language (SQL) How to build something useful
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 5 Sequences.
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
COP 4610L: Applications in the Enterprise Spring 2005
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

Advanced SQL Advanced SQL Complex Queries, Joining Tables

Some Basic SQL Numeric Functions

Example Aggregate Function Operations COUNT MAX and MIN SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT; SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT WHERE P_PRICE <= 10.00; SELECT MIN(P_PRICE) FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT WHERE P_PRICE = (SELECT MAX(P_PRICE) FROM PRODUCT);

More Complex Queries and SQL Functions SUM SELECT SUM(P_ONHAND*P_PRICE) FROM PRODUCT; AVG SELECT P_DESCRIPT, P_ONHAND, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE > (SELECT AVG(P_PRICE) FROM PRODUCT) ORDER BY P_PRICE DESC;

More Complex Queries and SQL Functions (con’t.) Grouping data –Creates frequency distributions –Only valid when used with SQL arithmetic functions –HAVING clause operates like WHERE for grouping output SELECT P_SALECODE, MIN(P_PRICE) FROM PRODUCT GROUP BY P_SALECODE; SELECT V_CODE,COUNT(DISTINCT(P_CODE)),AVG(P_PRICE) FROM PRODUCT GROUP BY V_CODE HAVING AVG(P_PRICE) < 10;

More Complex Queries and SQL Functions Virtual tables: creating a view –CREATE VIEW command –Creates logical table existing only in virtual memory CREATE VIEW PRODUCT_3 AS SELECT P_DESCRIPT, P_ONHAND, P_PRICE FROM PRODUCT WHERE P_PRICE > 50.00; SQL indexes: used to improve the efficiency of data searches CREATE INDEX P_CODEX ON PRODUCT(P_CODE); - Oracle automatically creates indexes on primary keys

Complex Queries Joining Database Tables SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE ;

The Results of a JOIN

Using Prefixes when Joining Tables Most current-generation DBMS, such as Oracle, do not require the table name to be used as prefixes, unless the same attribute name occurs in more then 1 table being joined. –If the product number was defined as p_prodnum in the product table and v_prodnum in the vendor table, the table name need not be specified when referenced If the same attribute name occurs in several places, its origin(table) must be specified –Product.prod_num and Vendor.prod_num

Complex Queries SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT, V_AREACODE, V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE AND P_INDATE > ‘08/15/1999’;

Natural Joins Old style: SELECT P_PRODUCT, V_VENDOR SELECT P_PRODUCT, V_VENDOR FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE New style: SELECT P_PRODUCT, V_VENDOR SELECT P_PRODUCT, V_VENDOR FROM PRODUCT NATURAL JOIN VENDOR

Updatable Views Views that can be updated which will update their corresponding tables. Views can be updatable if the primary key of the base table is a column in the view that also has unique values Complete Lab #6 – Functions, Complex Queries, Views, Joining Tables

Sequences Sequences are independent objects in the database They are considered “Auto-numbers” They are created and used as primary keys in tables where the key is a sequential number CREATE SEQUENCE Part_Num_Seq START WITH 100

Sequences on Insert Refer to the sequence (auto-number) object in place of the value needed for the key when inserting data into a table Can be used with NEXTVAL ( sequence is incremented ) or CURRVAL ( current value of sequence is used ) INSERT INTO PARTS VALUES ( PART_ID_SEQ.NEXTVAL, ‘PART_NAME, ect……..)

Procedural SQL Shortcomings of SQL –SQL doesn’t support execution of a stored set of procedures based on some logical condition. –SQL fails to support the looping operations. Solutions –Embedded SQL Embedded SQL can be called from within the procedural programming language –Shared Code Critical code is isolated and shared by all application programs.

Procedural SQL –Procedural SQL allows the use of procedural code and SQL statements that are stored within the database. –The procedural code is executed by the DBMS when it is invoked by the end user. –End users can use procedural SQL (PL/SQL) to create: Triggers Stored procedures PL/SQL functions Procedural SQL

Triggers –A trigger is procedural SQL code that is automatically invoked by the RDBMS upon the occurrence of a data manipulation event. A trigger is always invoked before or after a data row is selected, inserted, or updated. A trigger is always associated with a database table. Each database table may have one or more triggers. A trigger is executed as part of the transaction that triggered it. Procedural SQL

–Role of triggers Triggers can be used to enforce constraints that cannot be enforced at the design and implementation levels. Triggers add functionality by automating critical actions and providing appropriate warnings and suggestions for remedial action. Triggers can be used to update table values, insert records in tables, and call other stored procedures. Triggers add processing power to the RDBMS and to the database system.

Procedural SQL Stored Procedures –A stored procedure is a named collection of procedural and SQL statements. –Stored procedures are stored in the database and invoked by name. –Stored procedures are executed as a unit. –The use of stored procedures reduces network traffic, thus improving performance.

Stored Functions – is a named group of procedural and SQL statements that returns a value. –Invoked from within stored procedures or triggers –Cannot be invoked from within SQL statements PL/SQL Stored Functions