Top Banner
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
28

Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Jan 11, 2016

Download

Documents

Myles Dalton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Chapter 10 Queries and Updating

Part C. SQL

Copyright 2005 Radian Publishing Co.

Page 2: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.2/28

Contents

Chapter 10 Queries and Updating

10.1 Simple Query

• 10.1 A. Select *

• 10.1 B. Selecting Individual Columns

• 10.1 C. Queries with Distinction

• 10.1 D. Ordering the Result

• 10.1 E. Efficiency Issue with Order

• 10.1 F. Specifying Alias for Column

• 10.1 G. Simple Expressions in Query

10.2 The WHERE Clause

• 10.2 A. Using Logical Operators

• 10.2 B. The IN Operator

• 10.2 C. The BETWEEN … AND Operator

• 10.2 D. The LIKE Operator

10.3 Exporting the Query Results

• 10.3 A. Outputting as a New Table

• 10.3 B. Outputting as a a Text File

10.4 Updating and Deleting Records

• 10.4 A. The SQL UPDATE

• 10.4 B. The SQL DELETE

Page 3: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.3/28

Chapter 10 Queries and Updating

In this chapter, you will learn how to query the data contained in tables

using the SQL SELECT command. You will also learn how to update

and delete records using SQL UPDATE and DELETE commands.

Page 4: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.4/28

10.1 Simple Query

A query is a simple request to retrieve information from a database

using criteria.

Page 5: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.5/28

10.1 A. Select * (1/3)

In SELECT *, the asterisk is a wildcard character that tells DBMS to

return all the fields.

Page 6: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.6/28

10.1 A. Select * (2/3)

Page 7: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.7/28

10.1 A. Select * (3/3)

A cursor is the result of a query. It is a read-only table available for

browsing, reporting or input to application programs. It is temporary as

it will be removed when the application is closed.

Page 8: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.8/28

10.1 B. Selecting Individual Columns

The SQL command allows you to display any columns you want.

SELECT FieldNames

FROM TableName;

Page 9: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.9/28

10.1 C. Queries with Distinction

The keyword DISTINCT is used to remove duplicate records in the

query result.

SELECT DISTINCT FieldNames

FROM TableName;

Page 10: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.10/28

10.1 D. Ordering the Result (1/2)

The basic syntax for ordering the result is

SELECT [DISTINCT] FieldNames

FROM TableName

ORDER BY FieldName1, FieldName2 ¡K [ASC|DESC]

Page 11: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.11/28

10.1 D. Ordering the Result (2/2)

Page 12: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.12/28

10.1 E. Efficiency Issue with Order

The efficiency of ordering can be improved by creating indexes.

If a certain ordering is frequently needed, an index should be created

for this ordering.

Page 13: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.13/28

10.1 F. Specifying Alias for Column (1/2)

A column alias can provide more descriptive names for columns.

For MySQL and Visual FoxPro, alias can be used in references in the

ORDER BY and GROUP BY clauses, but this is not allowed in MS

Access.

Page 14: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.14/28

10.1 F. Specifying Alias for Column (2/2)

Page 15: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.15/28

10.1 G. Simple Expressions in Query

A query may consist of expressions instead of field names in the

SELECT clause.

Page 16: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.16/28

10.2 The WHERE Clause

The syntax of a SELECT statement with a WHERE clause is

SELECT [DISTINCT] SelectItems FROM TableName WHERE ConditionsORDER BY OrderItem1 [, OrderItem2 ] ;

Alias is not allowed in the WHERE clause for all DBMS.

An exact match is required for both MySQL and MS Access, but not for Visual FoxPro. (Note: All the DBMS mentioned in the textbook are not case-sensitive. )

Page 17: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.17/28

10.2 A. Using Logical Operators

Logical operators, AND, OR and NOT, are used in the WHERE clause.

Page 18: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.18/28

10.2 B. The IN Operator

The IN operator returns true if the specified data matches any one of

the elements in the given set.

Page 19: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.19/28

10.2 C. The BETWEEN … AND Operator

The BETWEEN … AND operator returns true if the specified data falls

between a starting value and an ending value inclusively.

Page 20: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.20/28

10.2 D. The LIKE Operator (1/2)

The LIKE operator compares the skeletons of two strings.

The wildcard (_) means any single character in the position.

The wildcard (%) means any string in forming the skeleton.

Page 21: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.21/28

10.2 D. The LIKE Operator (2/2)

Page 22: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.22/28

10.3 Exporting the Query Results

Since the result of a query will be lost when the application is closed,

sometimes, you might want to store the results.

Fig.10.2 Variations between DBMS in exporting query results

Page 23: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.23/28

10.3 A. Outputting as a New Table

In MS Access, the syntax to output the query as a table is:

SELECT [DISTINCT] SelectItemsINTO OutTable FROM TableName ...

In MySQL, the syntax to output the query as a table is:

CREATE TABLE OutTableSELECT SelectItemsFROM TableName ...

In Visual FoxPro, the syntax to output the query as a table is:

SELECT [DISTINCT] SelectItemsINTO TABLE OutTable FROM TableName ...

Page 24: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.24/28

10.3 B. Outputting as a a Text File

In MySQL, the syntax to output the query result to a text file is:

SELECT SelectItems

INTO OUTFILE 'FileName'

FROM TableName ...

In Visual FoxPro, the syntax to output the query result to a text file is:

SELECT SelectItems

TO FILE 'FileName'

FROM TableName ...

Page 25: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.25/28

10.4 A. The SQL UPDATE (1/2)

The basic syntax for updating a set of records is

UPDATE TableName

SET FieldName1 = Expression1 [, FieldName2 = Expression2 ]

WHERE Conditions;

Page 26: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.26/28

10.4 A. The SQL UPDATE (2/2)

Page 27: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.27/28

10.4 B. The SQL DELETE (1/2)

The basic syntax for deleting a set of records is

DELETE FROM TableName

WHERE Conditions;

If the WHERE clause is missed, all records will be deleted.

Page 28: Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.

Copyright 2005 Radian Publishing Co.28/28

10.4 B. The SQL DELETE (2/2)

Delete those records with quantity less than 5

Delete all records