Download presentation
Presentation is loading. Please wait.
1
5 Chapter 5 Structured Query Language (SQL2) Revision
2
5 2 In this lecture, you will learn: About more advanced queries SQL features such as updatable views, stored procedures, and triggers
3
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?
4
5 4 Queries Creating partial listings of table contents SELECT FROM WHERE ; Table 5.4 Mathematical Operators
5
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’;
6
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;
7
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 = 21344 OR V_CODE = 24288;
8
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’;
9
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
10
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;
11
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);
12
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;
13
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
14
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
15
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;
16
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
17
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)
18
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
19
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);
20
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.
21
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.
22
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)
23
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
24
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;
25
5 25 Example of Trigger Product table
26
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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.