Professional Documents
Culture Documents
Structured Query
Language (SQL)
More Complex Queries
and SQL Functions
• Ordering a Listing
ORDER BY <attributes>
Figure 6.20
The Partial Listing of the EMPLOYEE Table
Figure 6.21
SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL,
EMP_AREACODE, EMP_PHONE FROM EMPLOYEE
ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL;
Figure 6.22
Multiple restriction queries
Figure 6.24
Some Basic SQL Numeric Functions
FUNCTI ON OUTPUT
Figure 6.25
MAX and MIN Function Output Examples
Figure 6.26
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;
AVG Function Output Examples
Figure 6.28
GROUP BY
SELECT P_SALECODE, MIN(P_PRICE) FROM
PRODUCT_2
GROUP BY P_SALECODE;
Figure 6.29
Improper Use of the GROUP BY Clause
Figure 6.30
An Application of the HAVING Clause
Figure 6.31
More Complex
Queries and SQL
Functions
• Virtual Tables: Creating a View
Figure 6.32
More Complex
Queries and SQL
Functions
• SQL Indexes
Figure 6.33
More Complex
Queries and SQL
Functions
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’;
Figure 6.34
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
• To remedy the above shortcomings, SQL
statements can be inserted within the
procedural programming language
• The embedded SQL approach involves the
duplication of application code in many
programs.
– Shared Code
• Critical code is isolated and shared by all
application programs.
Procedural
SQL
• 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.
Trigger - Insert Trigger
A C+1
B D+1
Table A Table B
After data is inserted to Table A then
do something to Table B
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.
The Revised PRODUCT Table
Figure 7.30
Trigger
• Syntax to create a trigger in ORACLE
Figure 7.31
The PRODUCT Table’s P_REORDER Field is
Updated by the Trigger
Figure 7.32
The P_REORDER Value Mismatch
Figure 7.33
The Second Version of the
PRODUCT_REORDER Trigger
Figure 7.34
The Third Version of
the
Product Reorder
Trigger
Figure 7.37
Execution of the Third Trigger Version
Figure 7.38
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
Stored Procedure
A Stored C+ 1
B Procedure D+ 1
Table A Table B
Figure 7.41
Procedural
SQL
• PL/SQL Stored Functions
– A stored function is a named group of
procedural and SQL statements that
returns a value.
– 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;
The Y2K Problem
• Problem
– 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.
• Solutions
– Design and implement database
applications that always enter and
display dates with four-digit years and
use a Julian date field format.
– Julian date stores date field values as the
number of days since a predetermined
date.
References