5 Chapter 5 Structured Query Language (SQL2) Revision.

Slides:



Advertisements
Similar presentations
7 7 SQL Data Definition 4Spread throughout chapter 7.
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL) Hachim Haddouti.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
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.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
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.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Chapter 7 Introduction to Structured Query Language (SQL)
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 5 Advanced Querying
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
Database Systems: Design, Implementation, and Management Tenth Edition
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.
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
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.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Chapter 6 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management Peter Rob & Carlos Coronel.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – M. E. Kabay, PhD, CISSP-ISSMP.
1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
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.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
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.
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Chapter 1 File Systems and Databases Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Relational Database Design
Insert, Update and the rest…
Advanced SQL Advanced SQL Week 8 Comparison Operators
M. E. Kabay, PhD, CISSP-ISSMP V:
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

5 Chapter 5 Structured Query Language (SQL2) Revision

5 2 In this lecture, you will learn: About more advanced queries SQL features such as updatable views, stored procedures, and triggers

5 3 Four Question to Create A Query What output do you want to see? What do you already know (or what constraints are given)? What tables are involved? How are the tables joined together?

5 4 Queries Creating partial listings of table contents SELECT FROM WHERE ; Table 5.4 Mathematical Operators

5 5 Examples Mathematical operators on numeric/ integer Mathematical operators on character attributes Mathematical operators on dates SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE <> 21344; SELECT P_CODE,P_DESCRIPT,P_ONHAND,P_MIN,P_PRICE FROM PRODUCT WHERE P_CODE < ‘1558-QWI’; SELECT P_DESCRIPT,P_ONHAND,P_MIN,P_PRICE,PINDATE FROM PRODUCT WHERE P_INDATE >= ‘01/20/2002’;

5 6 Computed Columns New columns can be created through valid computations or formulas –Formulas may contain mathematical operators –May contain attributes of any tables specified in FROM clause Alias is alternate name given to table or column in SQL statement SELECT P_DESCRIPT,P_ONHAND,P_PRICE,P_ONHAND*P_PRICE AS TOTVALUE FROM PRODUCT;

5 7 Operators Logical: AND, OR, NOT Rules of precedence –Conditions within parenthesis executed first –Boolean algebra Special –BETWEEN - defines limits –IS NULL - checks for nulls –LIKE - checks for similar string –IN - checks for value in a set –EXISTS - opposite of IS NULL SELECT * FROM PRODUCT WHERE V_CODE = OR V_CODE = 24288;

5 8 Advanced Data Management Commands ALTER - changes table structure ADD - adds column MODIFY - changes column characteristics Entering data into new column ALTER TABLE ADD ( ); ALTER TABLE MODIFY ( ); UPDATE PRODUCT SET P_SALECODE = ‘2’ WHERE P_CODE = ‘1546-QQ2’;

5 9 Advanced Data Management Commands (con’t.) Dropping a column Arithmetic operators and rules of precedence ALTER TABLE VENDOR DROP COLUMN V_ORDER; Table 5.5

5 10 Advanced Data Management Commands (con’t.) Deleting a table from database Primary and foreign key designation DROP TABLE PART; ALTER TABLE LINE ADD PRIMARY KEY (INV_NUMBER, LINE_NUMBER) ADD FOREIGN KEY (INV_NUMBER) REFERENCES INVOICE ADD FOREIGN KEY (PROD_CODE) REFERENCES PRODUCT;

5 11 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 = MAX(P_PRICE);

5 12 Example Aggregate Function Operations (con’t.) SUM AVG SELECT SUM(P_ONHAND * P_PRICE) FROM PRODUCT; 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;

5 13 More Complex Queries and SQL Functions Ordering a listing Results ascending by default –Descending order uses DESC Cascading order sequence ORDER BY ORDER BY DESC ORDER BY

5 14 More Complex Queries and SQL Functions (con’t.) Listing unique values –DISTINCT clause produces list of different values Aggregate functions –Mathematical summaries SELECT DISTINCT V_CODE FROM PRODUCT; Table 5.6

5 15 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_2 GROUP BY P_SALECODE; SELECT V_CODE,COUNT(DISTINCT(P_CODE)),AVG(P_PRICE) FROM PRODUCT_2 GROUP BY V_CODE HAVING AVG(P_PRICE) < 10;

5 16 More Complex Queries and SQL Functions (con’t.) Joining database tables –Data are retrieved from more than one table –Recursive queries joins a table to itself –Outer joins can be used when ‘null’ values need to be included in query result 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; SELECT A.EMP_NUM,A.EMP_LNAME,A.EMP_MGR, B.EMP_LNAME FROM EMP A, EMP B WHERE A.EMP_MGR=B.EMP_NUM ORDER BY A.EMP_MGR

5 17 SQL JOIN (Two Table Joining) FROM table1 INNER JOIN table2 ON table1.column = table2.column FROM table1, table2 JOIN table1.column = table2.column SQL 92 syntax (Access and SQL Server) Informal syntax FROM table1, table2 WHERE table1.column = table2.column SQL 89 syntax (Oracle & DB2)

5 18 Syntax for Three Tables Join FROM Table1 INNER JOIN (Table2 INNER JOIN Table3 ON Table2.ColA = Table3.ColA) ON Table1.ColB = Table2.ColB SQL ‘92 syntax to join three tables FROM table1, table2, Table3 WHERE table1.column = table2.column AND table2.column = table3.column SQL ‘89 syntax to join three tables

5 19 More Complex Queries and SQL Functions (con’t.) Virtual tables: creating a view –CREATE VIEW command –Creates logical table existing only in virtual memory –SQL indexes CREATE VIEW PRODUCT_3 AS SELECT P_DESCRIPT, P_ONHAND, P_PRICE FROM PRODUCT WHERE P_PRICE > 50.00; CREATE INDEX P_CODEX ON PRODUCT(P_CODE);

5 20 Example of SQL Join Q: List the Last Name and Phone of anyone who bought a registered White cat between 6/1/2001 and 12/31/2001.

5 21 Example of SQL Join (con’t) Identify the tables involved. –Look at the columns you want to see. LastName, Phone: Customer –Look at the columns used in the constraints. Registered, Color, Category: Animal Sale Date: Sale –Find connector tables. To connect from Customer to Animal, four tables involved Select the desired columns and test the query. Enter the constraints. Set Order By columns. Add Group By columns. Add summary computations to the SELECT statement.

5 22 SELECT LastName, Phone FROM Customer, Animal, SaleAnimal, Sale WHERE Customer.CustomerID = Sale.CustomerID AND Sale.SaleID = SaleAnimal.SaleID AND SaleAnimal.AnimalID = Animal.AnimalID AND Category="Cat“ AND Registered Is Not Null AND Color Like "*White*" AND SaleDate Between ‘6/1/2001’ And ‘12/31/2001’); Example of SQL Join (con’t)

5 23 Triggers Procedural SQL code invoked before or after data row is selected, inserted, or updated Associated within the table Table may have multiple triggers Invoked before or after a data row is selected, inserted, or updated Can update values, insert records, and call procedures Add processing power

5 24 Triggers (con’t.) DB2 example CREATE TRIGGER [BEFORE/AFTER] [DELETE/INSERT/UPDATE OF <column_name] ON [FOR EACH ROW MODE DB2SQL] BEGIN PL/SQL instructions; …………… END;

5 25 Example of Trigger Product table

5 26 Example of Trigger (con’t) TRG_PRODUCT_REORDER create trigger TRG_PRODUCT REORDER after update of p_onhand, p_min on product for each row mode db2sql update product set reorder =1 where (p_onhand <= p_min);