Module 3 Lecture
Module 3 Lecture
Ninth Edition
Chapter 3
Structured Query Language
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Learning Objectives
2
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Structured Query Language
3
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL Statement Categories
4
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Wedgewood Pacific Relations
6
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
WP’s Referential Integrity
7
Constraints
Learn basic SQL statements for creating
database structures
• A referential integrity constraint is used to link (or reference) relations.
This means that a foreign key in a relation must also exist in the
relation in which it serves as the primary key. WP’s referential
integrity constraints:
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.1(a) Database Column
8
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.1(c) EMPLOYEE Table
10
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.1(d) PROJECT Table
11
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.1(e) ASSIGNMENT Table
12
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.2(a) DEPARTMENT Table
13
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.2(b) Sample Data for the WP
14
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.2(b) Sample Data for the WP
15
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.2(c) Sample Data for the WP
16
1100 2019 Q3 Marketing Plan Sales and Marketing 135.00 05/10/19 06/15/19
1500 2019 Q4 Marketing Plan Sales and Marketing 135.00 08/10/19 09/15/19
1600 2019 Q4 Portfolio Analysis Finance 140.00 10/05/19 Blank
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.2(d) Sample Data for the WP
17
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.3 The Microsoft Access 2019
18
(DDL)
Learn basic SQL statements for creating
database structures
• The SQL data definition statements include:
– CREATE
§ to create database objects
– ALTER
§ to modify the structure and/or characteristics of database
objects
– DROP
§ to delete database objects
– TRUNCATE
§ to delete table data while keeping the structure
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL CREATE TABLE Statement
20
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.4 SQL CREATE TABLE
21
Statements
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Data Types
22
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.6 Creating Primary Keys
23
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
24
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
25
Inserting Data
• SQL DML is used to query databases and to modify data in the
tables.
• There are three possible data modification operations:
– INSERT (adding data to a relation)
– UPDATE (modifying data in a relation)
– DELETE (deleting data in a relation)
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Inserting Data
29
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
The SQL Query Framework
30
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Reading Specified Columns from a
31
Single Table
Learn basic SQL SELECT statements and
options for processing a single table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries Displaying All
32
Columns
Learn basic SQL SELECT statements and
options for processing a single table
Column Order
Learn basic SQL SELECT statements and
options for processing a single table
MySQL Workbench
Single Table
Learn basic SQL SELECT statements and
options for processing a single table
Keyword
Learn basic SQL SELECT statements and
options for processing a single table
/* *** SQL-Query-CH03-06 *** */
SELECT DISTINCT Department
FROM PROJECT;
Clause
Learn basic SQL SELECT statements and
options for processing a single table
Operators
SQL Comparison Operators
Operator Meaning
= Is equal to
<> Is NOT Equal to
< Is less than
> Is greater than
<= Is less than OR equal to
>= Is greater than OR equal to
IN Is equal to one of a set of values
NOT IN Is NOT equal to any of a set of values
BETWEEN Is within a range of numbers (includes the end points)
NOT BETWEEN Is NOT within a range of numbers (includes the end points)
LIKE Matches a set of characters
NOT LIKE Does NOT match a set of characters
IS NULL Is equal to NULL
IS NOT NULL Is NOT equal to NULL
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.15 SQL Comparison
41
Operators
SQL Comparison Operators
!"#"$%&'
()*+&,"-.)%+"/%&01")"&
,234567286/472	M"&;!N;C&
!"#"$%&'&
()*+&.!!9D/+"/%
01")"&1=>5E0=5F2G&H"%0""/&IJ&./,&KLC&
!"#"$%&'
()*+&,"-.)%+"/%&
01")"&,234567286/472&9/&:;.<<=>86?8@;A&;$=73>625;BC&
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries Using
42
Comparison Operators
Learn basic SQL SELECT statements and
options for processing a single table
Results of a Query
Learn basic SQL SELECT statements and
options for processing a single table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
45
Clause Options
Learn basic SQL SELECT statements and
options for processing a single table
• Three options for the SQL WHERE clauses are:
– compound clauses
– ranges
– wildcards
• An example of the compound clause is below:
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.16 SQL Logical Operators
47
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: SQL WHERE
48
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: SQL WHERE
50
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: SQL Queries
51
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.17 SQL Built-in Aggregate
52
Functions
SQL Built-in Aggregate Functions
Function Meaning
COUNT(*) Count the number of rows in the table
COUNT Count the number of rows in the table where column {Name} IS
({Name}) NOT NULL
SUM Calculate the sum of all values (numeric columns only)
AVG Calculate the average of all values (numeric columns only)
MIN Calculate the minimum value of all values
MAX Calculate the maximum value of all values
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: Using SQL
53
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: Using Multiple
54
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Single Table Queries: Using
55
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
56
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.18 Department Groups in
57
GROUP BY Clause
Learn basic SQL SELECT statements and
options for processing a single table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Multiple Table Queries: SQL For
60
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Multiple Table Queries: Querying
61
with Subqueries
Learn basic SQL SELECT Statements for
processing multiple tables with subqueries
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
62
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Multiple Table Queries: Querying
63
with Joins (1 of 2)
Learn basic SQL SELECT Statements for
processing multiple tables with Joins
with Joins (2 of 2)
Learn basic SQL SELECT Statements for
processing multiple tables with Joins
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.22 Using Primary Key and
66
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Multiple Table Queries: SQL Inner
68
Joins
Learn basic SQL SELECT Statements for
processing multiple tables with Joins
• SQL Inner Join is also referred to as an SQL equijoin.
• An Inner Join only displays data from the rows that match
based on join conditions:
– if a row has a value that does not match the WHERE
clause condition, that row will not be included in the
join result
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.25 Types of SQL JOINS
69
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure 3.25 Types of SQL JOINS
70
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL for Data Manipulation (DML)–
71
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Modifying Data Example 1
72
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Deleting Data
74
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL For Data Definition (DDL) –
75
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
The SQL DROP TABLE Statement
76
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
The SQL ALTER TABLE Statement
77
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Copyright
78
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved