Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

47
Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition

Transcript of Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Page 1: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Chapter 3The Relational Model 2: SQL

Concepts of Database Management

Seventh Edition

Page 2: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Objectives

2

Introduce Structured Query Language (SQL)Use simple and compound conditions in SQLUse computed fields in SQLUse built-in SQL functionsUse subqueries in SQLGroup records in SQLJoin tables using SQLPerform union operations in SQLUse SQL to update database dataUse an SQL query to create a table in a

database

Page 3: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Introduction

3

SQL (Structured Query Language)Allows users to query a relational databaseMust enter commands to obtain the desired

resultsStandard language for relational database

manipulation

Page 4: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Getting Started with SQL

4

If you are completing the work in this chapter using Microsoft Office Access 2007, Microsoft Office Access 2010, or MySQL version 4.1 or higher, the following sections contain specific information about your DBMS

Page 5: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Getting Started with Microsoft Office Access 2007 and 2010

5

If you are using the Access 2007 or 2010 version of the Premiere Products database provided with the Data Files for this text:Tables in the database have already been

createdYou will not need to execute the CREATE

TABLE commands to create the tables or the INSERT commands to add records to the tables

Page 6: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Getting Started with Microsoft Office Access 2007 and 2010 (continued)

6

To execute SQL commands shown in the figures in Access 2007 or Access 2010:Open the Premiere Products databaseClick the Create tab on the RibbonClick the Query Design button in the Other groupClick the Close button in the Show Table dialog boxClick the View button arrow in the Results group

on the Query Design Tools tab, then click SQL ViewThe Query1 tab displays the query in SQL view,

ready for you to type your SQL commands

Page 7: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Getting Started with MySQL

7

MySQL-Premiere script provided with the Data Files for this text will:Activate the databaseCreate the tablesInsert the records

To run a script in MySQL:Type the SOURCE command followed by the name

of the filePress the Enter key

Before typing commands in MySQL, you must activate the database by typing the USE command followed by the name of the database

The most recent command entered in MySQL is stored in a special area of memory called the statement history

Page 8: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Table Creation

8

SQL CREATE TABLE commandCreates a table by describing its layout

Typical restrictions placed on table and column names by DBMSNames cannot exceed 18 charactersNames must start with a letterNames can contain only letters, numbers,

and underscores (_)Names cannot contain spaces

See Example 1 on page 74.

Page 9: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Table Creation (continued)

9

INTEGERNumber without a decimal point

SMALLINTUses less space than INTEGER

DECIMAL(p,q)P number of digits; q number of decimal

placesCHAR(n)

Character string n places longDATE

Dates in DD-MON-YYYY or MM/DD/YYYY form

Page 10: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Simple Retrieval

10

SELECT-FROM-WHERE: SQL retrieval command SELECT clause: lists fields to displayFROM clause: lists table or tables that

contain data to display in query resultsWHERE clause (optional): lists any

conditions to be applied to the data to retrieveSimple condition: field name, a

comparison operator, and either another field name or a value

See Examples 2 & 3 on page 75-78.

Page 11: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Simple Retrieval (continued)

11

FIGURE 3-6: SQL query with WHERE condition (Example 4)

Page 12: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Simple Retrieval (continued)

12

FIGURE 3-7: Query results (Example 4)

Page 13: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Simple Retrieval (continued)

13

FIGURE 3-8: Comparison operators used in SQL commands

Page 14: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Compound Conditions (P82)

14

Compound conditionConnecting two or more simple conditions using

one or both of the following operators: AND and ORPreceding a single condition with the NOT operator

Connecting simple conditions using AND operatorAll of the simple conditions must be true for the

compound condition to be trueConnecting simple conditions using OR operator

Any of the simple conditions must be true for the compound condition to be true

Page 15: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Compound Conditions (continued)

15

FIGURE 3-16: Query results

FIGURE 3-15: Compound condition that uses the AND operator

Page 16: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Compound Conditions (continued)

16FIGURE 3-18: Query results

FIGURE 3-17: Compound condition that uses the OR operator

Page 17: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Compound Conditions (continued)

17

Preceding a condition by NOT operatorReverses the truth or falsity of the original

conditionBETWEEN operator

Value must be between the listed numbers

Page 18: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Computed Fields (p86)

18

Computed field or calculated fieldField whose values you derive from existing

fieldsCan involve:

Addition (+)Subtraction (-)Multiplication (*)Division (/)

Page 19: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Computed Fields (continued)

19

FIGURE 3-26: Query results

FIGURE 3-25: SQL query with a computed field and condition

Page 20: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Using Special Operators (LIKE and IN, p88)

20

Wildcards in Access SQLAsterisk (*): collection of charactersQuestion mark (?): any individual character

Wildcards in MySQLPercent sign (%): any collection of charactersUnderscore (_): any individual character

To use a wildcard, include the LIKE operator in the WHERE clause

IN operator provides a concise way of phrasing certain conditions

Page 21: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Using Special Operators (LIKE and IN) (continued)

21

FIGURE 3-28: Query results

FIGURE 3-27: SQL query with a LIKE operator

