Download presentation
Presentation is loading. Please wait.
Published byMaximilian Taylor Modified over 9 years ago
1
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel
2
3 3 2 More Complex Queries and SQL Functions 4Ordering a Listing ORDER BY SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT ORDER BY P_PRICE;
3
3 3 3 Figure 3.18 Selected PRODUCT Table Attributes Ordered by (Ascending) P_PRICE
4
3 3 4 The Partial Listing of the EMPLOYEE Table Figure 3.19
5
3 3 5 SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL; Figure 3.20
6
3 3 6 SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT WHERE P_INDATE < ‘08/20/1999’ AND P_PRICE <= 50.00 ORDER BY V_CODE, P_PRICE, DESC; Figure 3.21 A Query Based on Multiple Restrictions
7
3 3 7 4Listing Unique Values SELECT DISTINCT V_CODE FROM PRODUCT; More Complex Queries and SQL Functions Figure 3.22 A Listing of Distinct V_CODE Values in the PRODUCT Table
8
3 3 8 Some Basic SQL Numeric Functions Table 3.6
9
3 3 9 Querying a Query: Nested Process Figure 3.23
10
3 3 10 COUNT Function Output Examples Figure 3.24
11
3 3 11 MAX and MIN Function Output Examples Figure 3.25
12
3 3 12 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
13
3 3 13 AVG Function Output Examples Figure 3.26
14
3 3 14 4Grouping Data GROUP BY SELECT P_SALECODE, MIN(P_PRICE) FROM PRODUCT_2 GROUP BY P_SALECODE;
15
3 3 15 Improper Use of the GROUP BY Clause Figure 3.28
16
3 3 16 An Application of the HAVING Clause Figure 3.29
17
3 3 17 4Virtual Tables: Creating a View More Complex Queries and SQL Functions Figure 3.30
18
3 3 18 4SQL Indexes CREATE INDEX P_CODEX ON PRODUCT(P_CODE); CREATE UNIQUE INDEX P_CODEX ON PRODUCT(P_CODE); More Complex Queries and SQL Functions
19
3 3 19 4Joining 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; More Complex Queries and SQL Functions Table 3.7 Creating Links Through Foreign Keys
20
3 3 20 The Results of a JOIN Figure 3.31
21
3 3 21 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’; More Complex Queries and SQL Functions Figure 3.32 An Ordered and Limited Listing After a JOIN
22
3 3 22 Procedural SQL 4Shortcomings of SQL u SQL doesn’t support execution of a stored set of procedures based on some logical condition. u SQL fails to support the looping operations. 4Solutions u Embedded SQL l To remedy the above shortcomings, SQL statements can be inserted within the procedural programming language l The embedded SQL approach involves the duplication of application code in many programs. u Shared Code l Critical code is isolated and shared by all application programs. l This approach allows better maintenance and logic control. u Procedural SQL
23
3 3 23 Procedural SQL 4Procedural SQL u Procedural SQL allows the use of procedural code and SQL statements that are stored within the database. u The procedural code is executed by the DBMS when it is invoked by the end user. u End users can use procedural SQL (PL/SQL) to create: l Triggers l Stored procedures l PL/SQL functions
24
3 3 24 Procedural SQL 4Triggers u A trigger is procedural SQL code that is automatically invoked by the RDBMS upon the occurrence of a data manipulation event. l A trigger is always invoked before or after a data row is selected, inserted, or updated. l A trigger is always associated with a database table. l Each database table may have one or more triggers. l A trigger is executed as part of the transaction that triggered it.
25
3 3 25 Procedural SQL u Role of triggers l Triggers can be used to enforce constraints that cannot be enforced at the design and implementation levels. l Triggers add functionality by automating critical actions and providing appropriate warnings and suggestions for remedial action. l Triggers can be used to update table values, insert records in tables, and call other stored procedures. l Triggers add processing power to the RDBMS and to the database system.
26
3 3 26 The Revised PRODUCT Table Figure 3.33
27
3 3 27 The PRODUCT List Output in the Oracle RDBMS Figure 3.34
28
3 3 28 Procedural SQL 4Syntax to create a trigger in ORACLE CREATE OR REPLACE TRIGGER [BEFORE/AFTER][DELETE/INSERT/UPDATE OF [FOR EACH ROW] BEGIN PL/SQL instructions; …………… END;
29
3 3 29 Creation of the Oracle Trigger for the PRODUCT Table Figure 3.35
30
3 3 30 The PRODUCT Table’s P_REORDER Field is Updated by the Trigger Figure 3.36
31
3 3 31 The P_REORDER Value Mismatch Figure 3.37
32
3 3 32 The Second Version of the PRODUCT_REORDER Trigger Figure 3.38
33
3 3 33 Figure 3.39
34
3 3 34 The P_REORDER Flag Has Not Been Properly Set After Increasing the P_ONHAND Value Figure 3.40
35
3 3 35 The Third Version of the Product Reorder Trigger Figure 3.41
36
3 3 36 Execution of the Third Trigger Version Figure 3.42
37
3 3 37 Procedural SQL 4Stored Procedures u A stored procedure is a named collection of procedural and SQL statements. u Stored procedures are stored in the database and invoked by name. u Stored procedures are executed as a unit. u The use of stored procedures reduces network traffic, thus improving performance.
38
3 3 38 Procedural SQL 4Syntax to create a stored procedure CREATE OR REPLACE PROCEDURE procedure_name (argument IN/OUT data-type, etc) IS/AS BEGIN DECLARE variable name and data type PL/SQL or SQL statements; END; 4Syntax to invoke a stored procedure EXEC store_procedure_name (parameter, parameter, …)
39
3 3 39 Procedural SQL 4Stored Procedures u DECLARE is used to specify the variables used within the procedure. u Argument specifies the parameters that are passed to the stored procedure. u IN / OUT indicates whether the parameter is for INPUT or OUTPUT or both. u Data-type is one of the procedural SQL data types used in the RDBMS.
40
3 3 40 Creating and Invoking A Simple Stored Procedure Figure 3.43
41
3 3 41 The PROD_SALE Stored Procedure Figure 3.44
42
3 3 42 Creation of the PROD_SALE Stored Procedure Figure 3.45
43
3 3 43 Executing the PROD_SALE Stored Procedure Figure 3.46
44
3 3 44 Procedural SQL 4PL/SQL Stored Functions u A stored function is a named group of procedural and SQL statements that returns a value. u Syntax to create a function: CREATE FUNCTION function_name (argument IN data-type, etc) RETURN data-type AS BEGIN PL/SQL statements; RETURN (value); …… END;
45
3 3 45 The Y2K Problem 4Problem u Many database vendors use 2-digit date formats as the default. How the 2-digit year is viewed depends on how the DBMS vendor treats dates. 4Solutions u Design and implement database applications that always enter and display dates with four-digit years and use a Julian date field format. u Julian date stores date field values as the number of days since a predetermined date.
46
3 3 46 The Default P_INDICATE Two-Digit Year Format Figure 3.47
47
3 3 47 Formatting the Date Fields to Four-Digit Years Figure 3.48
48
3 3 48 Using the Input Mask to Force Four-Digit Year Entries in MS Access Figure 3.49
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.