Module 8 Lecture
Module 8 Lecture
Ninth Edition
Extension B
Advanced SQL
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Learning Objectives (1 of 2)
2
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Learning Objectives (2 of 2)
3
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Background
4
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.1 WP Database Column
5
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.2 WP Database Column
6
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.3 WP Database Data for the
7
PRODUCTION_ITEM Table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.4 WP Database Data for the
8
CATALOG_SKU_2017 Table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.5 WP Database Data for the
9
CATALOG_SKU_2018 Table
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.6 SQL CREATE TABLE
10
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
11
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.8 The Revised Database
12
Database Tables (1 of 3)
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.9 Data in the New WP
14
Database Tables (2 of 3)
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.9 Data in the New WP
15
Database Tables (3 of 3)
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using the SQL ALTER TABLE
16
Statement
To understand reasons for using the SQL ALTER statement
• The SQL ALTER TABLE statement is used to modify the
structure of a table once it has been created in a
database.
– Can be used to add, modify, and drop columns and
constraints
– Very useful to modify the AUTO_INCREMENT starting
value in MySQL 8.0
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Modifying the MySQL
17
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Modifying the MySQL
18
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
19
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
20
2. Update the current rows in the PRODUCTION_ITEM table with the FAA
approval dates for the existing drone models. Examples:
UPDATE PRODUCTION_ITEM SET ApprovalDate = '2016-08-31' WHERE SKU = 170102001;
3. Modify the ApprovalDate column to set the NOT NULL column constraint.
ALTER TABLE PRODUCTION_ITEM MODIFY COLUMN ApprovalDate DATE NOT NULL;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.10
21
PRODUCTION_ITEM.ApprovalDate
set to NOT NULL
Existing Table
To use the SQL ALTER statement
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.11 The PRODUCTION_ITEM
23
CheckProductionDate Constraint
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Adding a Referential Integrity
25
ON UPDATE NO ACTION
ON DELETE NO ACTION;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.12 The Database Design for
26
Existing Table
To use the SQL ALTER statement
• The SQL ALTER TABLE statement can be used to
remove a column from a table. Note that the SQL
COLUMN keyword is used in the DROP COLUMN clause.
The basic syntax is:
/* *** EXAMPLE CODE – DO NOT RUN *** */
/* *** SQL-ALTER-TABLE-ExtB-10 *** */
ALTER TABLE {TableName}
DROP COLUMN {ColumnName};
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Removing a Table Constraint from an
28
Existing Table
To use the SQL ALTER statement
• The SQL ALTER TABLE statement can be used to
remove a table constraint. Note that the SQL
CONSTRAINT keyword is used in the DROP
CONSTRAINT clause. The basic syntax is:
/* *** EXAMPLE CODE – DO NOT RUN *** */
/* *** SQL-ALTER-TABLE-ExtB-11 *** */
ALTER TABLE {TableName}
DROP CONSTRAINT {ConstraintName};
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using the SQL MERGE Statement
29
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.13 SQL CREATE TABLE
30
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
31
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using the SQL MERGE Statement
33
ON PROI.SKU = PROID.SKU
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using Outer Join Queries
34
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
35
• The problem with the previous query is no data about the new project with
ProjectID 1700! But now consider the following SQL query statement—which
simplifies SQL Query-CH03-53 by retrieving employee numbers instead of
employee names and thus eliminates the need to include the EMPLOYEE
table in the query—and notice the use of the JOIN ON syntax—the SQL LEFT
keyword is simply added to the SQL query to include data from the left side
(PROJECT), even though it has no match!
/* *** SQL-Query-ExtB-02 *** */
SELECT ProjectName, EmployeeNumber, HoursWorked
FROM PROJECT AS P LEFT JOIN ASSIGNMENT AS A
ON P.ProjectID = A.ProjectID
ORDER BY P.ProjectID;
• The results of this query can be seen in Figure B-16. Notice the last row of
this data!
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
37
• Right outer joins operate similarly, except that the SQL RIGHT keyword is
used, and rows in the table on the right-hand side of the FROM clause are
included. For example, you could join all three tables together with the
following modification of SQL-QueryCH03-53, which includes a right outer join
which will show not only the employees assigned to projects but also those
employees who are not assigned.
/* *** SQL-Query-ExtB-03 *** */
SELECT ProjectName, FirstName, LastName, HoursWorked
FROM (PROJECT AS P JOIN ASSIGNMENT AS A
ON P.ProjectID = A.ProjectID)
RIGHT JOIN EMPLOYEE AS E
ON A.EmployeeNumber = E.EmployeeNumber
ORDER BY P.ProjectID, A.EmployeeNumber;
• The result of this join, which now shows not only the employees assigned to
projects but also those employees who are not assigned to any projects, is
shown in Figure B-17.
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
39
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using Correlated Subqueries
41
• Example: take the value of LastName from the first row in EMPLOYEE and
examine all of the other rows in the table. If we find a row that has the same
last name as the one in the first row, we know there are duplicates, so we
print the EmployeeNumber, LastName, and FirstName of the first row.
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
43
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
44
FROM EMPLOYEE AS E1
WHERE EXISTS
(SELECT E2.LastName
FROM EMPLOYEE AS E2
WHERE E1.LastName = E2.LastName
AND E1.EmployeeNumber <> E2.EmployeeNumber);
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL Queries on Recursive
45
Relationships
To use SQL queries on recursive relationships
• Recursive relationships use the same technique we just used in correlated
subqueries. This gives two virtual tables with the queries using them written
just the same as we would any other query.
• The following query will show which employees are supervisors (represented
by an “s”) and which employees (represented by an “e”) they supervise.
/* *** SQL-Query-ExtB-09 *** */
SELECT S.FirstName AS SupervisorFirstName,
S.LastName AS SupervisorLastName,
E.FirstName AS EmployeeFirstName,
E.LastName AS EmployeeLastName
FROM EMPLOYEE S JOIN EMPLOYEE E
ON S.EmployeeNumber = E.Supervisor
ORDER BY S.EmployeeNumber;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using SQL Set Operators
46
• Mathematicians use the term set theory to describe mathematical operations on sets,
where a set is defined as a group of distinct items.
• Venn diagrams are the standard method of visualizing sets and their relationships.
Some definitions of sets are:
– A set is represented by a labeled circle, as shown in Figure B-19(a).
– A subset is a portion of a set that is contained entirely within the set, as shown
in Figure B-19(b).
– The union of two sets is shown in Figure B-19(c), and represents the two sets
together to get a set that contains all values in both sets. This is equivalent to an
OR logical operation (A OR B).
– The intersection of two sets is shown in Figure B-19(d), and represents the
area common to both sets. This is equivalent to an AND logical operation (A AND
B).
– The complement of set B in set A is shown in Figure B-19(e), and represents
everything in set A that is not in set B. This is equivalent to a logical operation
using A AND (NOT B). It is equivalent to the set difference (A – B).
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.19 Venn Diagrams
47
(a) A Set (b) A Subset (c) The Union of Two Sets (d) The Intersection of Two
Sets (e) The Complement of Two Sets
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.20 SQL Set Operators
48
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using SQL Set Operators
49
FROM CATALOG_SKU_2018;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
50
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
51
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.21 Creating a View in the
52
MySQL Workbench
SQL Developer
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.27 SQL Views as the Basis
59
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
60
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
62
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
63
• For example, the following view allows the user to compare the maximum
hours allocated for each WP project to the total hours worked to date on the
project:
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
64
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
65
• Such views allow developers who do not know how to write complicated SQL
statements to enjoy the benefits of such statements.
• This use of views also ensures consistency. Suppose that WP users need to
know which employees are assigned to which projects and how many hours
each employee has worked on that project. The following view can show this:
/* *** SQL-CREATE-VIEW-ExtB-06 *** */
CREATE VIEW EmployeeProjectHoursWorkedView AS
SELECT ProjectName, FirstName, LastName, HoursWorked
FROM EMPLOYEE AS E JOIN ASSIGNMENT AS A
ON E.EmployeeNumber = A.EmployeeNumber
JOIN PROJECT AS P
ON A.ProjectID = P.ProjectID;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
66
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.30 The Results for SQL-
67
Query-ExtB-15
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
69
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
70
• Such layering can be continued over many levels. We can turn this SELECT
statement into another view named ProjectsOverAllocatedMaxHoursView (again
without the ORDER BY clause):
/* *** SQL-CREATE-VIEW-ExtB-07 *** */ /* *** SQL-Query-ExtB-17 *** */
CREATE VIEW SELECT ProjectID, ProjectName,
ProjectsOverAllocatedMaxHoursView AS ProjectMaxHours,
SELECT ProjectID, ProjectName, ProjectMaxHours, ProjectHoursWorkedToDate,
ProjectHoursWorkedToDate (ProjectHoursWorkedToDate - ProjectMaxHours)
FROM ProjectHoursToDateView AS HoursOverMaxAllocated
WHERE ProjectHoursWorkedToDate > FROM ProjectsOverAllocatedMaxHoursView
ProjectMaxHours; ORDER BY ProjectID;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
71
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL/PSM User-Defined Functions
72
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.31 User-Defined Functions
73
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Example of Not Using a User-Defined
74
Function
To use SQL statements to create and use SQL user-defined functions
• Using the data in the WP database, we could, of course, simply include the
code to do this in an SQL statement using a concatenation operator as shown
below.
/* *** SQL-Query-ExtB-18 *** */
SELECT CONCAT(RTRIM(LastName),', ',RTRIM(FirstName)) AS EmployeeName,
Department, OfficePhone, EmailAddress
FROM EMPLOYEE
ORDER BY EmployeeName;
• The result of the query above is shown in Figure B-32 that follows.
• Notice how a user-defined function could be created for storing the same
name to be reused later shown in Figure B-33!
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.32 The Results for SQL-
75
Query-ExtB-18
Functions (1 of 2)
Functions (2 of 2)
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Using the User-Defined Function
78
Query-ExtB-19
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Four Common Uses of Triggers
82
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Trigger Example
83
employeeNumber = OLD.employeeNumber,
lastname = OLD.lastname,
changedat = NOW();
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
SQL/PSM Stored Procedures
84
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.36 Triggers Versus Stored
85
Procedures
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure B.37 Advantages of Stored
86
Procedures
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Stored Procedures Example
87
DELIMITER //
BEGIN
FROM EMPLOYEE;
END //
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Working with Microsoft Access
88
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Adding a Column to the CUSTOMER
89
Table
To introduce the topic of using Microsoft Access 2019 as an
application development platform
• Let’s take this one step at time. First, we add the NULL column
ReferredBy. To do this, we will use the SQL ALTER TABLE statement:
/* *** SQL-ALTER-TABLE-WA-ExtB-01 *** */
ALTER TABLE CUSTOMER
ADD ReferredBy INTEGER NULL;
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure WA.B.1 WMCRM Database
90
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure WA.B.2 WMCRM Database
91
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure WA.B.3 The ReferredBy
92
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure WA.B.6 The 1:1 Recursive
95
Relationship Is Confirmed
as a View Equivalent
To introduce the topic of using Microsoft Access 2019 as an application
development platform
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
98
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved
Figure WA.B.14 Importing Excel
104
Worksheet Data (1 of 2)
Worksheet Data (2 of 2)
Table Name
SPECIFICATIONS_2019 Table –
Datasheet View
Copyright © 2020, 2017, 2015 Pearson Education, Inc. All Rights Reserved