Page 22: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Using Special Operators (LIKE and IN) (continued)

22

FIGURE 3-29: Query results

FIGURE 3-28: SQL query with an IN operator

Page 23: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Sorting (p90)

23

Sort data using the ORDER BY clauseSort key: field on which to sort dataWhen sorting data on two fields:

Major sort key (or primary sort key): more important sort key

Minor sort key (or secondary sort key): less important sort key

Page 24: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Sorting (continued)

24FIGURE 3-34: Query results

FIGURE 3-33: SQL query to sort data on multiple fields

Page 25: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Built-in Functions (p92)

25

Built-in functions (aggregate functions) in SQLCOUNT: calculates number of entriesSUM or AVG: calculates sum or average of all

entries in a given columnMAX or MIN: calculates largest or smallest

values respectively

Page 26: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Built-in Functions (continued)

26

FIGURE 3-36: Query results

FIGURE 3-35: SQL query to count records

Page 27: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Subqueries (p95)

27

Subquery: inner querySubquery is evaluated firstOuter query is evaluated after the

subquery

Page 28: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Subqueries (continued)

28

FIGURE 3-42: Query results

FIGURE 3-41: SQL query with a subquery

Page 29: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Grouping (p96)

29

Create groups of records that share a common characteristic

GROUP BY clause indicates grouping in SQL

HAVING clause is to groups what the WHERE clause is to rows

Page 30: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Grouping (continued)

30

FIGURE 3-46: Query results

FIGURE 3-45: SQL query to restrict the groups that are included

Page 31: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Joining Tables (p99)

31

Queries can locate data from more than one table

Enter appropriate conditions in the WHERE clause

To join tables, construct the SQL command as:1. SELECT clause: list all fields you want to display2. FROM clause: list all tables involved in the query3. WHERE clause: give the condition that will restrict

the data to be retrieved to only those rows from the two tables that match

Page 32: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Joining Tables (continued)

32

FIGURE 3-49: SQL query to join tables

Page 33: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Joining Tables (continued)

33

FIGURE 3-50: Query results

Page 34: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Union (p102)

34

Union of two tables is a table containing all rows in the first table, the second table, or both tables

Two tables involved must be union compatibleSame number of fieldsCorresponding fields must have same data

types

Page 35: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Union (continued)

35FIGURE 3-56: Query results

FIGURE 3-55: SQL query to perform a union

Page 36: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Updating Tables (p103)

36

UPDATE command makes changes to existing data

INSERT command adds new data to a tableDELETE command deletes data from the

database

Page 37: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Updating Tables (continued)

37

FIGURE 3-58: SQL query to insert a row

FIGURE 3-57: SQL query to update data

Page 38: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

38

Updating Tables (continued)

38

FIGURE 3-59: SQL query to delete rows

Page 39: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Creating a Table from a Query (p105)

39

INTO clauseSaves the results of a query as a tableSpecified before FROM and WHERE clauses

MySQL Create the new table using a CREATE TABLE

commandUse an INSERT command to insert the

appropriate data into the new table

Page 40: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

40

Creating a Table from a Query (continued)

40

FIGURE 3-60a: Query to create a new table (Access)

Page 41: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

41

Creating a Table from a Query (continued)

41

FIGURE 3-60b: Query to create a new table (for Oracle and MySQL)

Page 42: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Summary of SQL Commands (p107)

42

Generic versions of SQL commands for every example presented in this chapter

In most cases, commands in Access are identical to the generic versions

For those commands that differ, both the generic version and the Access version are included

Page 43: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Summary

43

Structured Query Language (SQL) is a language that is used to manipulate relational databases

Basic form of an SQL query: SELECT-FROM-WHERE

Use CREATE TABLE command to describe table layout to the DBMS, which creates the table

In SQL retrieval commands, fields are listed after SELECT, tables are listed after FROM, and conditions are listed after WHERE

In conditions, character values must be enclosed in single quotation marks

Page 44: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Summary (continued)

44

Compound conditions are formed by combining simple conditions using either or both of the following operators: AND and OR

Sorting is accomplished using ORDER BY clause

When the data is sorted in more than one field, can have a major and minor sort key

Grouping: use the GROUP BY clauseHAVING clause: restricts the rows to be

displayed

Page 45: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Summary (continued)

45

Joining tables: use a condition that relates matching rows in the tables to be joined

Built-in (aggregate) functions: COUNT, SUM, AVG, MAX, and MIN

One SQL query can be placed inside another; the subquery is evaluated first

UNION operator: unifies the results of two queries

Page 46: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Summary (continued)

46

Calculated fields: include the calculation, the word AS, the name of the calculated field

INSERT command adds a new row to a table

UPDATE command changes existing dataDELETE command deletes recordsINTO clause is used in a SELECT command

to create a table containing the results of the query

Page 47: Chapter 3 The Relational Model 2: SQL Concepts of Database Management Seventh Edition.

Chapter 3 Homework

47

Due: 2/19/2013Pages: 115 (7e) or 113 (6e)Do all ODD questions in the Premiere

Products Exercises.