42
Chapter 4 Structured Query Language (SQL)

Database Systems-Lec6

Embed Size (px)

Citation preview

Page 1: Database Systems-Lec6

Chapter 4Structured Query Language (SQL)

Page 2: Database Systems-Lec6

More Complex Queries and SQL Functions

• Ordering a Listing

ORDER BY <attributes>

SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT ORDER BY P_PRICE;

Page 3: Database Systems-Lec6

Selected PRODUCT Table Attributes Ordered by(Ascending) P_PRICE

Figure 6.20

Page 4: Database Systems-Lec6

The Partial Listing of the EMPLOYEE Table

Figure 6.21

Page 5: Database Systems-Lec6

SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL;

Figure 6.22

Page 6: Database Systems-Lec6

Multiple restriction queries

Example of a Query that is based on multiple restrictions:

SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICEFROM PRODUCT WHERE P_INDATE < ’08-20-1999’AND P_PRICE <= 50.00 ORDER BY V_CODE, P_PRICE, DESC;

Page 7: Database Systems-Lec6

• Listing Unique Values

SELECT DISTINCT V_CODE FROM PRODUCT;

More Complex Queries and SQL Functions

Figure 6.24

Page 8: Database Systems-Lec6

FUNCTION OUTPUT

COUNT The number of rows containing the specified attribute.

MIN The minimum attribute value encountered.

MAX The maximum attribute value encountered.

AVG The arithmetic mean (average) for the specified attribute.

SUM The sum of all values for a selected attribute.

Some Basic SQL Numeric Functions

Page 9: Database Systems-Lec6

COUNT Function Output Examples

Figure 6.25

Page 10: Database Systems-Lec6

MAX and MIN Function Output Examples

Figure 6.26

Page 11: Database Systems-Lec6

SUMSELECT SUM(P_ONHAND*P_PRICE) FROM

PRODUCT;

AVGSELECT P_DESCRIPT, P_ONHAND, P_PRICE,

V_CODEFROM PRODUCT WHERE P_PRICE >(SELECT AVG(P_PRICE) FROM PRODUCT)ORDER BY P_PRICE DESC;

More Complex Queries and SQL Functions

Page 12: Database Systems-Lec6

AVG Function Output Examples

Figure 6.28

Page 13: Database Systems-Lec6

GROUP BY SELECT P_SALECODE, MIN(P_PRICE) FROM

PRODUCT_2GROUP BY P_SALECODE;

Figure 6.29

Page 14: Database Systems-Lec6

Improper Use of the GROUP BY Clause

Figure 6.30

Page 15: Database Systems-Lec6

An Application of the HAVING Clause

Figure 6.31

Page 16: Database Systems-Lec6

• Virtual Tables: Creating a View

More Complex Queries and SQL

Functions

Figure 6.32

Page 17: Database Systems-Lec6

• SQL Indexes

CREATE INDEX P_CODEX ON PRODUCT(P_CODE);

CREATE UNIQUE INDEX P_CODEX ON PRODUCT(P_CODE);

More Complex Queries and SQL

Functions

Page 18: Database Systems-Lec6

• Joining Database Tables

SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONEFROM PRODUCT, VENDORWHERE PRODUCT.V_CODE = VENDOR.V_CODE;

More Complex Queries and SQL

Functions

Table 6.11

Page 19: Database Systems-Lec6

The Results of a JOIN

Figure 6.33

Page 20: Database Systems-Lec6

SELECT P_DESCRIPT, P_PRICE, V_NAME, V_CONTACT, V_AREACODE, V_PHONEFROM PRODUCT, VENDORWHERE PRODUCT.V_CODE = VENDOR.V_CODEAND P_INDATE > ’08-15-1999’;

More Complex Queries and SQL

Functions

Figure 6.34

Page 21: Database Systems-Lec6

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.• This approach allows better maintenance and logic

control.

– Procedural SQL

Page 22: Database Systems-Lec6

• 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

Page 23: Database Systems-Lec6

• 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

Page 24: Database Systems-Lec6

Table A Table B

After data is inserted to Table A then do something to Table B

A C+1B D+1

Trigger - Insert Trigger

Page 25: Database Systems-Lec6

– 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

Page 26: Database Systems-Lec6

The Revised PRODUCT Table

Figure 7.30

Page 27: Database Systems-Lec6

• Syntax to create a trigger in ORACLE

CREATE OR REPLACE TRIGGER <trigger_name>[BEFORE/AFTER][DELETE/INSERT/UPDATE OF <column_name] ON <table_name>[FOR EACH ROW]BEGIN

PL/SQL instructions;……………

END;

Trigger

Page 28: Database Systems-Lec6

• Syntax to create a trigger in IBM DB2

create trigger <TriggerName> <After Insert/After Update> on product for each row mode db2sql UPDATE <TableName> set <Conditions>

Trigger-Example

Page 29: Database Systems-Lec6

Creation of the Oracle Trigger for PRODUCT Table

Figure 7.31

Page 30: Database Systems-Lec6

The PRODUCT Table’s P_REORDER Field isUpdated by the Trigger

Figure 7.32

Page 31: Database Systems-Lec6

The P_REORDER Value Mismatch

Figure 7.33

Page 32: Database Systems-Lec6

The Second Version of the PRODUCT_REORDER Trigger

Figure 7.34

Page 33: Database Systems-Lec6

The Third Version of the Product Reorder Trigger

Figure 7.37

Page 34: Database Systems-Lec6

Execution of the Third Trigger Version

Figure 7.38

Page 35: Database Systems-Lec6

• 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.

Procedural SQL

Page 36: Database Systems-Lec6

Table A Table B

You can manipulate the whole set of records (changing, adding, updating, deleting…) using stored procedure

Stored Procedure

AB

C+ 1D+ 1

Stored Procedure

Page 37: Database Systems-Lec6

• Syntax 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 typePL/SQL or SQL statements;

END;

• Syntax to invoke a stored procedure

EXEC store_procedure_name (parameter, parameter, …)

Procedural SQL

Page 38: Database Systems-Lec6

• Stored Procedures

– DECLARE is used to specify the variables used within the procedure.

– Argument specifies the parameters that are passed to the stored procedure.

– IN / OUT indicates whether the parameter is for INPUT or OUTPUT or both.

– Data-type is one of the procedural SQL data types used in the RDBMS.

Procedural SQL

Page 39: Database Systems-Lec6

Creating and Invoking A Simple Stored Procedure

Figure 7.41

Page 40: Database Systems-Lec6

• 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-typeAS BEGIN

PL/SQL statements;RETURN (value); ……

END;

Procedural SQL

Page 41: Database Systems-Lec6

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.

Page 42: Database Systems-Lec6

References

•ROB, P. AND CORONEL, C., 2004, Database Systems. 6th Ed., Thomson Course Technology