Académique Documents
Professionnel Documents
Culture Documents
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
MySQL for Developers
Activity Guide
D61830GC40
Edition 4.0 | June 2016 | D96434
Disclaimer
This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and
print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way.
Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United
States Government, the following notice is applicable:
ฺ b r) deฺ
Solution 3-4: Executing SQL in a Web Environment with PHP/PDO..............................................................3-25
c o m Gui
Practice 3-5: Executing SQL in a Web Environment with Connector/J ..........................................................3-30
p a rฺ ent
Solution 3-5: Executing SQL in a Web Environment with Connector/J ..........................................................3-32
Practice 3-6: Executing SQL in a Web Environment with Connector/Python .................................................3-37
one s Stud
Solution 3-6: Executing SQL in a Web Environment with Connector/Python .................................................3-40
s
Practices for Lesson 4: Prepared Statements
i c
Practices for Lesson 4: PreparedrStatements e thi
ci@................................................................................................4-2
...............................................................................................4-1
r toฺ intMySQL
o us...............................................................................................4-3
e
Practice 4-1: Preparing Statements
b Statements
i ( roPrepared
Solution 4-1: Preparing
e n sein MySQL...............................................................................................4-4
i c c
Practice 4-2: Using
l i c Statements with PHP/PDO..............................................................................4-6
o m Gui
Practices for Lesson 7: Tables and Views .....................................................................................................7-2
c
a rฺ ent
Practice 7-1: Creating New Tables from Existing Tables ...............................................................................7-3
p
one s Stud
Solution 7-1: Creating New Tables from Existing Tables ...............................................................................7-4
s
Practice 7-2: Creating a View .........................................................................................................................7-7
c i@ thi
Solution 7-2: Creating a View .........................................................................................................................7-8
c
o ฺ r i u se
Practice 7-3: Updating a View ........................................................................................................................7-10
t
r ........................................................................................................................7-11
Solution 7-3: Updating aeView to
b
ro a View s e
c (
Practice 7-4: Checking
i c e n .......................................................................................................................7-14
7-4: Checkinglia View .......................................................................................................................7-15
Solution c
R i 7-5:
o 7-5: Obtaining
Practice
ertSolution
Obtaining View Metadata ..........................................................................................................7-16
ฺ b r) deฺ
Practice 11-5: Avoiding Data Insertion Errors ................................................................................................11-14
o m Gui
Solution 11-5: Avoiding Data Insertion Errors ................................................................................................11-15
c
a rฺ ent
Practice 11-6: Truncating Table Data.............................................................................................................11-17
p
one s Stud
Solution 11-6: Truncating Table Data.............................................................................................................11-18
s
Practice 11-7: Modifying Table Data ..............................................................................................................11-20
c i@ thi
Solution 11-7: Modifying Table Data ..............................................................................................................11-21
c
o ฺ i u se
r ..........................................................................................................12-1
t
Practices for Lesson 12: Transactions
r o
12:eTransactionst...........................................................................................................12-2
Practices for Lesson b
(ro– Transactions, e
ns Isolation Levels, and Locking ................................................................12-3
Solutionic
i
Practice 12-1: Quiz
c li c e
12-1: Quiz – Transactions, Isolation Levels, and Locking ................................................................12-4
R
o 12-2: Using Transaction Control Statements....................................................................................12-5
ertSolution
Practice
b 12-2: Using Transaction Control Statements....................................................................................12-6
Ro Practice 12-3: Examining the Effect of Isolation Levels in Concurrent Transactions ......................................12-12
Solution 12-3: Examining the Effect of Isolation Levels in Concurrent Transactions ......................................12-14
Practices for Lesson 13: Query Optimization ................................................................................................13-1
Practices for Lesson 13: Query Optimization .................................................................................................13-2
Practice 13-1: Quiz – Optimization .................................................................................................................13-3
Solutions 13-1: Quiz – Optimization ...............................................................................................................13-4
Practice 13-2: Creating a Table with Indexes .................................................................................................13-5
Solutions 13-2: Creating a Table with Indexes ...............................................................................................13-6
Practice 13-3: Altering the Indexes in an Existing Table ................................................................................13-7
Solutions 13-3: Altering the Indexes in an Existing Table ..............................................................................13-8
Practice 13-4: Removing an Existing Index ....................................................................................................13-9
Solutions 13-4: Removing an Existing Index ..................................................................................................13-10
Practice 13-5: Using Optimization Techniques ..............................................................................................13-11
Solutions 13-5: Using Optimization Techniques .............................................................................................13-12
Practice 13-6: Identifying and Fixing Slow Queries with MySQL Enterprise Monitor Query Analyzer ............13-16
Solution 13-6: Identifying and Fixing Slow Queries with MySQL Enterprise Monitor Query Analyzer ............13-24
o m Gui
Practices for Lesson 15: Triggers and Scheduled Events ..............................................................................15-2
c
a rฺ ent
Practice 15-1: Creating and Dropping Triggers ..............................................................................................15-3
p
Practice 15-2: Investigating Triggers in the sakila s oDatabase S tud
ne ..........................................................................15-10
Solution 15-1: Creating and Dropping Triggers ..............................................................................................15-5
c
Solution 15-2: Investigating Triggers in i @sakila Database
the t h is ..........................................................................15-11
o ฺ
Practice 15-3: Creating a Scheduled
t ricEventu...................................................................................................15-15
se
Solution 15-3: Creating e
b r
a Scheduled to ...................................................................................................15-16
Event
(ro16: cReporting e
ns ................................................................................................................16-1
i
Practices for Lesson
c e
icfor Lesson 16:li Reporting ................................................................................................................16-2
Practices
R
o 16-1: Aggregating Data ....................................................................................................................16-3
ertSolution
Practice
b 16-1: Aggregating Data ....................................................................................................................16-5
Ro Practice 16-2: Grouping Aggregated Data .....................................................................................................16-9
Solution 16-2: Grouping Aggregated Data .....................................................................................................16-10
Practice 16-3: Creating a Crosstab Report with a Bar Chart ..........................................................................16-13
Solution 16-3: Creating a Crosstab Report with a Bar Chart ..........................................................................16-14
Practices for Lesson 17: NoSQL.....................................................................................................................17-1
Practices for Lesson 17: Building NoSQL Applications ..................................................................................17-2
Practice 17-1: Configuring the Memcached Plug-in for InnoDB .....................................................................17-4
Solution 17-1: Configuring the Memcached Plug-in for InnoDB .....................................................................17-5
Practice 17-2: Using the Memcached Plug-in for InnoDB with PHP...............................................................17-9
Solution 17-2: Using the Memcached Plug-in for InnoDB with PHP...............................................................17-11
Practice 17-3: Using the Memcached Plug-in for InnoDB with Java ..............................................................17-16
Solution 17-3: Using the Memcached Plug-in for InnoDB with Java ..............................................................17-18
Practice 17-4: Using the Memcached Plug-in for InnoDB with Python ...........................................................17-24
Solution 17-4: Using the Memcached Plug-in for InnoDB with Python ...........................................................17-26
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 1:
om t Gto
Introduction i
u MySQL
r ฺ c
a 1 den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
• You are logged in to the Oracle Linux operating system as the root user (password:
oracle).
• MySQL Enterprise Edition (server version 5.7.12 or later) is installed.
• MySQL Workbench (version 6.3.6 or later) is installed.
• MySQL Enterprise Monitor (version 3.2 or later) is installed.
• The Apache web server is installed and running.
• Apache Tomcat is installed and not running. ble
fe r a
• The Mozilla Firefox web browser is installed.
ans
• The sakila and world sample databases are installed.
n - t r
o
•
an
The employees sample database installation files are located in the
/stage/databases/employees_db directory. s
) a
hscripts.
• The /labs directory contains the course practice files
ฺ b r
and
d e ฺ
om t Gu i
r ฺ
a denc
e p
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
Duration
This practice should take you approximately 30 minutes to complete.
Tasks
1. This course uses the MySQL Enterprise Edition (server version 5.7.12 or later), which is
installed for you. Confirm that the server is installed correctly.
Enter the following command at the Linux terminal prompt and receive the results shown:
ble
# mysql --version
fe r a
ns
mysql Ver 14.14 Distrib 5.7.12, for Linux (x86_64) using EditLine
wrapper
t r a
-not required
2. MySQL Workbench version 6.3.6 or later is installed in Oracle classrooms. o n
It is
n to interact with
for course activities, but is available in the course environment if you prefer
s a
MySQL by using a graphical tool. Confirm that MySQL Workbench
performing the following steps: r ) ha is installed
ฺ
by
R ob version 3.2 or later and configure it for use by performing the following steps:
a. Ensure that the MySQL Enterprise Monitor services are running by entering the
following commands at a Linux terminal prompt and receiving the results shown:
# service mysql-monitor-server status
MySQL Enterprise MySQL is running
MySQL Enterprise Tomcat is running
− Note: If the services shown above are not running, start them by executing the
following command at the Linux terminal prompt:
# service mysql-monitor-server start
Starting mysql service [ OK ]
160511 11:16:51 mysqld_safe Logging to
'/opt/mysql/enterprise/monitor/mysql/runtime/mysqld.log'.
160511 11:16:51 mysqld_safe Starting mysqld daemon with databases from
/opt/mysql/enterprise/monitor/mysql/data/
Starting tomcat service [ OK ]
b. Configure Mozilla Firefox to make it easier to enter the URL of a local server without
Firefox attempting to append “.com” to it. Open Firefox and perform the following steps:
− Enter about:config in the address bar.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
set to True, right-click the setting and select Toggle to change its value to False.
c. Enter the following URL in the Mozilla Firefox address bar:
https://localhost:18443/
− A page appears with the message “This Connection is Untrusted.”
d. Click “I understand the risks.”
e. Click the “Add Exception” button.
f. In the “Add Security Exception” dialog box, select the “Permanently store this
ble
exception” check box and click the “Confirm Security Exception” button.
fe r a
− The MySQL Enterprise Dashboard Setup page appears. ans
g. In the “Create user with ‘manager’ role” pane, enter the following: n - t r
− Username: monitormanager a no
− Password: oracle h a s
ฺ b r) deฺ
− Confirm Password: oracle
c o m Gui
ฺ thenfollowing:
h. In the “Create user with ‘agent’ role” pane, renter
a t
p e
− Username: monitoragent ne
s o S tud
− Password: oracle
c i @ this
− Confirm Password:
t o ฺ ricoracle
u se
i. Ensure that the
b e r MySQL tEnterprise
o Dashboard Setup page appears as follows:
r o s e
c c i ( licen
R i
ert o
b
Ro
c i (ro tomcat
Stopping
c e nsservice . [ OK ]
Ric li service 160511 11:19:58 mysqld_safe mysqld from
Stopping mysql pid
ob
. [ OK ]
R 4. This course uses the world, sakila, and employees sample databases. The world
and sakila databases are installed for you. You will install the employees database as
part of the “Optimizing Queries” lesson activities.
a. Verify that the world and sakila databases are installed by entering the following
command at a Linux terminal prompt and receiving the results shown:
# ls -l /var/lib/mysql
total 122956
-rw-r----- 1 mysql mysql 56 May 10 13:08 auto.cnf
-rw------- 1 mysql mysql 1680 May 10 13:08 ca-key.pem
-rw-r--r-- 1 mysql mysql 1075 May 10 13:08 ca.pem
-rw-r--r-- 1 mysql mysql 1079 May 10 13:08 client-cert.pem
-rw------- 1 mysql mysql 1676 May 10 13:08 client-key.pem
-rw-r----- 1 mysql mysql 1119 May 10 13:12 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 May 10 13:14 ibdata1
-rw-r----- 1 mysql mysql 50331648 May 10 13:14 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 May 10 13:08 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 May 10 13:14 ibtmp1
drwxr-x--- 2 mysql mysql 4096 May 10 13:08 mysql
( r o prompt
n s e the results shown:
i ci -l16/labs
# ls
c l i ce
t o R drwxr-xr-x 3 root root 4096 May 10 13:11 java
total
│ ├── cli
│ │ └── ...
│ └── web -> /var/www/cgi-bin
├── scripts
│ └── ...
└── sql
└── ...
ble
fe r a
18 directories, 98 files
an s
7. For the web-based practices to work correctly, you must ensure that the relevant web
n - t r
server is running and, if not, start it manually. The commands you use to do this depend on o
an
whether you are using the Apache web server or Tomcat application server.
s
If you are using Apache (for PHP and Python):
r ) ha ฺ
ฺb uide
a. Enter the following commands at the Linux terminal prompt to verify that you can start,
m
a r ฺco nt G
restart, and check the status of the Apache web server:
# service httpd status
o n ep tude
httpd (pid 16248) is running...
@ s is S
# service httpd stop
c c i th
Stopping httpd:
r i us e [ OK ]
# service httpd
e r toฺstatust o
robhttpd:
httpd is stopped
(
# service
n e
httpd sstart
c i c e
Ri#c servicelihttpd status
Starting [ OK ]
cc lic
Djava.util.logging.config.file=/opt/apache-tomcat-
to Ri7.0.64/conf/logging.properties -
e r Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -
ob
Djava.endorsed.dirs=/opt/apache-tomcat-7.0.64/endorsed -classpath
R /opt/apache-tomcat-7.0.64/bin/bootstrap.jar:/opt/apache-tomcat-
7.0.64/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7.0.64 -
Dcatalina.home=/opt/apache-tomcat-7.0.64 -Djava.io.tmpdir=/opt/apache-
tomcat-7.0.64/temp org.apache.catalina.startup.Bootstrap start
root 20035 14213 0 11:25 pts/1 00:00:00 grep tomcat
− The Tomcat server is running under the process ID (pid) 19994.
− Note: The pid number on your system will probably be different.
b. Open the Mozilla Firefox web browser and enter the following URL in the address bar:
http://localhost:8080
− Ensure that you can see the Tomcat page. If not, use the tc_start command at
the Linux terminal prompt to start the Tomcat server. Ensure that the Tomcat
application server is running before proceeding.
c. Exit the Mozilla Firefox web browser.
8. This Activity Guide contains the practices and solutions for each lesson. The course
environment includes lab scripts and solution files that you can use to copy and paste
commands and statements to complete the practices. However, you are encouraged to
Enter the following command at the Linux terminal prompt and receive the results shown:
# tree -l /labs
/labs
├── java
│ ├── cli
│ │ ├── activity17-3
│ │ │ └── MemcachedTest.java
ble
│ │ ├── activity5-4
fe r a
│ │ │ ├── ExceptionExample_soln.java
ans
│ │ │ └── ExceptionExample_start.java
n - t r
│ │ └── lib o
│ │ ├── commons-pool-1.5.6.jar s an
│ │ ├── java_memcached-release_2.6.6.jar
r ) ha ฺ
│ │ ฺb uide
├── mysql-connector-java-commercial-5.1.38-
m
a r ฺco nt G
bin.jar
│
│
│
│ o n ep tude
├── slf4j-api-1.6.1.jar
└── slf4j-simple-1.6.1.jar
@ s is S
│
c c i
└── web -> /opt/apache-tomcat-7.0.64/webapps/
th
r i e
toฺ to us
│ ├── activity3-2
│ │ e r
├── conn_soln.inc
│
i ( rob ense
│ ├── conn_start.inc
c lic
Ric
│ │ ├── index_soln.jsp
e r to │ │ ├── index_start.jsp
R ob │
│
│
│
└── WEB-INF
├── classes
│ │ └── lib
│ ├── activity3-5
│ │ ├── index_soln.jsp
│ │ ├── index_start.jsp
│ │ └── WEB-INF
│ │ ├── classes
│ │ ├── conn.inc
│ │ └── lib
│ ├── activity4-3
│ │ ├── addfilm_soln.jsp
│ │ ├── addfilm_start.jsp
│ │ ├── findfilm_soln.jsp
│ │ ├── findfilm_start.jsp
│ │ ├── index.html
│ │ └── WEB-INF
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
│ │ └── WEB-INF
│ │ ├── classes
│ │ ├── conn.inc
│ │ └── lib
│ ├── activity6-5
│ │ ├── index.jsp
│ │ ├── results_soln.jsp
ble
│ │ ├── results_start.jsp
fe r a
│ │ └── WEB-INF
ans
│ │ ├── classes
n - t r
o
an
│ │ ├── conn.inc
│ │ └── lib s
ha ฺ
│ └── activity6-8 r )
ฺb uide
│ ├── index_soln.jsp m
│ ├── index_start.jsp
a r ฺco nt G
│ └── WEB-INF
o n ep tude
│ ├── classes
@ s is S
│ c
├── conn.inc
c i th
r i e
│
├── php e r toฺ to us
└── lib
│ ( rob ense
├── cli
i
c lic
to Ric│
│
│
│
├── activity17-2
│ └── memcached_test.php
e r
R ob │
│
│
│
└── activity5-3
├── ExceptionExample_soln.php
│ │ └── ExceptionExample_start.php
│ └── web -> /var/www/html/php
│ ├── activity3-1
│ │ ├── conn_soln.php
│ │ ├── conn_start.php
│ │ ├── index_soln.php
│ │ └── index_start.php
│ ├── activity3-4
│ │ ├── conn.php
│ │ ├── index_soln.php
│ │ └── index_start.php
│ ├── activity4-2
│ │ ├── addfilm_soln.php
│ │ ├── addfilm_start.php
│ │ ├── index.php
│ │ ├── layout_soln.php
│ │ └── layout_start.php
│ ├── activity6-4
│ │ ├── conn.php
│ │ ├── index.php
│ │ ├── layout.php
ble
│ │ ├── results_soln.php
fe r a
│ │ └── results_start.php
ans
│ └── activity6-7
n - t r
o
an
│ ├── conn.php
│ ├── index.php s
ha ฺ
│ └── index_start.php r )
ฺb uide
├── python m
│ ├── cli
a r ฺco nt G
│ │ ├── activity17-4
o n ep tude
│ │ │ s is S
└── memcached_test.py
@
│ │ └── activity5-5
c c i th
r i e
│
│
│
│ e r toฺ to us
├── ExceptionExample_soln.py
└── ExceptionExample_start.py
│ ( rob ense
└── web -> /var/www/cgi-bin
i
c lic
to Ric│
│
├── activity3-3
│ ├── conn_soln.py
e r
R ob │
│
│
│
├── conn_start.py
├── index_soln.py
│ │ └── index_start.py
│ ├── activity3-6
│ │ ├── conn.py
│ │ ├── index_soln.py
│ │ └── index_start.py
│ ├── activity4-4
│ │ ├── addfilm_soln.py
│ │ ├── addfilm_start.py
│ │ ├── conn.py
│ │ ├── findfilm_soln.py
│ │ ├── findfilm_start.py
│ │ └── index.py
│ ├── activity6-3
│ │ ├── conn.py
│ │ ├── layout.py
│ │ ├── results_soln.py
│ │ └── results_start.py
│ └── activity6-9
│ ├── conn.py
│ ├── index_soln.py
│ ├── index_start.py
ble
│ └── results.py
fe r a
├── scripts
ans
│ ├── Practice_01-1.lab
n - t r
o
an
│ ├── Practice_02-1.lab
│ ├── Practice_03-1.lab s
ha ฺ
│ └── ... r )
ฺb uide
└── sql m
├── employees2.sql
a r ฺco nt G
├── film2.sql
o n ep tude
├── logins.sql
@ s is S
└── sensor.sql
c c i th
r i us e
e r
54 directories, toฺ 172to files
b terminal
roLinux se windows.
(
9. Exit any open
i e n
R icc lic
berto
Ro
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 2:
MySQL omConnectors
u i and APIs
r ฺ c
a 2 den t G
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
None
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Questions
1. Where must you install Connectors?
a. You must install the Connector on every client host where programs that use the
connector run.
b. You must install the Connector on the server host only. This makes it available to all
clients connecting from remote hosts. ble
fe r a
c. You must install the Connector on both the server host and all client hosts.
ans
2. Which of the following statements are true? (Choose all that apply.)
n - t r
o
an
a. Drivers for MySQL are available for all operating systems that MySQL supports.
b. MySQL server distributions include drivers for MySQL. s
ha ฺ
c. The MySQL GUI tools include drivers for MySQL. r )
ฺb uide
m
d. Oracle Corporation ships drivers for MySQL independently of MySQL Server
distributions.
a r ฺco nt G
o n ep tude
e. Third parties ship drivers for MySQL independently of MySQL Server distributions.
3. s is S
Which of the following is a good reason for using PDO instead of mysqli when developing
@
PHP/MySQL web applications?
c c i th
r i e
e r toฺ to us
a. PDO supports more features of MySQL than mysqli.
i ( rob ense
b. PDO is newer than mysqli and therefore more reliable.
c lic
c. PDO is more portable than mysqli.
to Ric
d. PDO can be run as an Apache server module, and mysqli cannot.
e r
4. All MySQL Connectors are based on MySQL’s C API and are implemented using the
R ob MySQL C client library.
a. True
b. False
5. MySQL Connectors are written in C, like all other MySQL client programs.
a. True
b. False
download directly from Oracle Corporation and others from third-party websites:
http://www.mysql.com/products/connector/
3. c. The primary reason for using PDO is that it provides a standard way to access many
different relational databases.
4. b. False. There are many different architectures for Connectors. Some Connectors are
based on MySQL’s C API (using libmysql). Others implement the MySQL protocol entirely
within the host language or environment. One (the MySQL JavaScript Connector for
Node.js and MySQL Cluster) is written partly in JavaScript and partly in C, but without ble
libmysql. fe r a
ans
5. b. False. Developers create Connectors using many languages. For example, Connector/J
is written entirely in Java. n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 3: Using
Connectorsom t Gu i
r ฺ c
a 3 den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Important: The coding practices in this lesson are available in PHP, Java, and Python. Perform
all the practices for this and all future coding activities in only one language, choosing the
language you are most familiar with. If in doubt, perform the PHP activities. Ensure that you
complete all practices that are not language-specific.
For this lesson, perform the following practices:
• If your chosen programming language is PHP:
− Practice 3-1: Connecting to the MySQL Server in a Web Environment with ble
PHP/PDO
fe r a
− Practice 3-4: Executing SQL in a Web Environment with PHP/PDO ans
• If your chosen programming language is Java: n - t r
a no
− Practice 3-2: Connecting to the MySQL Server in a Web Environment with
Connector/J h a s
ฺ b r) deฺ
− Practice 3-5: Executing SQL in a Web Environment with PHP/PDO
• If your chosen programming language is Python: c o m Gui
p a rฺ ent
− Practice 3-3: Connecting to the MySQL Server in a Web Environment with
Connector/Python
s one s Stud
c c i@ thi
− Practice 3-6: Executing SQL in a Web Environment with Connector/Python
o ฺ r i u se
Assumptions e r t to
b e
• You have
c i (rodecided
c e s programming language (PHP, Java, or Python) to use for the
nwhich
R
practices.
li
iIfcyou are completing
•
e r to − The Apache web server the coding practices in PHP:
• You have access to the MySQL server root account (password: oracle).
• The MySQL server contains the world and sakila databases.
• The /labs directory contains the course practice files and scripts.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• The Apache server is running.
Duration
This practice should take you approximately 20 minutes to complete.
ble
Tasks fe r a
ans
1. In a Linux terminal window logged in as the root Linux user, change the current working
directory to /labs/php and use the ls -al command to display a long list of the directory n - t r
o
contents.
s an
What does the /labs/php/web directory represent?
r ) ha ฺ
ฺb uide
2. Change the current working directory to /labs/php/web/activity3-1 and list the files
m
within that directory.
a r ฺco nt G
3. Copy the index_start.php and conn_start.php files to index.php and conn.php,
respectively.
o n ep tude
@ s is S
4. Examine the PHP code in conn.php in a text editor such as gedit, emacs, or vim.
c c i th
r i e
5. Under the comment labelled “A. Create the DSN”, add a suitable DSN (data source name)
r toฺ to us
to connect to the world database on the local MySQL server. Store the DSN in a variable
e
called $dsn.
i ( rob ense
c lic
6. Under the comment labelled “B. Connect to the database server”, create a new instance of
to Ric
the PDO class, passing in the DSN, the MySQL user name root, and the password
e r oracle. If the connection is successful, display a suitable message to the user.
R ob 7. Under the comment labelled “C. Disconnect from the database server”, close the
connection to the MySQL server and display a suitable message to the user.
8. Save and close the conn.php file.
9. Examine the HTML markup in index.php in your chosen text editor.
10. Under the comment labelled “D. Add a reference to the conn.php utility library file”, write
PHP code that references the conn.php utility library file.
11. Save and close the index.php file.
12. Open the index.php page in the Firefox browser using the following URL: http://localhost
/php/web/activity3-1/index.php.
ble
fe r a
ans
n - t r
a no
h a s
The application connects to the database and then immediately ฺ b r) disconnects,
d e ฺ displaying an
om t Gu i
appropriate message at each stage of the process.
r ฺ c
a youdprobablyn have an error in your PHP
p
Note: If the page does not display as expected,
e e
code. To diagnose the error, examine
s n contents
othe S tuof the Apache error log file (located at
/var/log/httpd/error_log
c i in Oracle s
i in theenvironments.)
classroom
@last fewthentries Use a suitable Linux
terminal command to display
t o ฺ ric use
the log, such as:
r to
# tail -10 /var/log/httpd/error_log
b e
(roandcthe
14. Leave Firefox e
s terminal window open for the next practice.
nLinux
c i li e
R ic
berto
Ro
contents.
What does the /labs/php/web directory represent?
Enter the following commands at the Linux terminal prompt and receive the results shown:
# ls -al
total 12
drwxr-xr-x 3 root root 4096 May 10 13:11 .
drwxr-xr-x 6 root root 4096 May 10 13:11 ..
drwxr-xr-x 4 root root 4096 Apr 15 09:07 cli
ble
lrwxrwxrwx 1 root root 17 May 10 13:11 web -> /var/www/html/php
fe r a
Answer: The /labs/php/web directory is a symbolic link to a folder called php in the ans
Apache server’s document root (var/www/html.) n - t r
2. no
Change the current working directory to /labs/php/web/activity3-1 and list the files
a
within that directory.
h a s
ฺ b r) deฺ
Enter the following command at the Linux terminal prompt and receive the results shown:
# cd /labs/php/web/activity3-1
c o m Gui
# ls
conn_soln.php conn_start.php ep
arฺ dent index_start.php
index_soln.php
3. Copy the index_start.php and s on s Stu files to index.php and conn.php,
i@ thi
conn_start.php
respectively.
i c c
t o ฺ
Enter the following commandsr atuthe seLinux terminal prompt and receive the results shown:
b e r toindex.php
ro ens conn.php
# cp index_start.php
(conn_start.php e
# cp
c
c lsi lic
R i#conn.php
erto conn_soln.php index.php
conn_start.php index_soln.php
b index_start.php
Ro 4. Examine the PHP code in conn.php in a text editor such as gedit, emacs, or vim.
The contents of the conn.php file are as follows:
<?php
try {
# A. Create the DSN
}
catch (PDOException $e) {
die ("<p>Cannot connect to the database server</p>");
}
?>
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
one s Stud
}
...
s
7. Under the comment labelledc“C.
i c e t hi from the database server”, close the
i@Disconnect
connection to the MySQL
r t o ฺr serveruand
s display a suitable message to the user.
be linessofePHP
Enter the following o
t code under the comment labelled “C. Disconnect from the
r o
database server”:
i ( licen
i c
...c
t o R
e r # C. Disconnect from the database server
R ob $dbh = NULL;
echo "<p>Disconnected from the database server</p>";
?>
...
8. Save and close the conn.php file.
9. Examine the HTML markup in index.php in your chosen text editor.
The index.php file contains the following HTML markup:
<!doctype html>
<html>
<head>
<title>Activity 3-1 (PHP)</title>
</head>
<body>
<h1>Activity 3-1 (PHP)</h1>
<p>
<strong>Connection status:</strong>
<?php
# D. Add a reference to the conn.php utility library file
Enter the following line of PHP code under the comment labelled “D. Add a reference to the
conn.php utility library file”:
<body>
<h1>Activity 3-1 (PHP)</h1>
<p>
<strong>Connection status:</strong>
<?php
# D. Add a reference to the conn.php utility library file
require_once("conn.php");
ble
?>
fe r a
</body>
ans
11. Save and close the index.php file.
n - t r
o
/php/activity3-1/index.php. s an
12. Open the index.php page in the Firefox browser using the following URL: http://localhost
) ha ฺ
13. Verify that you receive the following messages in the webpage:
r
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
The application connects to the database and then immediately disconnects, displaying an
appropriate message at each stage of the process.
Note: If the page does not display as expected, you probably have an error in your PHP
code. To diagnose the error, examine the contents of the Apache error log file (located at
/var/log/httpd/error_log in Oracle classroom environments.) Use a suitable Linux
terminal command to display the last few entries in the log, such as:
# tail -10 /var/log/httpd/error_log
14. Leave Firefox and the Linux terminal window open for the next practice.
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• The Tomcat server is running.
Duration
This practice should take you approximately 20 minutes to complete.
ble
Tasks fe r a
ans
1. In a Linux terminal window logged in as the root Linux user, display the contents of the
$CATALINA_HOME/lib folder. Verify that the .jar files for Connector/J and JSTL (the n - t r
o
JSP standard tag library) are present.
s an
r ) ha ฺ
Note: $CATALINA_HOME refers to the root of the Tomcat server installation. The lib
a r ฺco nt G
2. Change the current working directory to /labs/java and use the ls -al command to
o n ep tude
display a long list of the directory contents.
s is S
What does the /labs/java/web directory represent?
@
c c i th
i e
3. Change the current working directory to /labs/java/web/activity3-2 and list the files
r
e r toฺ to us
within that directory and its subdirectories.
Notes:
i ( rob ense
c lic
− You can use the tree command to display a hierarchical listing of this folder’s
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
The application connects to the database o neand then d
tuimmediately disconnects, displaying an
s s S
appropriate message at each stage
i c c
of
e
the
t hi consider the following:
i@as expected,
process.
Note: If the page does not
r t o ฺr display
us You can use the tc_status alias in Oracle
− Is the Tomcate server t o
running?
r ob environments
classroom
i ( n se to check if Tomcat is running, and use tc_start and
i−cctc_stop l e
icto start and stop the Tomcat server.
R You might have an error in your JSTL markup. To diagnose the error, review the
berto stack trace displayed in the browser, or examine the Tomcat log files
Ro (localhost.[date].log) in $CATALINA_HOME/logs.
14. Leave Firefox and the Linux terminal window open for the next practice
R ic
Enter the li
following commands Linux terminal prompt and receive the results shown:
erto # ls -al
# cd /labs/java
b
Ro total 12
drwxr-xr-x 3 root root 4096 May 11 14:42 .
drwxr-xr-x 6 root root 4096 May 10 13:11 ..
drwxr-xr-x 5 root root 4096 May 10 13:11 cli
lrwxrwxrwx 1 root root 34 May 10 13:11 web -> /opt/apache-tomcat-
7.0.64/webapps/
Answer: The /labs/java/web directory is a symbolic link to the Tomcat server’s
webapps directory (/opt/apache-tomcat-7.0.64/webapps in Oracle classroom
environments.)
3. Change the current working directory to /labs/java/web/activity3-2 and list the files
within that directory and its subdirectories.
Notes:
− You can use the tree command to display a hierarchical listing of this folder’s
subdirectories and its contents.
− The server only makes files in the application’s document root available to clients.
Files placed within WEB-INF are never served directly to clients.
Enter the following command at the Linux terminal prompt and receive the results shown:
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
└── WEB-INF
├── classes
└── lib
• The root of the activity3-2 directory (the “document root”) contains start and
solution versions of the files that the web application requires.
• The WEB-INF directory contains all application resources that are not found within the
document root, including any deployment descriptor file.
ble
• The WEB-INF/classes directory is for any Java servlet and utility classes the web
fe r a
application requires, and is currently empty.
ans
• The WEB-INF/libs directory contains any application-specific .jar files and is
n - t r
currently empty.
n o
4. Copy the index_start.jsp file to index.jsp within the document root. s a
h a
ฺ b r) deฺ
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp index_start.jsp index.jsp
c o m Gui
# ls
conn_soln.inc index.jsp
p a rฺ ent
index_start.jsp
conn_start.inc index_soln.jsp
s one sWEB-INF
S tud
5. Copy the conn_start.inc file i@to a new
ccontains i called conn.inc within the application’s
thfile
WEB-INF directory. Thisrfile
ฺ i c s econnection information. Clients should never use this
file directly. r t o u
toat the Linux terminal prompt and receive the results shown:
b e e
i ro ens
Enter the following commands
(conn_start.inc
#c
R
cp
lic
i#c ls WEB-INF WEB-INF/conn.inc
<c:catch var="connexception">
<sql:setDataSource
var="conn"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql//localhost/world"
user="root"
password="oracle"
/>
</c:catch>
ble
8. Save and close the WEB-INF/conn.inc file.
fe r a
9. Examine the JSTL code in index.jsp in the activity3-2 directory. ans
The index.jsp file contains the following JSTL code: n - t r
o
s an
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
r ) ha ฺ
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ include file="/WEB-INF/conn.inc" %>
m ฺb uide
<!DOCTYPE html>
a r ฺco nt G
ep tude
<html>
<head>
<title>Activity 3-2 s o n S
i @ h s
(Java)</title>
i
</head>
<body>
ฺ r i cc se t
<h1>Activityr u
to 3-2to(Java)</h1>
e
ob A. nUnsuccessful
i
<c:choose>
( r<%-- e se connection attempt --%>
c c l i c
to Ri <%-- B. Successful connection attempt --%>
e r
R ob </c:choose>
</body>
</html>
Notes:
− The taglib directives at the top of the file define tag prefixes for the JSTL core
and sql tag libraries respectively.
− The include directive makes the contents of the conn.inc file you created in
WEB-INF available to the page.
− The <c:choose> tag must contain one or more <c:when> tags with a test
attribute to allow for conditional processing.
10. Under the comment labelled “A. Unsuccessful connection attempt”, provide a <c:when>
tag which tests whether an exception is raised by the code in WEB-INF/conn.inc. If an
exception occurs, display an “error connecting to the database” method to the user.
Enter the following lines of JSTL code in the index.jsp file:
...
<c:choose>
<%-- A. Unsuccessful connection attempt --%>
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
The application connects to the database and then immediately disconnects, displaying a
message that signifies either success or failure.
Note: If the page displays an error message, consider the following:
14. Leave Firefox and the Linux terminal window open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• The Apache server is running.
Duration
This practice should take you approximately 20 minutes to complete.
ble
Tasks fe r a
ans
1. In a Linux terminal window logged in as the root Linux user, change the current working
directory to /labs/python and use the ls -al command to display a long list of the n - t r
o
directory contents.
s an
What does the /labs/python/web directory represent?
r ) ha ฺ
ฺb uide
2. Change the current working directory to /labs/python/web/activity3-3 and list the
m
contents of the directory.
a r ฺco nt G
3. Copy the index_start.py and conn_start.py files to index.py and conn.py
respectively.
o n ep tude
@ s is S
4. Examine the Python code in conn.py in a text editor such as gedit, emacs, or vim.
c c i th
r i e
5. Under the comment labelled “A. Add a reference to the Connector/Python module”, provide
r toฺ to us
the necessary import statement to include the Connector/Python module in your
e
application.
i ( rob ense
c lic
6. Under the comment labelled “B. Provide appropriate connection parameters”, enter values
to Ric
for the conn_params dictionary object’s properties that allows the object to be used in a
e r call to the Connector/Python connect() method. This creates a connection to the world
R ob database on the local MySQL server instance as the root user (password: oracle).
7. Under the comment labelled “C. Connect to the database and return the connection object”
within the dbconnect() function:
• Call the Connector/Python module’s connect() method, passing the conn_params
object as the parameter.
• Store the resulting connection object in the global variable conn.
• Display a suitable message to indicate to the user that the connection was successful.
• Return the connection object to the script that calls the conn.py library file’s
dbconnect() function.
8. Save and close conn.py.
9. Examine the Python code in index.py in your chosen text editor.
10. Under the comment labelled “D. Create a reference to the conn library file”, add an import
statement to reference the conn.py file.
11. Under the comment labelled “E. Connect to the database”, call the dbconnect() method
in the conn.py library file. Store the connection object that the dbconnect() method
returns in a variable called dbconn.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e t
r to the to database and then immediately disconnects, displaying an
The applicationbconnects e
i o
(rmessage natseach stage of the process.
appropriate
c c e
icIf the pageli does not display as expected, you probably have an error in your Python
Note:
R
bertocode. To diagnose the error, examine the contents of the Apache error log file (located at
/var/log/httpd/error_log in Oracle classroom environments.) Use a suitable Linux
Ro terminal command to display the last few entries in the log, such as:
# tail -10 /var/log/httpd/error_log
16. Leave Firefox and the Linux terminal window open for the next practice.
directory contents.
What does the /labs/python/web directory represent?
Enter the following commands at the Linux terminal prompt and receive the results shown:
# ls -al
total 12
drwxr-xr-x 3 root root 4096 May 10 13:11 .
drwxr-xr-x 6 root root 4096 May 10 13:11 ..
drwxr-xr-x 4 root root 4096 Apr 15 09:38 cli
ble
lrwxrwxrwx 1 root root 16 May 10 13:11 web -> /var/www/cgi-bin
fe r a
Answer: The /labs/python/web directory is a symbolic link to the Apache server’s ans
cgi-bin directory. The cgi-bin directory is where the web server stores CGI (common n - t r
no
gateway interface) scripts. CGI is a standard method used to generate dynamic webpage
a
content.
h a s
2. r) deฺ
Change the current working directory to /labs/python/web/activity3-3 and list the
ฺ b
contents of the directory.
c o m Gui
p a rฺ ent
Enter the following commands at the Linux terminal prompt and receive the results shown:
one s Stud
# cd /labs/python/web/activity3-3
# ls s
conn_soln.py conn_start.py
i c c e hi
i@ tindex_soln.py index_start.py
3. Copy the index_start.py
r t o ฺr anduconn_start.py
s files to index.py and conn.py.
respectively. e t o
( rob ecommands
Enter the following
i n se at the Linux terminal prompt and receive the results shown:
R i#cccp index_start.py
lic index.py
e rt o # cp conn_start.py conn.py
o b # ls
R conn.py conn_soln.py
index_start.py
conn_start.py index.py index_soln.py
4. Examine the Python code in conn.py in a text editor such as gedit, emacs, or vim.
The contents of the conn.py file are as follows:
# conn.py: library file for connecting to MySQL using the
# Connector/Python module
conn = None
except:
print('''<p>Could not connect to the database</p>''')
def dbclose():
if conn is None:
print('''</p>Can't disconnect: there is no connection</p>''')
else:
# D. Close the connection
R ob </html>
''')
10. Under the comment labelled “D. Create a reference to the conn library file”, add an import
statement to reference the conn.py file.
Enter the following line of code under the comment labelled “D. Create a reference to the
conn file”:
#!/usr/bin/python
database”:
...
# F. Disconnect from the database
print("<p>Disconnecting from the database server</p>")
dbconn.close()
print('''
</body>
</html>
''')
ble
13. Save and close index.py.
fe r a
14. Make the conn.py and index.py scripts executable. ans
Enter the following commands at the Linux terminal prompt and receive the results shown: n - t r
a no
# chmod +x conn.py
# ls -al
index.py
h a s
total 24
root 4096 May 11 15:31 ฺ.b
r) deฺ
drwxr-xr-x 2 root
c o m .. Gui
drwxr-xr-x. 3 root
ฺ
11r 15:28 n
root 4096 May 11 15:24
root 814 May a t
-rwxr-xr-x 1 root
p e conn.py
ud conn_soln.py
e 11 t15:23
-rw-r--r-- 1 root root
root s o0nMay S
-rw-r--r-- 1 root
-rwxr-xr-x 1 root
c i
root
652
is11 15:33 index.py
May 11
@ 418 tMay
h
15:26 conn_start.py
ฺ
-rw-r--r-- 1 root
t o ricrootu e0 May 11 15:23 index_soln.py
s313
r
-rw-r--r-- 1 root
e t o
root May 11 15:31 index_start.py
robmayenread,
The file's owner
i ( sewrite, and execute the conn.py and index.py files. Users in
c grouplcan
the root
icexecute ic read and execute the conn.py and index.py files. Other users can
R
only the conn.py and index.py files.
berto
Ro
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
The application connects to the databaseeand p rฺ immediately
athen e n t disconnects, displaying an
appropriate message at each stage of
s S tud
onthe process.
Note: If the page does not display
c i t h is you probably have an error in your Python
@as expected,
ฺ
code. To diagnose the error,
t o ric examine
u s ethe contents of the Apache error log file (located at
e r
/var/log/httpd/error_log
b tothe last
in Oracle classroom environments.) Use a suitable Linux
( ro ens
terminal command to e
display few entries in the log, such as:
i
R i#cctail -10lic/var/log/httpd/error_log
erto
16. Leave Firefox and the Linux terminal window open for the next practice.
b
Ro
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• Mozilla Firefox is open.
• The Apache server is running.
Duration
This practice should take you approximately 20 minutes to complete.
ble
Tasks fe r a
ans
1. Log in to the mysql command line client as the root user (password: oracle).
n - t r
2. Create a new table in the world database called GelderlandDist, which consists of the o
an
Name, District, and CountryCode columns for all cities in the City table within the
s
Gelderland district.
r ) ha ฺ
3. Exit the mysql command line client.
m ฺb uide
contents of the directory. a r ฺco nt G
4. Change the current working directory to /labs/php/web/activity3-4 and list the
o n ep tude
5. Examine the PHP code in the conn.php file in your chosen text editor.
@ s is S
i th
6. Copy the index_start.php file to index.php.
c c
r i e
toฺ to us
7. Examine the contents of the index.php file in your chosen text editor.
e r
8. Under the comment labelled “A. Define the query”, populate the variable $sql1 with a SQL
( rob ense
statement that retrieves all rows from the GelderlandDist table.
i
c lic
to Ric
9. Under the comment labelled “B. Execute the query”, call the appropriate method on the
$dbh connection object to execute the query using the SQL statement in $sql1. Store the
e r returned statement handle in a variable called $sth.
R ob 10. Under the comment labelled “C. Display the returned rows”, create a while loop that
returns a row from the result set as an associative array of column values by using the
$sth->fetch() method. Display the contents of the row to the user as a comma-
separated list of column values. Continue looping through the rows until you have displayed
all the rows in the GelderlandDist table.
11. Under the comment labelled “D. Define the insert”, populate the variable $sql2 with a SQL
statement that inserts a new row into the GelderlandDist table with the following column
values:
• Name: Atlantis
• District: Gelderland
• CountryCode: NLD
12. Under the comment labelled “E. Execute the insert”, call the appropriate method on the
$dbh connection object to execute the INSERT statement in variable $sql2. Store the
return value in a variable called $num and display this value to the user with a suitable
message.
What is the value of $num if the insert operation completes successfully?
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
user.
15. Save and close the index.php file.
16. Open the index.php page in the Firefox browser using the following URL:
http://localhost/php/activity3-4/index.php.
17. Verify that you receive the following messages in the webpage:
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
The application connects to the MySQL server’s world database and displays the contents
of the GelderlandDist table. It then inserts a new row into the table, reports the number
of rows affected, and then displays the new contents of the table. Finally, the application
disconnects from the MySQL server.
18. Close the Firefox browser and any open terminal windows.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights
reserved.
ble
fe r a
Oracle is a registered trademark of Oracle Corporation and/or its
ans
affiliates. Other names may be trademarks of their respective
n - t r
no
owners.
a
s current input
Type 'help;' or '\h' for help. Type '\c' to clear the
h a
statement.
ฺ b r) deฺ
mysql>
c o m Gui
2. Create a new table in the world database p a rฺ GelderlandDist,
called e n t which consists of the
Name, District, and CountryCode e
on columns d
tufor all cities in the City table within the
Gelderland district. s s S
Enter the following statementsi c c i@
at thee t hi prompt and receive the results shown:
r t o ฺr us
mysql
e
mysql> USE world
b information t o
Readingotable
(
You i canr turn e n sethis feature for completion of table and column names
c c l i
iDatabase changedc off to get a quicker startup with -A
o R
b e rt mysql> CREATE TABLE GelderlandDist AS
R o -> SELECT Name, District, CountryCode
-> FROM City
-> WHERE District = 'Gelderland';
Query OK, 4 rows affected (#.## sec)
Records: 4 Duplicates: 0 Warnings: 0
The statement creates the GelderlandDist table with four rows.
3. Exit the mysql command line client.
Enter the following command at the mysql prompt and receive the results shown:
mysql> EXIT
Bye
#
4. Change the current working directory to /labs/php/web/activity3-4 and list the
contents of the directory.
Enter the following command at the Linux terminal prompt and receive the results shown:
# cd /labs/php/web/activity3-4
# ls
conn.php index_start.php index_soln.php
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
try {
# Create the DSN
$dsn = "mysql:host=localhost;dbname=world";
R i</head>
<title>Activity
ert o <body>
b
Ro
<h1>Activity 3-4 (PHP)</h1>
<p>
<?php
require_once "conn.php";
?>
</p>
</body>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
</html>
8. Under the comment labelled “A. Define the query”, populate the variable $sql1 with the
SQL statement that retrieves all rows from the GelderlandDist table.
Enter the following line of code under the comment labelled “A. Define the query”:
...
<?php
require_once "conn.php";
ble
# A. Define the query
fe r a
ns
$sql1 = "SELECT * FROM GelderlandDist";
...
t r a
-the end of
Note: The usual semicolon SQL statement terminator is not required, because o n
the string serves as the statement terminator.
s an
9. Under the comment labelled “B. Execute the query”, call the appropriate
) h a method on the
$dbh connection object to execute the query using the SQL ฺ b rstatement
d e ฺin $sql1. Store the
returned statement handle in a variable called $sth. om t Gu i
r ฺ c
a labelled
Enter the following line of code under the comment n “B. Execute the query”:
e p d e
...
# B. Execute the query s
on s Stu
$sth = $dbh->query($sql1);
c c i@ thi
...
o ฺ r i u se
e t
r labelledt“C. o Display the returned rows”, create a while loop that
b
10. Under the comment e
(rofromcthe
returns ai row
c e s set as an associative array of column values by using the
nresult
ic list oflicolumn
$sth->fetch()
R method. Display the contents of the row to the user as a comma-
return value in a variable called $num and display this value to the user with a suitable
message.
Enter the following lines of code under the comment labelled “E. Execute the insert”:
...
echo("<p>Inserting new row into table</p>");
# E. Execute the insert
$num = $dbh->exec($sql2);
printf("%s row(s) inserted", $num);
ble
...
fe r a
What is the value of $num if the insert operation completes successfully?
ans
Answer: 1. The exec() function returns the number of rows affected by the operation.
n - t r
no
13. Under the comment labelled “F. Execute the first query again and display the returned
a
h a s
rows”, write PHP code to display the contents of the GelderlandDist table. This time,
ฺ b r) deฺ
retrieve the column values in each row using the position of the columns in the row array
instead of the column names. For example, use $row[0] instead of $row["Name"].
c o m Gui
query again and display the returned rows”:p a rฺ ent
Enter the following lines of PHP code under the comment labelled “F. Execute the first
t o ฺ r
# F. Execute the first
u s display the returned rows
r= $sth->fetch
$sth = $dbh->query($sql1);
b e to (PDO::FETCH_NUM)) {
while ($row
(ro cen%s, e
s %s<br/>", $row[0], $row[1], $row[2]);
c c i
printf("%s,
li
Ri...
}
e r
14.
toUnder the comment labelled “G. Close the connection to the database server”, write PHP
R ob code to close the connection to the MySQL server and display a suitable message to the
user.
Enter the following lines of PHP code under the comment labelled “G. Close the connection
to the database server”:
...
# G. Close the connection to the database server
$dbh = NULL;
echo("<p>Disconnected from the database server</p>");
?>
</p>
</body>
</html>
15. Save and close the index.php file.
16. Open the index.php page in the Firefox browser using the following URL:
http://localhost/php/activity3-4/index.php.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
The application connects to the MySQL server’s world database and displays the contents
of the GelderlandDist table. It then inserts a new row into the table, reports the number
of rows affected, and then displays the new contents of the table. Finally, the application
disconnects from the MySQL server.
18. Close the Firefox browser and any open terminal windows.
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• Mozilla Firefox is open.
• The Tomcat server is running.
Duration
This practice should take you approximately 20 minutes to complete.
ble
Tasks fe r a
ans
1. Log in to the mysql command line client as the root user (password: oracle).
n - t r
2. Create a new table in the world database called GelderlandDist, which consists of the o
an
Name, District, and CountryCode columns for all cities in the City table within the
s
Gelderland district.
r ) ha ฺ
3. Exit the mysql command line client.
m ฺb uide
4.
a r ฺco nt G
Change the current working directory to /labs/java/web/activity3-5 and list the
contents of the directory by using the tree command.
5. o n ep tude
Examine the JSTL code in the WEB-INF/conn.inc file in your chosen text editor.
@ s is S
6. i th
Copy the index_start.jsp file to index.jsp.
c c
r i e
toฺ to us
7. Examine the contents of the index.jsp file in your chosen text editor.
8. e r
Under the comment labelled “A. Define the query”, use a <sql:query> tag to define a
( rob ense
query that selects all rows from the GelderlandDist table into a variable called rs.
i
c lic
9.
to Ric
Under the comment labelled “B. Display query results”, use a <c:forEach> tag to iterate
through the collection of rows stored in the variable rs. Retrieve each row by calling the rs
e r collection’s rowsByIndex() method. Display each row as a comma-separated list of
R ob column values.
10. Under the comment labelled “C. Define the update”, use a <sql:update> tag to define a
SQL statement that inserts a new row into the GelderlandDist table with the following
column values:
• Name: Atlantis
• District: Gelderland
• CountryCode: NLD
Specify a variable within the <sql:update> tag to store the return value of the operation,
which is the number of rows affected.
11. Under the comment labelled “D. Report the number of rows affected by the update”, display
the return value of the update operation.
12. Under the comment labelled “E. Requery the database”, reissue the query that lists all the
rows in the GelderlandDist table.
13. Save and close the index.jsp file.
14. Open the index.jsp page in the Firefox browser using the following URL:
http://localhost:8080/activity3-5/index.jsp.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
15. Close the Firefox browser and any open terminal windows.
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights
reserved.
ble
fe r a
Oracle is a registered trademark of Oracle Corporation and/or its
ans
affiliates. Other names may be trademarks of their respective
n - t r
no
owners.
a
s current input
Type 'help;' or '\h' for help. Type '\c' to clear the
h a
statement.
ฺ b r) deฺ
mysql>
c o m Gui
2. Create a new table in the world database p a rฺ GelderlandDist,
called e n t which consists of the
Name, District, and CountryCode e
on columns d
tufor all cities in the City table within the
Gelderland district. s s S
Enter the following statementsi c c i@
at thee t hi prompt and receive the results shown:
r t o ฺr us
mysql
e
mysql> USE world
b information t o
Readingotable
(
You i canr turn e n sethis feature for completion of table and column names
c c l i
iDatabase changedc off to get a quicker startup with -A
o R
b e rt mysql> CREATE TABLE GelderlandDist AS
R o -> SELECT Name, District, CountryCode
-> FROM City
-> WHERE District = 'Gelderland';
Query OK, 4 rows affected (#.## sec)
Records: 4 Duplicates: 0 Warnings: 0
The statement creates the GelderlandDist table with four rows.
3. Exit the mysql command line client.
Enter the following command at the mysql prompt and receive the results shown:
mysql> EXIT
Bye
#
4. Change the current working directory to /labs/java/web/activity3-5 and list the
contents of the directory by using the tree command.
Enter the following command at the Linux terminal prompt and receive the results shown:
# cd /labs/java/web/activity3-5
# tree
.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
3 directories, 3 files
5. Examine the JSTL code in the WEB-INF/conn.inc file in your chosen text editor.
The contents of the WEB-INF/conn.inc file are as follows:
<c:catch var="connexception">
<sql:setDataSource
var="conn"
driver="com.mysql.jdbc.Driver"
ble
url="jdbc:mysql://localhost/world"
user="root" fe r a
password="oracle"
ans
/>
n - t r
</c:catch>
a no
The conn.inc file creates a connection to the world database onsthe MySQL server. This
file is identical to the conn.inc file you created in the previous) a activity.
hJava
ฺ b r d e ฺ
6. Copy the index_start.jsp file to index.jsp. m Gu
oprompt i
ฺ c
a dent and receive the results shown:
Enter the following commands at the Linux terminalr
# cp index_start.jsp index.jspep
# ls s on s Stu
index.jsp index_soln.jsp
i c c e t hi
i@ index_start.jsp WEB-INF
7. Examine the contentsoofฺrthe index.jsp us file in your chosen text editor.
e r t
The contents ofbthe index.jsp t o
( r o n s e file are as follows:
<%@ i taglib uri="http://java.sun.com/jsp/jstl/core"
e
i c c tagliblicuri="http://java.sun.com/jsp/jstl/sql" prefix="c" %>
be r
Ro <!DOCTYPE html>
<html>
<head>
<title>Activity 3-5 (Java)</title>
</head>
<body>
<h1>Activity 3-5 (Java)</h1>
<c:choose>
<%-- Unsuccessful connection attempt --%>
<c:when test="${not empty connexception}">
<p>Error connecting to the database</p>
</c:when>
<%-- Successful connection attempt --%>
<c:otherwise>
<p>Connected to the database</p>
</c:otherwise>
</c:choose>
<br/>
c ( A.ceDefine
i <%-- n the query --%>
R c l i
i <sql:query dataSource="${conn}" var="rs">
ert o SELECT * FROM GelderlandDist
b
Ro
</sql:query>
...
Notes:
− The JSTL <sql:query> tag provides a convenient tag-based equivalent for the
Connector/J Statement.executeQuery() method.
− The usual semicolon SQL statement terminator is not required because the end of
the tag serves as the statement terminator.
9. Under the comment labelled “B. Display query results”, use a <c:forEach> tag to iterate
through the collection of rows stored in the variable rs. Retrieve each row by calling the rs
collection’s rowsByIndex() method. Display each row as a comma-separated list of
column values.
</c:forEach>
...
10. Under the comment labelled “C. Define the update”, use a <sql:update> tag to define a
SQL statement that inserts a new row into the GelderlandDist table with the following
column values:
• Name: Atlantis
• District: Gelderland
• CountryCode: NLD
ble
Specify a variable within the <sql:update> tag to store the return value of the operation,
fe r a
which is the number of rows affected.
ans
Enter the following lines of JSTL markup under the comment labelled “C. Define the n - t r
update”:
a no
...
h a s
<%-- C. Define the update --%>
ฺ b r) deฺ
<sql:update dataSource="${conn}" var="num">
o mDistrict,u i CountryCode)
INSERT INTO GelderlandDist (Name,
VALUES ('Atlantis', 'Gelderland', a c
rฺ e'NLD')
n t G
p
one s Stud
</sql:update>
... s
Note: The JSTL <sql:update>
i c c e t hi a convenient tag-based equivalent for the
i@tag provides
r t o ฺr
Connector/J Statement.executeUpdate() us method.
11. Under the comment e
bof thelabelled t o
e “D.operation.
Report the number of rows affected by the update”, display
the return ( r o
value n s
update
i
Enter c ci l
the following i celine of JSTL markup under the comment labelled “D. Report the number
t o R
of rows affected by the update”:
be r
Ro
...
<%-- D. Report the number of rows affected by the update --%>
<c:out value="${num} row(s) affected"/>
<br/>
...
12. Under the comment labelled “E. Requery the database”, reissue the query that lists all the
rows in the GelderlandDist table.
Enter the following lines of JSTL markup under the comment labelled “E. Requery the
database”:
...
<p>Contents of the GelderlandDist table:</p>
http://localhost:8080/activity3-5/index.jsp.
Verify that you receive the following messages in the webpage:
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
15. Close the Firefox browser and any open terminal windows.
Assumptions
• You are logged in as the Linux root user at a terminal prompt.
• Mozilla Firefox is open.
• The Apache server is running.
Duration
ble
This practice should take you approximately 20 minutes to complete.
fe r a
ans
Tasks
n - t r
1. Log in to the mysql command line client as the root user (password: oracle). o
2. s an
Create a new table in the world database called GelderlandDist, which consists of the
r ) ha ฺ
Name, District, and CountryCode columns for all cities in the City table within the
Gelderland district.
m ฺb uide
3. Exit the mysql command line client.
a r ฺco nt G
contents of the directory. o n ep tude
4. Change the current working directory to /labs/python/web/activity3-6 and list the
@ s is S
i th
5. Examine the Python code in the conn.py file in your chosen text editor.
c c
r i e
e r toฺ to us
6. Copy the index_start.py file to index.py.
7. Examine the contents of the index.py file in your chosen text editor.
( rob ense
8. Under the comment labelled “A. Define the query, create a variable called sql1 which
i
c lic
to Ric
contains a SQL statement that retrieves all rows from the GelderlandDist table.
9. Under the comment labelled “B. Execute the query”, call the appropriate method on the
e r
R ob dbconn connection object that returns a cursor to allow you to work with the query results.
Store the returned cursor object in a variable called cursor. Then call the
cursor.execute() method, passing in the SQL statement you created in the previous
step.
10. Under the comment labelled “C. Display query results”, create a for loop that uses the
cursor object to retrieve rows from the GelderlandDist table. Display the contents of
each row to the user as a comma-separated list of column values. Call the appropriate
method on the cursor object to close the cursor when you have retrieved all the rows.
11. Under the comment labelled “D. Define the insert”, create a variable called sql2 which
contains a SQL statement that inserts a new row into the GelderlandDist table with the
following column values:
• Name: Atlantis
• District: Gelderland
• CountryCode: NLD
titled “Transactions.”)
• Use the cursor.rowcount property to display the number of rows affected by the
insert operation to the user.
What is the value of cursor.rowcount when the insert operation completes
successfully?
13. Under the comment labelled “F. Execute the first query again and display the returned
rows”, write Python code to display the contents of the GelderlandDist table. Close the
cursor object you created in the previous step when you have displayed all the rows. ble
14. Make the index.py and conn.py files executable. fe r a
ans
15. Save and close the index.py file.
n - t r
16. Open the index.py page in the Firefox browser using the following URL: o
http://localhost/cgi-bin/activity3-6/index.py.
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
18. Close the Firefox browser and any open terminal windows.
Enter the following command at the Linux terminal prompt and receive the results shown:
# mysql -uroot -poracle
mysql: [Warning] Using a password on the command line interface can be
insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 28
Server version: 5.7.7-rc-enterprise-commercial-advanced MySQL
Enterprise Server - Advanced Edition (Commercial)
ble
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights
reserved. fe r a
ans
Oracle is a registered trademark of Oracle Corporation and/or its
n - t r
affiliates. Other names may be trademarks of their respective
a no
owners.
h a s
Type 'help;' or '\h' for help. Type '\c' to clear
ฺ b r) dtheeฺcurrent input
statement.
c o m Gui
mysql>
p a rฺ ent
2. Create a new table in the world database
s one called
S ud
tGelderlandDist, which consists of the
Name, District, and CountryCode
c i @ t h i s
columns for all cities in the City table within the
Gelderland district. i c
ฺr atuthe e
s mysql prompt and receive the results shown:
e t o
Enter the following rstatements t o
mysql>ro b worldse
c i ( USE
c e ninformation for completion of table and column names
R c
Reading
l i
table
iYou can turn off this feature to get a quicker startup with -A
ert o
b
Ro
Database changed
mysql> CREATE TABLE GelderlandDist AS
-> SELECT Name, District, CountryCode
-> FROM City
-> WHERE District = 'Gelderland';
Query OK, 4 rows affected (#.## sec)
Records: 4 Duplicates: 0 Warnings: 0
The statement creates the GelderlandDist table with four rows.
3. Exit the mysql command line client.
Enter the following command at the mysql prompt and receive the results shown:
mysql> EXIT
Bye
#
5. Examine the Python code in the conn.py file in your chosen text editor.
The contents of the conn.py file are as follows:
# conn.py: library file for connecting to MySQL using the
# Connector/Python module
import mysql.connector
conn = None
ble
conn_params = { fe r a
"database": "world",
ans
"host": "localhost",
n - t r
"user": "root",
"password": "oracle" a no
}
h a s
ฺ b r) deฺ
def dbconnect():
c o m Gui
try:
global conn
p a rฺ ent
ne Stud
conn = mysql.connector.connect(**conn_params)
print('''<p>Connectedso
@ this
to the database</p>''')
return conn
c i
except:
t o ฺ ric not u seconnect to the database</p>''')
r
print('''<p>Could
b e to
This code isro
( the same e
nass youfile
used in the previous activity.
c i li c e
6. Copy
R ic the index_start.py to index.py.
e r toEnter the following commands at the Linux terminal prompt and receive the results shown:
R ob # cp index_start.py index.py
# ls
conn.py index.py index_soln.py index_start.py
7. Examine the contents of the index.py file in your chosen text editor.
The contents of the index.py file are as follows:
#!/usr/bin/python
import conn
print('''Content-Type: text/html
<html>
<head><title>Activity 3-6 (Python)</title></head>
<body>
''')
method on the cursor object to close the cursor when you have retrieved all the rows.
Enter the following lines of code under the comment labelled “C. Display query results”:
...
# C. Display query results
for (name, district, countrycode) in cursor:
print("%s, %s, %s<br />" % (name, district, countrycode))
cursor.close()
...
11. Under the comment labelled “D. Define the insert”, create a variable called sql2 which ble
contains a SQL statement that inserts a new row into the GelderlandDist table with the fe r a
following column values: ans
• Name: Atlantis n - t r
• District: Gelderland a no
• CountryCode: NLD h a s
ฺ b r) deฺ
c o m Gui
Enter the following line of code under the comment labelled “D. Define the update”:
...
p a rฺ ent
ne Stud
# D. Define the insert
sql2 = '''
s o
c i @
INSERT INTO GelderlandDist (Name,
t h is District, CountryCode)
ric use
VALUES ('Atlantis', 'Gelderland', 'NLD')
'''
r t o ฺ
...
b e e to
12. Under the
i ro elabelled
(comment ns “E. Execute the insert”:
c
c a new
• iCreate
c
li cursor from the dbconn connection object. Call the
R
berto cursor.execute() method, passing in the sql2 SQL statement you created in the
previous step.
Ro • Call dbconn.commit() to persist the change. (This is important, because
Connector/Python does not support autocommit, which is discussed in the lesson
titled “Transactions.”)
• Use the cursor.rowcount property to display the number of rows affected by the
insert operation to the user.
Enter the following lines of code under the comment labelled “E. Execute the insert”:
...
print("<p>Inserting new row into table</p>")
Enter the following lines of Python code under the comment labelled “F. Execute the first
query again and display the returned rows”:
...
print("<p>Contents of GelderlandDist table:</p>")
# F. Execute the first query again and display the returned rows
cursor.execute(sql1)
for (name, district, countrycode) in cursor:
print("%s, %s, %s<br />" % (name, district, countrycode))
ble
cursor.close()
fe r a
...
ans
14. Make the index.py and conn.py files executable.
n - t r
o
an
Enter the following commands at the Linux terminal prompt and receive the results shown:
s
# chmod +x index.py conn.py
# ls -al
r ) ha ฺ
total 24
m ฺb uide
drwxr-xr-x 2 root root 4096 May 11 16:37
drwxr-xr-x. 4 root root 4096 May a
co ...t G
11rฺ16:25 n
-rwxr-xr-x 1 root root 453 May
o n ep 11 u de conn.py
16:26
t
-rwxr-xr-x 1 root root 1241
@ s Mayis11S16:37 index_soln.py
May 11 16:36 index.py
-rwxr-xr-x 1 root root
r i c ci 6791241
e h 11 16:34 index_start.py
tMay
toฺ tofile. us
-rw-r--r-- 1 root root
15. Save and close thee rindex.py
( rob enpage
16. Open the index.py
i sein the Firefox browser using the following URL:
icc lic
http://localhost/cgi-bin/activity3-6/index.py.
R
berto
Ro
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
18. Close the Firefox browser and any open terminal windows.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 4:
Prepared u i
om Statements
r ฺ c
a 4 den t G
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Important: The coding practices in this lesson are available in PHP, Java, and Python. Perform
all the practices for this and all future coding activities in only one language, choosing the
language you are most familiar with. If in doubt, perform the PHP activities. Ensure that you
complete all practices that are not language-specific
For this lesson, perform the following practices:
• Practice 4-1: Preparing Statements in MySQL
• If your chosen programming language is PHP: ble
− Practice 4-2: Using Prepared Statements with PHP/PDO fe r a
ans
• If your chosen programming language is Java:
n - t r
− Practice 4-3: Using Prepared Statements with Connector/J o
• If your chosen programming language is Python: s an
) ha ฺ
− Practice 4-4: Using Prepared Statements with Connector/Python
r
m ฺb uide
Assumptions
a r ฺ co nt G
• If you are completing the coding practices
o n e p in PHP:
t u de
− The Apache web server is running on S
port 80
i @s practices
• If you are completing theccoding t h i s in Java:
r i c e
− The Tomcat tapplication
r us is running on port 8080
oฺ to server
• If you areo b e
completing ethe coding practices in Python:
( r
i Apache n s
i− cThe
c l i ceweb server is installed and running on port 80.
•R The sakila sample database is installed
r t o
R obe • The film2.sql file is available in the /labs/sql directory
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Use the mysql command-line client to connect to the MySQL server as the root user
(password: oracle).
2. Create a prepared statement called mystmt. The statement selects the title from the film
table in the sakila database for a given film_id, which you provide in a variable.
ble
3. Create a variable called fid and set its value to 803.
fe r a
4. Execute the prepared statement with the variable created in the preceding step.
ans
5. Assign 380 to the fid variable.
n - t r
o
6.
7. s an
Re-execute the prepared statement with the new variable value.
Issue a statement which explicitly removes the prepared statement.
8. r ) ha ฺ
Keep the mysql command-line client open for the next practice.
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# mysql –u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
2. Create a prepared statement called mystmt. The statement selects the title from the film
table in the sakila database for a given film_id, which you provide in a variable.
Enter the following statement at the mysql prompt and receive the results shown: ble
fe r a
mysql> USE sakila
ans
Reading table information
... n - t r
Database changed
a no
mysql> PREPARE mystmt FROM
-> 'SELECT title FROM film WHERE film_id=?'; h a s
Query OK, 0 rows affected (#.## sec)
ฺ b r) deฺ
Statement prepared
c o m Gui
rฺ803. ent
3. Create a variable called fid and set its valueato
p
ne prompt
Enter the following statement at the mysql
o d receive the results shown:
tuand
mysql> SET @fid = 803;@
s S
issec)
c i t h
t o ฺ ric use
Query OK, 0 rows affected (#.##
4. Execute the prepared
b e r statement
to with the variable created in the preceding step.
(ro cestatement
Enter the following e
ns at the mysql prompt and receive the results shown:
c c i li mystmt USING @fid;
R imysql> EXECUTE
erto | title
+------------------+
|
b
Ro +------------------+
| SLACKER LIAISONS |
+------------------+
1 row in set (#.## sec)
5. Assign 380 to the fid variable.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SET @fid = 380;
Query OK, 0 rows affected (#.## sec)
6. Re-execute the prepared statement with the new variable value.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> EXECUTE mystmt USING @fid;
+----------------+
| title |
+----------------+
| GREEK EVERYONE |
+----------------+
1 row in set (#.## sec)
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
• You have a Linux terminal window open logged in as the root user (password:
oracle).
• The Apache web server is installed and running on port 80.
Duration
This practice should take you approximately 30 minutes to complete. ble
fe r a
Tasks
t r a ns
1. Display the contents of the /labs/sql/film2.sql file in the Linux terminal.nWhat
o - does
the SQL code in this file do?
2. Execute the /labs/sql/film2.sql script to create the film2a s ainnthe sakila
table
database. r ) h ฺ
3. Verify that the film2 table exists and contains 1,000m
b
ฺ uide
records.
r
4. Change the current working directory to /labs/php/web/activity4-2
o
ฺc nt G and list the files
within that directory. p
e tuda e
5. Copy the addfilm_start.php file s otonaddfilm.php.
s S
i @ h i
6. Copy the findfilm_start.php
ฺ r i cc sfileetot findfilm.php
7. Examine the contents r o u
toof the tindex.html file in a text editor such as vi, emacs, or gedit.
e
rob Clicking
The index.html
field andi a(button.
file
e n s e
displays
the
a simple
button for
webpage
the “New
with two forms. Each form contains a text
Film” text box invokes the
i c c l i c
script, and clicking the button for the “Search Films” text box invokes the
o R
addfilm.php
ert
findfilm.php script.
b
Ro
8. Examine the contents of the addfilm.php file in your chosen text editor. You complete
the code in the addfilm.php file to insert a new row into the films2 table.
9. Examine the contents of the findfilm.php file in your chosen text editor. The PHP code
in the findfilm.php file enables a user to search for a film by its title.
10. In the addfilm.php file, enter code under the comment labelled “A. Prepare the insert
statement” to prepare a statement that inserts a new row into the films2 table. Use
named placeholders for the title and description columns.
11. In the addfilm.php file, enter code under the comment labelled “B. Bind parameters”, and
enter code to bind local variables to the named parameters in the SQL statement.
12. In the addfilm.php file, enter code under the comment labelled “C. Execute the insert
statement” to execute the prepared statement you created in the preceding step. The
execute() method must pass an array of column values. Populate the title column
with the name of the new film title supplied by the user. For the description column,
enter any text string.
13. Save your work, open the Firefox browser and visit the following URL:
http://localhost/php/activity4-2/index.html.
validation of the value the user enters. This makes it vulnerable to SQL injection attacks by
malicious users. Enter the following expression exactly as shown into the “Search Films”
text box and click its associated “Submit Query” button. What happens, and why?
' OR 'X' = 'X
17. In the findfilm.php script, comment out the existing query.
18. Under the comment labelled “D. Replace the query with a prepared statement”, rewrite the
query as a prepared statement.
19. Save the findfilm.php file and reload the index.html page in Firefox. Enter the ble
following expression exactly as shown into the “Search Films” text box. Click its associated fe r a
“Submit Query” button. What happens this time, and why? ans
n - t r
' OR 'X' = 'X
o
20. Close Firefox and any open terminal windows. s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# cd /labs/sql/
# cat film2.sql
USE sakila
DROP TABLE IF EXISTS `film2`;
CREATE TABLE `film2` AS SELECT `film_id`, `title`, `description` FROM
`film`;
ALTER TABLE `film2` MODIFY `film_id` SMALLINT(5) UNSIGNED NOT NULL
PRIMARY KEY AUTO_INCREMENT;
ble
Answer: The SQL in film2.sql creates a copy of the film table in the sakila
fe r a
database called film2, preserving only the film_id, title and description columns.
ans
The ALTER TABLE statement adds the auto_increment and primary key attributes to
the film_id column. n - t r
2. a no
Execute the /labs/sql/film2.sql script to create the film2 table in the sakila
database. h a s
ฺ b r) deฺ
Enter the following command at the Linux terminal prompt and receive the results shown:
# mysql -uroot -poracle < film2.sql com u i
mysql: [Warning] Using a passwordaon n G
rฺ the ecommand
t line interface can be
p
one s Stud
insecure.
# s
i c
3. Verify that the film2 tablecexists
e hi 1,000 records.
i@and contains
t
t o ฺr at the
Enter the following rcommand usLinux terminal prompt and receive the results shown:
e t o
i ( r b en-poracle
# mysqlo-uroot se\ \
i>ccSHOW CREATE
lic TABLE film2\G \
> -e "USE sakila;
R
erto mysql: [Warning] Using a password on the command line interface can be
> SELECT COUNT(*) FROM film2;"
b
Ro insecure.
*************************** 1. row ***************************
Table: film2
Create Table: CREATE TABLE `film2` (
`film_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
`description` text CHARACTER SET utf8,
PRIMARY KEY (`film_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=latin1
+----------+
| COUNT(*) |
+----------+
| 1000 |
+----------+
[root@edddr9p1 sql]#
4. Change the current working directory to /labs/php/web/activity4-2 and list the files
within that directory.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/php/web/activity4-2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
# ls
addfilm.php addfilm_start.php findfilm_soln.php index.html
addfilm_soln.php conn.php findfilm_start.php
6. Copy the findfilm_start.php file to findfilm.php
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp findfilm_start.php findfilm.php
# ls
addfilm.php addfilm_start.php findfilm.php
ble
findfilm_start.php
fe r a
addfilm_soln.php conn.php findfilm_soln.php index.html
an s
7. Examine the contents of the index.html file in a text editor such as vi, emacs, or gedit.
n - t r
o
an
The index.html file displays a simple web page with two forms. Each form contains a text
field and a button. Clicking the button for the “New Film” text box invokes the
s
r ) ha ฺ
addfilm.php script, and clicking the button for the “Search Films” text box invokes the
findfilm.php script.
m ฺb uide
a r ฺ co nt G
The index.html file contains the following HTML markup:
<!doctype html>
<html>
o n ep tude
<head>
@ s is S
c i
<title>Activity 4-2 (PHP)</title>
c th
</head>
r i
oฺ (PHP)</h1>us e
<body>
e r t4-2 t o
rob ense
<h1>Activity
<p>
i (
cc
Ri<br/><br/>lic
e r to <form action="addfilm.php" method="post">
<br/><br/>
</body>
</html>
8. Examine the contents of the addfilm.php file in your chosen text editor. You complete
the code in the addfilm.php file to insert a new row into the films2 table.
The addfilm.php file contains the following PHP code:
<?php
require_once "conn.php";
# B. Bind Parameters
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
?>
9. Examine the contents of the findfilm.php file in your chosen text editor. The PHP code
in the findfilm.php file enables a user to search for a film by its title.
The findfilm.php file contains the following PHP code: ble
fe r a
<?php
ans
require_once "conn.php";
n - t r
a no
# Retrieve search text submitted by the user h a s
$title = $_POST["name"];
ฺ b r) deฺ
o m WHERE u i
$sql = "SELECT title, description FROM
a r ฺ c film2
n t G title = '" . $title
. "'";
$sth = $dbh->query($sql);
o n ep tude
@ s is S
# D. Replace the query
c c i with athprepared statement
r i e
e r toฺ to us
i ( rob ense $sql);
printf("<p>%s</p>",
c c
echo
l i c
"<p><strong>Search results:</strong></p>";
to Ri printf("%s<br/>", $row[0]
while ($row = $sth->fetch (PDO::FETCH_NUM)) {
. ": " . $row[1]);
e r
ob
}
R ?>
10. In the addfilm.php file, enter code under the comment labelled “A. Prepare the insert
statement” to prepare a statement that inserts a new row into the films2 table. Use
named placeholders for the title and description columns.
Enter the following PHP code in the addfilm.php file under the comment labelled “A.
Prepare the insert statement”:
<?php
require_once "conn.php";
# B. Bind parameters
?>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
11. In the addfilm.php file, enter code under the comment labelled “B. Bind parameters”, and
enter code to bind local variables to the named parameters in the SQL statement.
Enter the following PHP code in the addfilm.php file under the comment labelled “B.
Bind parameters”:
...
# B. Bind Parameters
$psh->bindParam(':title', $title);
$psh->bindParam(':description', $description);
ble
...
fe r a
12. In the addfilm.php file, enter code under the comment labelled “C. Execute the insert
ans
statement” to execute the prepared statement you created in the preceding step. The
n - t r
o
an
execute() method must pass an array of column values. Populate the title column
i ( rob e=n"Temporary
$description se description";
cc lic
$psh->execute();
Ri...
e r
13.toSave your work, open the Firefox browser and visit the following URL:
R ob http://localhost/php/activity4-2/index.html.
The browser displays the following web page:
15. Verify that the row has been added to the table. Navigate back to the main page of your
application by clicking the Firefox browser’s “back” button. Enter “ANOTHER TRIP” into the
ble
“Search Films” text box and click its associated “Submit Query” button.
fe r a
The browser redirects to the findfilm.php script to display the new row in the film2
ans
table and the SQL statement that it used to locate the row.
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
16. The c in thelifindfilm.php script is not a prepared statement and there is no
Riquery
e r tovalidation of the value the user enters. This makes it vulnerable to SQL injection attacks by
R ob malicious users. Enter the following expression exactly as shown into the “Search Films”
text box and click its associated “Submit Query” button. What happens, and why?
' OR 'X' = 'X
Answer: The findfilm.php script displays the entire contents of the film2 table. This is
because the actual query generated by the script is as follows:
SELECT title, description FROM film2 WHERE title = '' OR 'X' = 'X'
The initial single quote you entered terminated the first search string, allowing you to
append an OR clause to the SQL expression. Because 'X' always equals 'X', the expression
evaluates to true for every record in the film2 table. This could potentially be a security
risk, especially if the table contains sensitive information.
17. In the findfilm.php script, comment out the existing query.
Comment out the following lines in the findfilm.php script:
<?php
require_once "conn.php";
printf("<p>%s</p>", $sql);
echo "<p><strong>Search results:</strong></p>";
while ($row = $sth->fetch (PDO::FETCH_NUM)) {
printf("%s<br/>", $row[0] . ": " . $row[1]);
}
?>
18. Under the comment labelled “D. Replace the query with a prepared statement”, rewrite the
query as a prepared statement.
ble
Enter the following PHP code under the comment labelled “D. Replace the query with a
fe r a
prepared statement.”
ans
... n - t r
# D. Replace the query with a prepared statement
a no
h a s
$sql = "SELECT title, description FROM film2 WHERE title = :title";
r) deฺ
$sth = $dbh->prepare($sql);
$sth->bindParam(':title', $title);
ฺ b
m Gui
$sth->execute();
c o
rฺ ent
p a
ne Stud
printf("<p>%s</p>", $sql);
o
echo "<p><strong>Search results:</strong></p>";
s
@ t.hi":s " . $row[1]);
while ($row = $sth->fetch (PDO::FETCH_NUM))
printf("%s<br/>",ci$row[0]
{
}
t o ฺ ric use
?>
b e r to
e
(ro cens file and reload the index.html page in Firefox. Enter the
19. Save the findfilm.php
c i li exactly as shown into the “Search Films” text box. Click its associated
R ic Query”
following expression
erto
“Submit button. What happens this time, and why?
b
Ro
' OR 'X' = 'X
Answer: The server is unable to process the query. SQL injection is a risk because user
input is used as part of the SQL statement. By using prepared statements you can force the
user input to be handled as the content of a parameter and not as a part of the SQL
statement.
20. Close Firefox and any open terminal windows.
Assumptions
• You have a Linux terminal window open logged in as the root user (password:
oracle).
• The Tomcat application server is installed and running on port 8080.
Duration
This practice should take you approximately 30 minutes to complete. ble
fe r a
Tasks
t r a ns
1. Display the contents of the /labs/sql/film2.sql file in the Linux terminal.nWhat
o - does
the SQL code in this file do?
2. Execute the /labs/sql/film2.sql script to create the film2a s ainnthe sakila
table
database. r ) h ฺ
3. Verify that the film2 table exists and contains 1,000m
b
ฺ uide
records.
r o
ฺc nt G
4. Change the current working directory to /labs/java/web/activity4-3 and list the files
within that directory and its subdirectories. p
e tuda e
5. Copy the addfilm_start.jsp file s otonaddfilm.jsp.
s S
i @ h i
6. Copy the findfilm_start.jsp
ฺ r i cc sfileetot findfilm.jsp.
7. Examine the contents r o u
toof the tindex.html file in a text editor such as vi, emacs, or gedit.
e
rob Clicking
The index.html
field andi a(button.
file
e n s e
displays
the
a simple
button for
web
the
page with two forms. Each form contains a text
“New Film” text box invokes the
i c c l i c
page, and clicking the button for the “Search Films” text box invokes the
o R
addfilm.jsp
ert
findfilm.jsp page.
b
Ro
8. Examine the contents of the addfilm.jsp file in your chosen text editor. You complete
the code in the addfilm.jsp file to insert a new row into the films2 table.
9. Examine the contents of the findfilm.jsp file in your chosen text editor. The code in the
findfilm.jsp file enables a user to search for a film by its title.
10. In the addfilm.jsp file, enter a <sql:update> tag under the comment labelled “A.
Prepare and execute the insert statement” to prepare and execute a statement that inserts
a new row into the films2 table. Use placeholders for the title and description
columns, and supply the actual values to the statement by using <sql:param> tags.
Populate the title column with the name of the new film title supplied by the user. For the
description column, supply any text string.
11. Save your work, open the Firefox browser and visit the following URL:
http://localhost:8080/activity4-3/index.html.
12. Enter “ANOTHER TRIP” into the “New Film” text box and click its associated “Submit
Query” button.
13. Verify that the row has been added to the table. Navigate back to the main page of your
application by clicking the Firefox browser’s “back” button. Enter “ANOTHER TRIP” into the
“Search Films” text box and click its associated “Submit Query” button.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# cd /labs/sql/
# cat film2.sql
USE sakila
DROP TABLE IF EXISTS `film2`;
CREATE TABLE `film2` AS SELECT `film_id`, `title`, `description` FROM
`film`;
ALTER TABLE `film2` MODIFY `film_id` SMALLINT(5) UNSIGNED NOT NULL
PRIMARY KEY AUTO_INCREMENT;
ble
Answer: The SQL in film2.sql creates a copy of the film table in the sakila
fe r a
database called film2, preserving only the film_id, title, and description
ans
columns. The ALTER TABLE statement adds the auto_increment and primary key
attributes to the film_id column. n - t r
2. a no
Execute the /labs/sql/film2.sql script to create the film2 table in the sakila
database. h a s
ฺ b r) deฺ
Enter the following command at the Linux terminal prompt and receive the results shown:
# mysql -uroot -poracle < film2.sql com u i
mysql: [Warning] Using a passwordaon n G
rฺ the ecommand
t line interface can be
p
one s Stud
insecure.
# s
i c
3. Verify that the film2 tablecexists
e hi 1,000 records.
i@and contains
t
t o ฺr at the
Enter the following rcommand usLinux terminal prompt and receive the results shown:
e t o
i ( r b en-poracle
# mysqlo-uroot se\ \
i>ccSHOW CREATE
lic TABLE film2\G \
> -e "USE sakila;
R
erto mysql: [Warning] Using a password on the command line interface can be
> SELECT COUNT(*) FROM film2;"
b
Ro insecure.
*************************** 1. row ***************************
Table: film2
Create Table: CREATE TABLE `film2` (
`film_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
`description` text CHARACTER SET utf8,
PRIMARY KEY (`film_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=latin1
+----------+
| COUNT(*) |
+----------+
| 1000 |
+----------+
[root@edddr9p1 sql]#
4. Change the current working directory to /labs/java/web/activity4-3 and list the files
within that directory and its subdirectories.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/java/web/activity4-3
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
├── index.html
└── WEB-INF
├── classes
├── conn.inc
└── lib
3 directories, 6 files
5. Copy the addfilm_start.jsp file to addfilm.jsp.
Enter the following commands at the Linux terminal prompt and receive the results shown: ble
fe r a
# cp addfilm_start.jsp addfilm.jsp
# ls
t r a ns
addfilm.jsp addfilm_soln.jsp addfilm_start.jsp
o n -
findfilm_soln.jsp
findfilm_start.jsp index.html WEB-INF
s an
6. Copy the findfilm_start.jsp file to findfilm.jsp.
) h a
Enter the following commands at the Linux terminal prompt ฺ b r e
and receive
d ฺ the results shown:
# cp findfilm_start.jsp findfilm.jsp om u i
# ls r
a denฺ c t G
addfilm.jsp addfilm_soln.jsp ep
o n
s is S
findfilm_soln.jsp findfilm_start.jsp t u
addfilm_start.jsp
index.html
findfilm.jsp
WEB-INF
7. Examine the contents of the i @
cindex.html th file in a text editor such as vi, emacs, or gedit.
r i c e
s web page with two forms. Each form contains a text
field and a button.e t ฺ the
The index.html fileodisplays
r
Clicking t o
ausimple
button for the “New Film” text box invokes the
i
addfilm.jsp( robpage,e n seclicking the button for the “Search Films” text box invokes the
and
icc
findfilm.jsp
R
c
lipage.
e r toThe index.html file contains the following HTML markup:
R ob <!doctype html>
<html>
<head>
<title>Activity 4-3 (Java)</title>
</head>
<body>
<h1>Activity 4-3 (Java)</h1>
<p>
<br/><br/>
</body>
</html>
8. Examine the contents of the addfilm.jsp file in your chosen text editor. You complete
the code in the addfilm.jsp file to insert a new row into the films2 table.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
<c:choose>
<%-- Unsuccessful connection attempt --%>
<c:when test="${not empty connexception}">
<p>Error connecting to the database server</p>
ble
</c:when>
fe r a
<%-- Successful connection attempt --%>
ans
<c:otherwise>
<p>Connected to the database server</p>
n - t r
</c:otherwise>
a no
</c:choose>
h a s
<%--A. Prepare and execute the insert statement
ฺ b r) --%> d e ฺ
omadded u i
<p><c:out value="${param.name}"></c:out>
r ฺ c
afile in your t G to the database</p>
nchosen text editor. The code in the
9. Examine the contents of the findfilm.jsp e p d e
findfilm.jsp file enables a usersto
n for
osearch S taufilm by its title.
The findfilm.jsp file contains c t s
hi Java/JSTL code:
i@the following
ฺr i c e
r t o us
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
t o
<%@ taglibeuri="http://java.sun.com/jsp/jstl/sql"
prefix="c" %>
rob efile="/WEB-INF/conn.inc"
se
prefix="sql" %>
c i (
<%@ include
c n %>
R c
i<c:choose>li
berto <%-- Unsuccessful connection attempt --%>
Ro
<c:when test="${not empty connexception}">
<p>Error connecting to the database server</p>
</c:when>
<%-- Successful connection attempt --%>
<c:otherwise>
<p>Connected to the database server</p>
</c:otherwise>
</c:choose>
<%
String newFilm = request.getParameter("name");
String sql = "SELECT title, description FROM film2 WHERE title =
'" + newFilm + "'";
%>
Prepare and execute the insert statement” to prepare and execute a statement that inserts
a new row into the films2 table. Use placeholders for the title and description
columns and supply the actual values to the statement by using <sql:param> tags.
Populate the title column with the name of the new film title supplied by the user. For the
description column, supply any text string.
Add the following code to the addfilm.jsp file under the comment labelled “A. Prepare
and execute the insert statement”:
ble
...
<%--A. Prepare and execute the insert statement --%>
fe r a
<sql:update dataSource="${conn}" var="num">
ans
INSERT INTO film2 (title, description)
n - t r
VALUES (?,?)
<sql:param value="${param.name}" /> a no
<sql:param value="Temporary description" />
h a s
</sql:update>
ฺ b r) deฺ
...
o mfollowingu i
11. Save your work, open the Firefox browser and r
a ฺ c
visit the
n t G URL: http://localhost:8080/
activity4-3/index.html.
ep tude
onpage:
The browser displays the followings web
i s S
i @ h
ฺ r i cc se t
e r to to u
i ( rob ense
R icc lic
berto
Ro
13. Verify that the row has been added to the table. Navigate back to the main page of your
application by clicking the Firefox browser’s “back” button. Enter “ANOTHER TRIP” into the
ble
“Search Films” text box and click its associated “Submit Query” button.
fe r a
The browser redirects to the findfilm.jsp page to display the new row in the film2
ans
table and the SQL statement that it used to locate the row.
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
14. The c in thelifindfilm.jsp page is not a prepared statement and there is no
Riquery
e r tovalidation of the value the user enters. This makes it vulnerable to SQL injection attacks by
R ob malicious users. Enter the following expression exactly as shown into the “Search Films”
text box and click its associated “Submit Query” button. What happens, and why?
' OR 'X' = 'X
Answer: The findfilm.jsp page displays the entire contents of the film2 table. This is
because the actual query generated by the page is as follows:
SELECT title, description FROM film2 WHERE title = '' OR 'X' = 'X'
The initial single quote you entered terminated the first search string, allowing you to
append an OR clause to the SQL expression. Because 'X' always equals 'X', the expression
evaluates to true for every record in the film2 table. This could potentially be a security
risk, especially if the table contains sensitive information.
Assumptions
• You have a Linux terminal window open logged in as the root user (password:
oracle).
• The Apache web server is installed and running on port 80.
Duration
This practice should take you approximately 30 minutes to complete. ble
fe r a
Tasks
t r a ns
1. Display the contents of the /labs/sql/film2.sql file in the Linux terminal.nWhat
o - does
the SQL code in this file do?
2. Execute the /labs/sql/film2.sql script to create the film2a s
table
ainnthe sakila
database. r ) h ฺ
3. Verify that the film2 table exists and contains 1,000m
b
ฺ uide
records.
r o
ฺc nt G
4. Change the current working directory to /labs/python/web/activity4-4 and list the
p a e
files within that directory.
5. Copy the addfilm_start.py filestooaddfilm.py.
ne Stud
c i t h is
@file to findfilm.py.
6. Copy the findfilm_start.py
o ฺ ic use
rthe
7. Examine the contents
e r t of
t o
index.py file in a text editor such as vi, emacs, or gedit. The
index.py file
r o b
outputs a
s e
simple webpage with two forms. Each form contains a text field
c i (
and a button.
c e
Clicking nthe button for the “New Film” text box invokes the addfilm.py
c l i
i and clicking the button for the “Search Films” text box invokes the findfilm.py
script,
R
ert oscript.
b
Ro
8. Examine the contents of the addfilm.py file in your chosen text editor. You complete the
code in the addfilm.py file to insert a new row into the films2 table.
9. Examine the contents of the findfilm.py file in your chosen text editor. The code in the
findfilm.py file enables a user to search for a film by its title.
10. In the addfilm.py file under the comment labelled “A. Prepare the insert statement”,
create a variable called sql that contains a SQL statement which inserts a new row into the
films2 table. Use placeholders for the title and description columns.
11. In the addfim.py file under the comment labelled “B. Execute the insert statement”, create
a cursor object by calling the cursor() method on the database connection instance,
passing in prepared=True as the method argument.
12. In the addfilm.py file under the comment labelled “B. Execute the insert statement”, call
the cursor object’s execute() method, passing in the SQL statement and a tuple that
contains both the film title supplied by the user and any descriptive text string.
13. Close the cursor and call the database connection object’s commit() method to persist the
change to the database.
14. Save your work, and make the conn.py, index.py, addfilm.py, and findfilm.py
files executable.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
“Search Films” text box and click its associated “Submit Query” button.
18. The query in the findfilm.py script is not a prepared statement and there is no
validation of the value the user enters. This makes it vulnerable to SQL injection attacks by
malicious users. Enter the following expression exactly as shown into the “Search Films”
text box and click its associated “Submit Query” button. What happens, and why?
' OR 'X' = 'X
19. In the findfilm.py page under the comment labelled “C. Replace with a prepared
statement”, rewrite the query as a prepared statement. ble
20. Save the findfile.py file and reload the index.html page in Firefox. Enter the fe r a
following expression exactly as shown into the “Search Films” text box. Click its associated ans
“Submit Query” button. What happens this time, and why? n - t r
o
' OR 'X' = 'X
s an
21. Close Firefox and any open terminal windows.
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# cd /labs/sql/
# cat film2.sql
USE sakila
DROP TABLE IF EXISTS `film2`;
CREATE TABLE `film2` AS SELECT `film_id`, `title`, `description` FROM
`film`;
ALTER TABLE `film2` MODIFY `film_id` SMALLINT(5) UNSIGNED NOT NULL
PRIMARY KEY AUTO_INCREMENT;
ble
Answer: The SQL in film2.sql creates a copy of the film table in the sakila
fe r a
database called film2, preserving only the film_id, title, and description
ans
columns. The ALTER TABLE statement adds the auto_increment and primary key
attributes to the film_id column. n - t r
2. a no
Execute the /labs/sql/film2.sql script to create the film2 table in the sakila
database. h a s
ฺ b r) deฺ
Enter the following command at the Linux terminal prompt and receive the results shown:
# mysql -uroot -poracle < film2.sql com u i
mysql: [Warning] Using a passwordaon n G
rฺ the ecommand
t line interface can be
p
one s Stud
insecure.
# s
i c
3. Verify that the film2 tablecexists
e hi 1,000 records.
i@and contains
t
t o ฺr at the
Enter the following rcommand usLinux terminal prompt and receive the results shown:
e t o
i ( r b en-poracle
# mysqlo-uroot se\ \
i>ccSHOW CREATE
lic TABLE film2\G \
> -e "USE sakila;
R
erto mysql: [Warning] Using a password on the command line interface can be
> SELECT COUNT(*) FROM film2;"
b
Ro insecure.
*************************** 1. row ***************************
Table: film2
Create Table: CREATE TABLE `film2` (
`film_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8 NOT NULL,
`description` text CHARACTER SET utf8,
PRIMARY KEY (`film_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=latin1
+----------+
| COUNT(*) |
+----------+
| 1000 |
+----------+
[root@edddr9p1 sql]#
4. Change the current working directory to /labs/python/web/activity4-4 and list the
files within that directory.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/python/web/activity4-4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
# ls
addfilm.py addfilm_start.py findfilm_soln.py index.py
addfilm_soln.py conn.py findfilm_start.py
6. Copy the findfilm_start.py file to findfilm.py.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp findfilm_start.py findfilm.py
# ls
addfilm.py addfilm_start.py findfilm.py findfilm_start.py
ble
addfilm_soln.py conn.py findfilm_soln.py index.py
fe r a
7. Examine the contents of the index.py file in a text editor such as vi, emacs, or gedit. The
ans
index.py file outputs a simple webpage with two forms. Each form contains a text field
n - t r
o
s an
and a button. Clicking the button for the “New Film” text box invokes the addfilm.py
script, and clicking the button for the “Search Films” text box invokes the findfilm.py
script.
r ) ha ฺ
ฺb uide
The index.py file contains the following Python code:
m
#!/usr/bin/python
a r ฺco nt G
o
print('''Content-Type: text/htmln ep tude
@ s is S
c c i th
<html>
r i us e
<head>
e r toฺ4-4 t(Python)</title>
<title>Activity o
</head>ob se
i ( r e n
cc lic 4-4 (Python)</h1>
<body>
to Ri<h1>Activity
e r <p>
R ob <br/><br/>
<br/><br/>
</body>
</html>
''')
8. Examine the contents of the addfilm.py file in your chosen text editor. You complete the
code in the addfilm.py file to insert a new row into the films2 table.
import conn
import cgi
print('''Content-Type: text/html
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
<html>
<head><title>Activity 4-4 (Python)</title></head>
<body>
''')
print("<p>Disconnecting from o n S
s theisdatabase server</p>")
i @ h
dbconn.close()
ฺ r i cc se t
</body> be
print(''' rto to u
i (
</html>ro ense
cc
Ri''') lic
e r
9. toExamine the contents of the findfilm.py file in your chosen text editor. The code in the
R ob findfilm.py file enables a user to search for a film by its title.
The findfilm.py file contains the following Python code:
#!/usr/bin/python
import conn
import cgi
print('''Content-Type: text/html
<html>
<head><title>Activity 4-4 (Python)</title></head>
<body>
''')
...
s o ne Stud
# Connect to the database
c i @ this
t o ฺ ric use
dbconn = conn.dbconnect()
b e r the insert
to statement
# A. Prepare
o
sql =(r"INSERT e
s film2 (title, description) VALUES (%s, %s)"
nINTO
c i li c e
R ic
berto # B. Execute the insert statement
Ro
print("<p>%s added to the database</p>" % newFilm)
dbconn.close()
...
12. In the addfilm.py file under the comment labelled “B. Execute the insert statement”, call
the cursor object’s execute() method, passing in the SQL statement and a tuple that
contains both the film title supplied by the user and any descriptive text string.
Add the following code to the addfilm.py file under the comment labelled “B. Execute the
insert statement”:
...
ble
# Connect to the database
fe r a
dbconn = conn.dbconnect()
ans
n - t r
no
# A. Prepare the insert statement
sql = "INSERT INTO film2 (title, description) VALUES (%s, %s)"
s a
h a
# B. Execute the insert statement
cursor = dbconn.cursor(prepared=True)
ฺ b r) deฺ
cursor.execute(sql, (newFilm, "Temporarym
o u i
description"))
a c
rฺ %ennewFilm)
t G
p
ne Stud
print("<p>%s added to the database</p>"
s o
i
print("<p>Disconnecting from the
c @ thisdatabase server</p>")
dbconn.close()
...
t o ฺ ric use
13. Close the cursorb e r thetodatabase connection object’s commit() method to persist the
and call e
change toi rodatabase.
(the e ns
c c
icthe followingli code to the addfilm.py file under the comment labelled “B. Execute the
R
Add
bertoinsert statement”
Ro ...
# Connect to the database
dbconn = conn.dbconnect()
ble
fe r a
ans
n - t r
n o
18. The query in the findfilm.py script is not a prepared statement and a
s there is no
validation of the value the user enters. This makes it vulnerablehtoaSQL injection attacks by
malicious users. Enter the following expression exactly as ฺ b r) into
shown
d e ฺ “Search Films”
the
text box and click its associated “Submit Query” button. omWhat u i
happens, and why?
r ฺ
a den c t G
' OR 'X' = 'X
e p
Answer: The findfilm.py script displays
s tu contents of the film2 table. This is
on stheSentire
because the actual query generated
i c c e hi page is as follows:
i@ by tthe
r t o ฺr
SELECT title, description
u s FROM film2 WHERE title = '' OR 'X' = 'X'
b
The initial single o
equote eyout entered terminated the first search string, allowing you to
r o
append an(OR clausen
i tosthe SQL expression. Because 'X' always equals 'X', the expression
c
icespecially
evaluates c e
li for every record in the film2 table. This could potentially be a security
to true
R
erto
risk, if the table contains sensitive information.
b 19. In the findfilm.py page under the comment labelled “C. Replace with a prepared
Ro statement”, rewrite the query as a prepared statement.
Make the following adjustments to the code in the findfilm.py file under the comment
labelled “C. Replace with a prepared statement”:
...
# Connect to the database
dbconn = conn.dbconnect()
the user input to be handled as the content of a parameter and not as a part of the SQL
statement.
21. Close Firefox and any open terminal windows.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 5:
Handling om Errors i
u and
r ฺ
a denc t G
Warnings
p
e
on Chaptertu5
s s S
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
None
Duration
This practice should take approximately 5 minutes to complete.
Tasks
1. Which of the following statements is true?
ble
a. Setting the SQL mode affects all clients that connect to the server.
fe r a
b. If you want to set two SQL modes (for example, the STRICT_ALL_TABLES and
ans
ERROR_FOR_DIVISION_BY_ZERO modes), you must issue two SET statements.
n - t r
o
an
c. Unless you explicitly declare otherwise, setting the SQL mode affects only the client
that sets the mode. s
) ha ฺ
d. SQL modes affect the behavior of the server. For example, they influence the way in
r
which the server handles invalid input data.
m ฺb uide
a r ฺco nt G
e. SQL modes affect the features that the server provides for a client. For example, you
o n ep tude
can turn InnoDB support on and off using SQL modes.
2.
s is S
What is the syntax for changing the SQL mode to STRICT_TRANS_TABLES and
@
PIPES_AS_CONCAT?
c c i th
r i e
3.
e r toฺ to us
If you attempt to store a negative value in an UNSIGNED column, MySQL converts it to the
'nearest' legal value for the column. Which value is that?
4. ( rob ense
What type of values are inserted into table1 with the following INSERT statement?
i
cc INTO
RiINSERT lictable1 VALUES();
e r
5. toWith a particular setting for the SQL mode, MySQL adjusts invalid input values to legal
R ob values when possible and generates warning messages. You can also adjust the SQL
mode to issue errors instead of warnings. What is the general term for SQL modes that
enable this behavior?
6. The MySQL server generates warnings or errors when it cannot fully comply with a request
or when an action has possible unintended side effects. Which two statements do you use
to view these errors and warnings?
7. List the three levels of severity for warnings.
b. False. You can set multiple SQL modes by specifying them in a comma-delimited list.
c. True
d. True
e. False. SQL modes control some aspects of MySQL syntax, and how MySQL performs
data validation.
2. The syntax for changing the SQL mode to STRICT_TRANS_TABLES and
PIPES_AS_CONCAT is as follows:
ble
SET sql_mode = 'STRICT_TRANS_TABLES,PIPES_AS_CONCAT';
fe r a
3. The “nearest” legal value for an UNSIGNED column that is assigned a negative number is
ans
zero.
n - t r
4. no
The INSERT statement generates default values for the columns in table1, or NULL
a
where no default value is specified
h a s
5. r) deฺ
The general term for the SQL modes that enable the described behavior is “strict mode.”
ฺ b
c o m Gui
Strict mode comprises the SQL modes STRICT_ALL_TABLES and
STRICT_TRANS_TABLES.
p a rฺ ent
6. Display warnings and errors with the SHOW
o n e tud and SHOW ERRORS statements,
WARNINGS
respectively.
@ s is S
c
7. The three levels of severity for
c i warningsthare:
r i e
• Note
e r toฺ to us
• Warning b
i ( ro ense
icc
• Error
R lic
berto
Ro
Assumptions
• You have the Firefox web browser open
• You have a Linux terminal window open and logged in as the root user (password:
oracle).
Duration
ble
This practice should take approximately 10 minutes to complete.
fe r a
ans
Tasks
n - t r
1.
no
Find the pages in the MySQL Reference Manual that list the MySQL server, and mysql
client error codes and messages. a
2. h a s
Use the perror utility to determine what operating system error the error code 130
represents. ฺ b r) deฺ
3. c o m Gui
Change the current database to sakila and attempt to display all the rows in a non-
p a rฺ ent
existent table called no_such_table. Do you receive an error or a warning?
4. one s Stud
Look up the code generated by the preceding statement in the MySQL Reference Manual.
s
5. i@ thi
Display the current SQL mode setting.
c c
6. ฺ r i se
Change the SQL mode to ANSI. Confirm the change.
o u
e t
rthe table tnamed
o no_such_table using a DROP TABLE IF
7. Attempt to removeb
ro type e EXISTS
statement.
c i (What c e nofsmessage do you receive?
8. UseR li SHOW statement to view a description of the message.
icthe appropriate
be9.rtoExecute SHOW ERRORS. Explain the result.
Ro 10. Exit the mysql client program and leave the Linux terminal window open for the next
practice.
Assumptions
You are logged in to a Linux terminal window as the root user (password: oracle).
Duration
This practice should take you approximately 20 minutes to complete.
Tasks e
1. Change the current working directory to /labs/php/cli/activity5-3 and display the r a bl
contents of the directory. s fe
2. Copy the ExceptionExample_start.php file to ExceptionExample.php. - t r an
on
3.
an
Inspect the contents of the ExceptionExample.php file in a text editor.
s
4.
r ) ha ฺ
Under the comment labelled “A. Wrap connection attempt in a try/catch block”, write code
that attempts to connect to the database using the DSN provided. If the connection attempt
m ฺb uide
fails, display the MySQL error code and details, and a suitable message to the user before
terminating the script.
a r ฺco nt G
5. ep tude
Under the comment labelled “B. Define PDO error handling mode”, set the PDO attribute
o n
s is S
which forces PDO to throw an exception when an error occurs.
@
c i th
Note: This is the preferred method for error handling in PHP/PDO because it enables you
c
r i e
6. e toฺ to us
to pinpoint application errors and handle them gracefully.
r
Under the comment labelled “C. Wrap query attempt in a try/catch block”, write code that
( rob ense
attempts to query the database using the SQL provided. If the query attempt fails, display
i
c lic
Ric
the MySQL error code and details, and a suitable message to the user.
e
7.
r to Save your work and execute the ExceptionExample.php script.
R ob 8. Modify the ExceptionExample.php script to use the SQL statement string defined in the
variable $sql2.
9. Save and execute the ExceptionExample.php script. What happens?
10. Change the PDO attribute to raise warnings instead of errors when an exception occurs.
Note: This method of dealing with errors can be useful if you want to test or debug without
disrupting the flow of the application.
11. Save and execute the ExceptionExample.php script. What happens?
12. Change the PDO attribute to ignore errors and warnings when they occur.
Note: This is the default error handling mode and is not recommended.
13. Save your work and execute the ExceptionExample.php script again. What happens?
14. Close all open terminal windows.
Tasks
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
i@ i
$dsn = "mysql:host=localhost;dbname=world";
c "root",th
$dbh = new PDO ($dsn,
ฺr i c e "oracle");
r t o
print ("Connected\n");
t o us
e
rob ePDOnserror
# B. Define
i ( e handling mode
R icc lic
e r to # C. Wrap query attempt in a try/catch block
R ob $sql1 = "SELECT Name, Population FROM Country ORDER BY Population DESC
LIMIT 10";
// $sql2 = "SELECT WrongColumn FROM WrongTable";
$sth = $dbh->query($sql1);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
printf("Name: %s, Population: %s\n", $row['Name'],
$row['Population']);
}
$dbh = NULL;
print ("Disconnected\n");
?>
4. Under the comment labelled “A. Wrap connection attempt in a try/catch block”, write code
that attempts to connect to the database using the DSN provided. If the connection attempt
fails, display the MySQL error code and details, and a suitable message to the user before
terminating the script.
$dsn = "mysql:host=localhost;dbname=world";
$dbh = new PDO ($dsn, "root", "oracle");
print ("Connected\n");
# B. Define PDO error handling mode
s
# A. Wrap connection attempt in s
o
c i @ thi a try/catch block
ric use
try {
o ฺ
$dsn = "mysql:host=localhost;dbname=world";
$dbh = r t
b e e o "root", "oracle");
new PDO t($dsn,
c i (#roB. Define
print
c e nsPDO error handling mode
("Connected\n");
li
Ric $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
ob
echo "Connection error: \n";
R echo "Error code: " . $ex->getCode() . "\n";
echo "Error message: " . $ex->getMessage() . "\n";
exit ("Terminating.\n\n");
}
6. Under the comment labelled “C. Wrap query attempt in a try/catch block”, write code that
attempts to query the database using the SQL provided. If the query attempt fails, display
the MySQL error code and details, and a suitable message to the user.
Add the following code under the comment labelled “C. Wrap query attempt in a try/catch
block”:
...
# C. Wrap query attempt in a try/catch block
try {
$sql1 = "SELECT Name, Population FROM Country ORDER BY
Population DESC LIMIT 10";
// $sql2 = "SELECT WrongColumn FROM WrongTable";
$sth = $dbh->query($sql1);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$dbh = NULL;
print ("Disconnected\n");
?>
7. Save your work and execute the ExceptionExample.php script.
Enter the following command at the Linux terminal prompt. The script runs without any
errors and displays the results shown: ble
fe r a
# php ExceptionExample.php
ans
Connected
Name: China, Population: 1277558000 n - t r
Name: India, Population: 1013662000
a no
Name: United States, Population: 278357000
Name: Indonesia, Population: 212107000 h a s
Name: Brazil, Population: 170115000
ฺ b r) deฺ
Name: Pakistan, Population: 156483000
c o m Gui
p a rฺ ent
Name: Russian Federation, Population: 146934000
one s Stud
Name: Bangladesh, Population: 129155000
Name: Japan, Population: 126714000
s
Disconnected
c c i@ thi
Name: Nigeria, Population: 111506000
#
o ฺ r i u se
e r t to
b e
8. Modify the ExceptionExample.php
ro ens
script to use the SQL statement string defined in the
(
variable i$sql2.
c following
R
Makeicthe lic change to the ExceptionExample.php script:
berto ...
Ro # C. Wrap query attempt in a try/catch block
try {
$sql1 = "SELECT Name, Population FROM Country ORDER BY
Population DESC LIMIT 10";
$sql2 = "SELECT WrongColumn FROM WrongTable";
$sth = $dbh->query($sql2);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
printf("Name: %s, Population: %s\n", $row['Name'],
$row['Population']);
}
}
...
9. Save and execute the ExceptionExample.php script. What happens?
When you execute the script, the query generates a PDOException error, which is caught
by the catch block you implemented in step 6:
# php ExceptionExample.php
Connected
Error processing query:
Error code: 42S02
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
bertoNote: The line numbers referred to in the output might be different on your machine.
Ro 12. Change the PDO attribute to ignore errors and warnings when they occur.
Note: This is the default error handling mode and is not recommended.
Change the following line of code in the ExceptionExample.php script:
<?php
#
When you execute the script, it ignores the failed query. It then proceeds to the fetch()
operation and ultimately fails with a fatal error because the $sth variable does not contain
a PDOStatement object.
Note: The line numbers referred to in the output might be different on your machine.
14. Close all open terminal windows.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
You are logged in to a Linux terminal window as the root user (password: oracle).
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
ble
1. Change the current working directory to /labs/java/cli/activity5-4 and display the
fe r a
contents of the directory.
ans
2. Copy the ExceptionExample_start.java file to ExceptionExample.java.
n - t r
3. Inspect the contents of the ExceptionExample.java file in a text editor.
a no
4. Compile the ExceptionExample.java program. What is the result?
h a s
5. r) deฺ
Under the comment labelled “A. Wrap connection attempt in a try/catch block”, write code
ฺ b
c o m Gui
that attempts to connect to the database using the JDBC connection details provided.
p a rฺ ent
If the connection attempt fails for any reason, catch the Exception object that is thrown
onecall
and determine if the error is related to MySQL: d
tushowError()
• If the error is of type SQLException, s i s S
the method provided,
i @ h
t that was caught.
passing in the SQLException
ฺ r i cc sobject
e
r to exception,
• If the error is a general
t o u display the error details to the user.
e
6. Under c i robexit the
In either case,
(comment e n se program.
running
i c the
l i c labelled “B. Wrap query attempt in a try/catch block”, write code that
t o R to query the database using the SQL statement string provided in variable sql1. If
attempts
be r the query attempt fails, call the showError() method provided, passing in the
Ro 7.
SQLException object that was caught.
Under the comment labelled “C. Close the database connection in a finally block”, write
code that closes the database connection regardless of the success or failure of the query.
8. Compile and execute the ExceptionExample.java file.
9. Change the program so that it attempts to query the database using the SQL statement
string provided in variable sql2 instead of sql1.
10. Compile and execute the ExceptionExample.java file. What happens?
11. Under the comment labelled “D. Write code to display SQLException errors”, modify the
showErrors() method of the ExceptionExample class so that it displays the following
error details:
• Error message
• SQLSTATE
• MySQL error code
12. Compile and execute the ExceptionExample.java file. What happens?
13. Close any open terminal windows.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/java/cli/activity5-4/
# ls
ExceptionExample_start.java ExceptionExample_soln.java
2. Copy the ExceptionExample_start.java file to ExceptionExample.java.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp ExceptionExample_start.java ExceptionExample.java
ble
# ls
fe r a
ExceptionExample.java ExceptionExample_start.java
an s
ExceptionExample_soln.java
n - t r
3. no
Inspect the contents of the ExceptionExample.java file in a text editor.
a
The contents of the ExceptionExample.java file are as follows:
h a s
import java.sql.*;
ฺ b r) deฺ
c o m Gui
public class ExceptionExample {
p a rฺ ent
d
one s Stuargs){
public static void main(String[]
s
new ExceptionExample().showTop10Populations();
}
c c i@ thi
o i se
ฺr showTop10Populations()
u
privatertvoid o
e t {
i ( rob Connection
e n se conn = null;
c lic//
to Ric A. Wrap connection attempt in a try/catch block
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
e r conn = DriverManager.getConnection(
R ob "jdbc:mysql://localhost/world",
"root", "oracle");
System.out.println("Connected.");
if(rs.first()) {
do {
name = rs.getString("Name");
population = rs.getInt("Population");
output = String.format("Name: %s,Population: %d",
name, population);
s.close();
if (conn != null) {
conn.close ();
System.out.println ("Disconnected");
}
} ble
fe r a
// D. Write code to display SQLException errors
ans
private void showErrors(SQLException se) {
n - t r
}
System.out.println("There has been an error.");
a no
}
h a s
4. Compile the ExceptionExample.java program. What b
ฺ ) result?
is rthe e ฺ
om tand
Enter the following command at the Linux terminal prompt i d
u the results shown:
receive
# javac ExceptionExample.java pa r ฺ c n G
e d e
ExceptionExample.java:13: error:
xception; must be caught s ondeclared tuto be thrown
unreported
S
exception ClassNotFoundE
c i
or
is
@ th("com.mysql.jdbc.Driver").newInstance
ric use
Class.forName
();
r t o ฺ
...
b e e to error: unreported exception SQLException;
(berocaught s declared to be thrown
ExceptionExample.java:14:
c
must i c e nor
R ic li conn = DriverManager.getConnection(
if(rs.first()) {
do {
name = rs.getString("Name");
population = rs.getInt("Population");
output = String.format("Name: %s, Population: %d",
name, population);
System.out.println(output);
} while(rs.next());
} else {
System.out.println("No rows returned.");
}
s.close();
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Enter the following code in the ExceptionExample.java file under the comment labelled
“C. Close the database connection in a finally block”:
...
s.close();
}
catch (SQLException se) {
showErrors(se);
}
// C. Close the database connection in a finally block
ble
finally {
fe r a
if (conn != null) {
ans
try {
n - t r
no
conn.close ();
System.out.println ("Disconnected");
} s a
h a
r) deฺ
catch (SQLException se) {
showErrors(se);
ฺ b
m Gui
}
}
c o
rฺ ent
p a
one s Stud
}
}
s
// D. Write code
i c c i@
e t hi SQLException errors
to display
r t
private void
o ฺr showErrors(SQLException
u s se) {
e t o
System.out.println("There has been an error.");
}rob e
} ci (
c e ns
8. Ric li
Compile and execute the ExceptionExample.java file.
e r toEnter the following commands at the Linux terminal prompt and receive the results shown:
R ob # javac ExceptionExample.java
# java ExceptionExample
Thu May 12 10:18:36 UTC 2016 WARN: Establishing SSL connection
...
Connected.
Name: China, Population: 1277558000
Name: India, Population: 1013662000
Name: United States, Population: 278357000
Name: Indonesia, Population: 212107000
Name: Brazil, Population: 170115000
Name: Pakistan, Population: 156483000
Name: Russian Federation, Population: 146934000
Name: Bangladesh, Population: 129155000
Name: Japan, Population: 126714000
Name: Nigeria, Population: 111506000
Disconnected
#
− Ignore the SSL warning.
− The program compiles and executes without any errors.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
if(rs.first()) { ble
do {
fe r a
...
ans
10. Compile and execute the ExceptionExample.java file. What happens? n - t r
a no
Enter the following commands at the Linux terminal prompt and receive the results shown:
# javac ExceptionExample.java h a s
# java ExceptionExample
ฺ b r) deฺ
Thu May 12 10:18:36 UTC 2016 WARN: Establishing
o m GuSSL i connection
...
a c
rฺ ent
p
one s Stud
Connected.
There has been an error.
s
i@ withthani error. There are no details about the error,
Disconnected
i c c
The program compiles and
t o ฺ
making this issue difficult
executes
r to debug.
u se
b e r o Write code to display SQLException errors”, modify the
t“D.
(ro method
11. Under the comment e
nsof the ExceptionExample class so that it displays the following
labelled
c i c e
icdetails: li
showErrors()
R
error
Connected.
There has been an error.
SQLException: Table 'world.WrongTable' doesn't exist
SQLState: 42S02
MySQL Error code: 1146
Disconnected
#
The program compiles and executes with an error as before, but this time provides more
helpful error information.
ble
13. Close any open terminal windows.
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
You are logged in to a Linux terminal window as the root user (password: oracle).
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
ble
1. Change the current working directory to /labs/python/cli/activity5-5 and display
fe r a
the contents of the directory.
ans
2. Copy the ExceptionExample_start.py file to ExceptionExample.py.
n - t r
o
an
3. Inspect the contents of the ExceptionExample.py file in a text editor.
4. Make the ExceptionExample.py file executable. s
ha ฺ
5. Execute the ExceptionExample.py program. r )
ฺb uide
m
6. Change the ExceptionExample.py program so that it attempts to query the database
r ฺco nt G
using the SQL statement string provided in variable sql2 instead of sql1.
a
o n ep tude
7. Save and execute the ExceptionExample.py program.
s is S
8. Under the comment labelled “A. Wrap connection attempt in a try/except block”, write code
@
c i th
that attempts to connect to the database using the connection parameters provided. If the
c
r i e
e r toฺ to us
connection attempt fails, catch the mysql.connector.Error object and use it to display
the error code and error message before exiting the program.
i ( rob ense
9. Under the comment labelled “B. Wrap query attempt in a try/except block”, write code that
c lic
to Ric
attempts to query the database using the SQL statement string provided in variable sql1. If
the query attempt fails, catch the mysql.connector.Error object and use it to display
e r
R ob the error code and error message.
10. Execute the ExceptionExample.py script again. What happens?
11. Close any open terminal windows.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/python/cli/activity5-5/
# ls
ExceptionExample_soln.py ExceptionExample_start.py
2. Copy the ExceptionExample_start.py file to ExceptionExample.py.
# cp ExceptionExample_start.py ExceptionExample.py
# ls
ble
ExceptionExample.py ExceptionExample_start.py
fe r a
ExceptionExample_soln.py
ans
3. Inspect the contents of the ExceptionExample.py file in a text editor.
n - t r
The contents of the ExceptionExample.py file are as follows:
a no
#!/usr/bin/python
h a s
ฺ b r) deฺ
m Gui
import sys
import mysql.connector
c o
rฺ ent
p a
conn = None
conn_params = {
s o ne Stud
"database": "world",@
c i t h is
"host": "localhost",
"user": "root",
t o ฺ ric use
b e r "oracle"to
}
"password":
(=ro"SELECT s e
nName,
c c
sql1 i l i c e Population FROM Country ORDER BY Population DESC
R i LIMIT 10"
conn.close()
print("Disconnected from the database server")
b
Ro 7. Save and execute the ExceptionExample.py program.
Enter the following command at the Linux terminal prompt and receive the results shown:
# python ExceptionExample.py
Connected to the database server
Traceback (most recent call last):
File "ExceptionExample.py", line 22, in <module>
cursor.execute(sql2)
File "/usr/lib/python2.6/site-packages/mysql/connector/cursor.py",
line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/lib/python2.6/site-
packages/mysql/connector/connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY,
query))
File "/usr/lib/python2.6/site-
packages/mysql/connector/connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1146 (42S02): Table
'world.WrongTable' doesn't exist
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Make the following changes to the code in the ExceptionExample.py file under the
comment labelled “A. Wrap connection attempt in a try/except block”:
...
# A. Wrap connection attempt in try/except block
try:
conn = mysql.connector.connect(**conn_params)
print("Connected to the database server")
except mysql.connector.Error as e:
ble
print("Cannot connect to server")
fe r a
print("Error code: %s" % e.errno)
ans
print("Error message: %s" % e.msg)
n - t r
no
print("Terminating.")
sys.exit(1)
s a
...
h a
9. Under the comment labelled “B. Wrap query attempt in a b
ฺ r) dblock”,
try/except e ฺ write code that
attempts to query the database using the SQL statement
om string i
uprovided in variable sql1. If
r ฺ
the query attempt fails, catch the mysql.connector.Error
a den c t G object and use it to display
the error code and error message. e p
Make the following changes to the s onin the
code S tu
ExceptionExample.py file under the
comment labelled “B. Wrap query c i@attempt
t s
hini a try/except block”:
ฺr i c e
...
r t o t o us
# B. Wrapbe
try: (ro n s e
query attempt in a try/except block
c c i l i c e
R i cursor.execute(sql1)
cursor = conn.cursor()
conn.close()
print("Disconnected from the database server")
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 6:
Building u i
om Database-Driven
r ฺ c n
a Applicationst G
Web
p d e
e
on Chaptertu6
s s S
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
You have a terminal window open and are logged in as the Linux root user.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
1. In a Linux terminal window, list the contents of the /labs/php/web/activity6-1 ble
directory. fe r a
2. Copy the layout_start.php file to create a new file called layout.php. ans
n - t r
3. Examine the contents of the conn.php script. What is the purpose of this script? o
an
4. Examine the contents of the index.php script. What is the purpose of this script?
s
r ) ha ฺ
5. Open the layout.php script in a text editor. This script contains the beginnings of the
ฺb uide
show_in_table() function. The following steps tell you how to complete the function,
m
using the comments as a guide.
a r ฺco nt G
n ep tude
6. Under the comment labelled “A. Get table headers from the result set”, write PHP code to
retrieve the first row in the $items array. Use the use PHP’s current() function to return
o
s is S
the first array element and store it in a variable called $fields.
@
c c i th
i e
7. Under the comment labelled “B. Print table headers”, process each key/value pair in the
r
e r toฺ to us
$fields array and use the key as the heading for each column in the HTML table. Append
i ( rob ense
the appropriate markup to the $result variable.
Note: You can use <th></th> table header tags to make the column names appear in
c lic
to Ric
bold format. Ensure that you close the enclosing table row (<tr>) tag for the header row.
e r
8. Locate the loop in the page that iterates through the rows in the $items result set array.
R ob Within this loop is a nested loop, which iterates through each column value in the row.
Under the comment labelled “C. Get each column value from the row”, write PHP code that
populates each column in the HTML table with the column value from the result set. Ensure
that each column value is properly encoded to display any accented characters correctly.
9. Under the comment labelled “D. Close tag for current row”, write PHP code that creates the
appropriate markup to close the current row tag.
10. Under the comment labelled “E. Close table tag”, write PHP code that creates the
appropriate markup to close the table tag.
11. Save your work and launch the index.php page in the Firefox browser by visiting the
following URL: http://localhost/php/activity6-1/index.php.
12. Leave the Linux terminal window and Firefox browser open for the next practice.
# cd /labs/php/web/activity6-1
# ls
conn.php index.php layout_soln.php layout_start.php
2. Copy the layout_start.php file to create a new file called layout.php.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp layout_start.php layout.php
[root@edddr9p1 activity6-1]# ls
ble
conn.php layout_soln.php index.php
fe r a
layout.php layout_start.php
ans
3. Examine the contents of the conn.php script. What is the purpose of this script?
n - t r
no
Enter the following command at the Linux terminal prompt and receive the results shown:
a
# cat conn.php
h a s
<?php
ฺ b r) deฺ
# conn.php: utility library for connecting
c o m to Gtheui MySQL Server
p a rฺ ent
ne Stud
try {
# Create the DSN
s o
i @ this
$dsn = "mysql:host=localhost;dbname=world";
c
# Connect toothe
t ฺ ricdatabase
u se server
ber PDOse($dsn,
$dbh = new
echoro
toto "root", "oracle");
} ci ( c e n
"<p>Connected the database server</p>";
c l i
Ricatch (PDOException $e) {
e r to die ("<p>Cannot connect to the database server</p>");
R ob }
?>
− Answer: The conn.php script tries to connect to the world database on the
MySQL server and fails gracefully if the connection is unsuccessful.
4. Examine the contents of the index.php script. What is the purpose of this script?
# cat index.php
<!doctype html>
<html>
<head>
<title>Activity 6-1 (PHP)</title>
</head>
<body>
<h1>Activity 6-1 (PHP)</h1>
<?php
require_once "conn.php";
require_once "layout.php";
# generate table
print (show_in_table ($items));
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
$dbh = NULL;
echo("<p>Disconnected from the database server</p>");
?>
</p>
</body>
</html>
− Answer: The index.php script queries the City table for the five most populated
cities and returns the results in descending order of population. The script then
ble
passes the results of the query (an associative array) to the show_in_table()
fe r a
function in layout.php and prints the return value.
ans
5. Open the layout.php script in a text editor. This script contains the beginnings of the
n - t r
show_in_table() function. The following steps tell you how to complete the function, o
using the comments as a guide.
s an
The contents of the layout.php script are as follows:
r ) ha ฺ
<?php
m ฺb uide
function show_in_table ($items) {
a r ฺco nt G
$result = "";
o n ep tude
S result set
s fromisthe
i @ h
cc se t
// A. Get table headers
$result =to ฺ r i u
// B.be
r "<table t o border='1'>\n<tr>\n";
ro ense
Print table headers
c i (
Ric lic
e r to // Retrieve each row from the resultset
}
}
// E. Close tag for current row
return $result;
}
?>
6. Under the comment labelled “A. Get table headers from the result set”, write PHP code to
retrieve the first row in the $items array. Use the use PHP’s current() function to return
the first array element and store it in a variable called $fields.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
$fields = current($items);
...
7. Under the comment labelled “B. Print table headers”, process each key/value pair in the
$fields array and use the key as the heading for each column in the HTML table. Append
the appropriate markup to the $result variable.
Note: You can use <th></th> table header tags to make the column names appear in
bold format. Ensure that you close the enclosing table row (<tr>) tag for the header row.
Enter the following code in the layout.php script under the comment labelled “B. Print
ble
table headers”:
fe r a
...
ans
$result = "<table border='1'>\n<tr>\n";
n - t r
// B. Print table headers
foreach ($fields as $key => $value) { a no
$result .= "<th>$key</th>\n";
h a s
}
ฺ b r) deฺ
$result .= "</tr>\n";
c o m Gui
...
8. Locate the loop in the page that iterates through p a rฺthe rows
e n t the $items result set array.
in
Within this loop is a nested loop, which
s S tud each column value in the row.
e through
oniterates
Under the comment labelled “C.
c i @Get each
t h is value from the row”, write PHP code that
column
populates each column in
ฺ cthe HTMLsetable with the column value from the result set. Ensure
riproperly
that each column valuer t o is
t o u encoded to display any accented characters correctly.
e
b codesinethe layout.php script under the comment labelled “C. Get
i ( rovalue
Enter the following
e n the row”:
c c
each column
i... l i c from
o R
bert // Retrieve each row from the resultset
Ro
foreach ($items as $row) {
// Generate output for new row
$result .= "<tr>\n";
foreach ($row as $val) {
// C. Get each column value from the row
$val_html = htmlentities($val);
$result .= "<td>$val_html</td>\n";
}
}
...
$result .= "</tr>\n";
...
10. Under the comment labelled “E. Close table tag”, write PHP code that creates the
appropriate markup to close the table tag.
Enter the following code in the layout.php script under the comment labelled “D. Close
table tag”:
...
// E. Close table tag
ble
$result .= "</table>\n";
fe r a
ans
}
return $result;
n - t r
o
an
11. Save your work and launch the index.php page in the Firefox browser by visiting the
s
r ) ha ฺ
following URL: http://localhost/php/activity6-1/index.php.
The page displays the following:
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
12. Leave the Linux terminal window and Firefox browser open for the next practice.
Assumptions
You have a terminal window open and are logged in as the Linux root user.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
1. In a Linux terminal window, list the contents of the /labs/java/web/activity6-2 ble
directory. fe r a
2. Copy the index_start.jsp file to create a new file called index.jsp. ans
n - t r
3. Examine the contents of the WEB-INF/conn.inc file. What is the purpose of the JSTL o
markup in this file?
s an
r ) ha ฺ
4. Open the index.jsp page in a text editor. The code in this page connects to the world
ฺb uide
database and then creates an HTML table. The following steps tell you how to complete
m
ฺco nt G
and populate the table, using the comments as a guide.
a r
5. Under the comment labelled “A. Get column names for header row”, use the result set’s
n ep tude
columnNames property to iterate through the column names and display each in a table
o
s is S
header tag (<th>) in the first row of the HTML table.
@
c c i th
6. Under the comment labelled “B. Iterate through each row”, create a <c:forEach> loop
r i e
e r toฺ to us
that iterates through each row in the result set.
i ( rob ense
7. Under the comment labelled “C. Create a row in the HTML table for each row in the result
set”, create the HTML row tag that will display the contents of each row.
c lic
Ric
8. Under the comment labelled “D. Create a column in the HTML table for each column in the
to
e r result set”, loop through each column value in the row and output it as an HTML table
# cd /labs/java/web/activity6-2
# ls
index_soln.jsp index_start.jsp WEB-INF
2. Copy the index_start.jsp file to create a new file called index.jsp.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp index_start.jsp index.jsp
# ls
ble
index.jsp index_soln.jsp index_start.jsp WEB-INF
fe r a
3. Examine the contents of the WEB-INF/conn.inc file. What is the purpose of the JSTL
ans
markup in this file?
n - t r
no
Enter the following command at the Linux terminal prompt and receive the results shown:
a
# cat WEB-INF/conn.inc
h a s
<c:catch var="connexception">
ฺ b r) deฺ
<sql:setDataSource
var="conn"
c o m Gui
driver="com.mysql.jdbc.Driver" rฺ t
p a e n
user="root"
s o ne Stud
url="jdbc:mysql://localhost/world"
password="oracle" @
c i t h is
/>
t o ฺ ric use
</c:catch>
b e r toin WEB-INF/conn.inc tries to connect to the world
− Answer: The markup
(ro conethe e
nsMySQL server and raises an exception if the connection is
c i
database
li
Ric unsuccessful.
toOpen the index.jsp page in a text editor. The code in this page connects to the world
e r
4.
R ob database and then creates an HTML table. The following steps tell you how to complete
and populate the table, using the comments as a guide.
The contents of the index.jsp page are as follows:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ include file="/WEB-INF/conn.inc" %>
<!DOCTYPE html>
<html>
<head>
<title>Activity 6-2 (Java)</title>
</head>
<body>
<h1>Activity 6-2 (Java)</h1>
<c:choose>
<c:when test="${not empty connexception}">
<p>Error connecting to the database</p>
</c:when>
<c:otherwise>
<p>Connected to the database</p>
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
<table border="1">
<%-- A. Get column names for header row --%>
<%-- C. Create a row in the HTML table for each row in the
resultset --%>
</c:forEach>
...
7. Under the comment labelled “C. Create a row in the HTML table for each row in the result
set”, create the HTML row tag that will display the contents of each row.
resultset --%>
<tr>
<%-- D. Create a column in the HTML table for each column
in the result row --%>
</tr>
</c:forEach>
...
8. Under the comment labelled “D. Create a column in the HTML table for each column in the
result set”, loop through each column value in the row and output it as an HTML table ble
column entry using the <td> tag. fe r a
Enter the following code in the index.jsp file under the comment labelled “D. Create a ans
column in the HTML table for each column in the result set”: n - t r
... a no
<%-- B. Iterate through each row --%>
h a s
<c:forEach items="${rs.rowsByIndex}" var="row">
ฺ b r) fordeach
e ฺ row in the
<%-- C. Create a row in the HTML table
u i
om t Gresultset
a r ฺ c n
--%>
o n
<%-- D. Create a column
s is Svar="column">in the result row --%>
@
<c:forEach iitems="${row}"
th
c value="${column}"/></td>
r i c e
oฺ to us
<td><c:out
r t
</c:forEach>
e
r o b
</tr>
se
c i ( e
</c:forEach>n
Ric
... lic
e r to
R ob
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
o mopen for u i next practice.
10. c
Leave the Linux terminal window and Firefox browser
a rฺ ent G the
p
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
You have a terminal window open and are logged in as the Linux root user.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
1. In the Linux terminal window, list the contents of the ble
/labs/python/web/activity6-3 directory. fe r a
2. Copy the layout_start.py file to create a new file called layout.py. ans
n - t r
3. Examine the contents of the conn.py file. What is the purpose of this Python code? o
4. an
Examine the contents of the index.py file. What is the purpose of the Python code in this
s
file?
r ) ha ฺ
5. ฺb uide
Open the layout.py program in a text editor. This program contains the beginnings of the
m
using the comments as a guide. a r ฺco nt G
show_in_table() function. The following steps tell you how to complete the function,
6. n ep tude
Under the comment labelled “A. Get column names”, write code that uses the
o
s is S
column_names property of the cursor object to retrieve a list of column names. Store the
@
c c i th
results in a variable called colnames.
r i e
7. toฺ to us
Under the comment labelled “B. Create header row for table from column names”, write
e r
i ( rob ense
code that creates the header row, looping through each column name in the colnames
variable and enclosing it within table header tags (<th></th>). Append the generated
c lic
to Ric
markup to the results variable.
e r
8. Under the comment labelled “C. Loop through the rows in the result set”, write code to
R ob create a for loop that iterates through each row in the result set.
9. Under the comment labelled “D. Create a new row in the HTML table for each row in the
result set”, write code that appends a new row to the table using <tr></tr> tags. Create a
variable called count to keep track of the row numbers and initialize it to zero.
10. Under the comment labelled “E. Populate the HTML table columns with the column values
in the result set”, write code that iterates through each column in the current row and
outputs its value in column tags (<td></td>).
11. Save your work.
12. Make the scripts in the labs/python/web/activity6-3 directory executable.
13. Open the index.py page in the Firefox browser by visiting the following URL:
http://localhost/cgi-bin/activity6-3/index.py.
14. Leave the Linux terminal window and Firefox browser open for the next practice.
# cd /labs/python/web/activity6-3
# ls
conn.py index.py layout_soln.py layout_start.py
2. Copy the layout_start.py file to create a new file called layout.py.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp layout_start.py layout.py
# ls
conn.py index.py layout.py layout_soln.py layout_start.py ble
3. Examine the contents of the conn.py file. What is the purpose of this Python code? fe r a
ans
The contents of the conn.py file are as follows:
n - t r
o
an
# cat conn.py
# conn.py: library file for connecting to MySQL using the
Connector/Python s
ha ฺ
# module r )
ฺb uide
m
r
# A. Add a reference to the Connector/Python
a ฺco nt module
G
import mysql.connector
o n ep tude
conn = None
@ s is S
c i th
c connection
ฺ r i
# B. Provide appropriate
u s e parameters
conn_params r=to o
be "world",
{
e t
r o
"database":
n s
i ( li"localhost",
c c
"host":
c e
to Ri "user": "root",
"password": "oracle"
e r
ob
}
R def dbconnect():
try:
global conn
def dbclose():
if conn is None:
print('''</p>Can't disconnect: there is no connection</p>''')
else:
# D. Close the connection
# cat index.py
#!/usr/bin/python
import conn
import sys
reload(sys) # Reload does the trick!
from layout import *
print('''Content-Type: text/html
ble
<html>
fe r a
<head><title>Activity 6-3 (Python)</title></head>
ans
<body>
''')
n - t r
o
print("<h1>Activity 6-3 (Python)</h1>")
s an
r ) ha ฺ
# Set the default encoding
sys.setdefaultencoding('UTF8') m ฺb uide
a r ฺco nt G
# Connect to the database
dbconn = conn.dbconnect()
o n ep tude
s is S
# Define the query ci@ th
r i c e
sql = '''
r us BY Population DESC LIMIT 5
toฺCitytoORDER
e
SELECT * FROM
'''
i ( rob ense
c c
to Ri#c Executelithe query
cursor = dbconn.cursor(buffered=True)
e r
ob
cursor.execute(sql)
R print(show_in_table(cursor))
cursor.close()
print('''
</body>
</html>
''')
− Answer: The index.py script queries the City table for the five most populated
cities and returns the results in descending order of population. The script then passes
the results of the query (as a cursor object) to the show_in_table() function in
layout.py and prints the return value.
5. Open the layout.py program in a text editor. This program contains the beginnings of the
show_in_table() function. The following steps tell you how to complete the function,
using the comments as a guide.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
def show_in_table(cursor):
# A. Get column names
results = ""
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
# D. Create a new row in the HTML table for each row in the
resultset
ble
# E. Populate the HTML table columns with the column values in
fe r a
the resultset
ans
results += '''</table>'''
n - t r
return results
a no
6. Under the comment labelled “A. Get column names”, write code that
h a suses the
ฺ b )
column_names property of the cursor object to retrieve arlist of column
e ฺ names. Store the
results in a variable called colnames.
o m the u id
a r ฺ
Enter the following code in the layout.py programc under
n t G comment labelled “A. Get
column names”:
o n ep tude
...
@ s is S
c
def show_in_table(cursor):
# A. Get columnic
i th
r
oฺ to us
names e
colnames = tcursor.column_names
e r
i ( rob= e""nse
results
for j in range(cursor.rowcount):
row = cursor.fetchone()
...
9. Under the comment labelled “D. Create a new row in the HTML table for each row in the
result set”, write code that appends a new row to the table using <tr></tr> tags. Create a
variable called count to keep track of the row numbers and initialize it to zero.
Enter the following code in the layout.py program under the comment labelled “D.
Create a new row in the HTML table for each row in the result set”: e
r a bl
...
s fe
an
# D. Create a new row in the HTML table for each row in the
resultset
n - t r
no
results += '''<tr>'''
count = 0
s a
# E. Populate the HTML table columns with the column values in
h a
ฺ b r) deฺ
the resultset
results += '''</tr>'''
c o m Gui
...
p a rฺ ent
10. Under the comment labelled “E. Populate
in the result set”, write code that iterates s onethrough
the HTML
S udtable
teach columns with the column values
column in the current row and
outputs its value in column tags c i @ t h i
(<td></td>).
s
ฺ r i cthe s e
Enter the following code
e r t o to u
in layout.py program under the comment labelled “E.
Populate the HTML
r o b tablesecolumns with the column values in the result set”:
... i
c ( c e n
R c # C. l
Loopi
i for j in range(cursor.rowcount):
through the rows in the resultset
results += '''</tr>'''
results += '''</table>'''
return results
11. Save your work.
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 30 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/php/web/activity6-4 fe r a
directory. ans
2. Copy the results_start.php file to create a new file called results.php. n - t r
o
3. an
Examine the index.php file. What is the purpose of this script?
s
4. ha ฺ
Open results.php in a text editor. This file contains the beginnings of a script that
r )
ฺb uide
displays Country table data in a tabular format on the page ten rows at a time. Users can
m
r ฺco nt G
navigate different “pages” of data by using the “Prev” and “Next” links. The following steps
tell you how to complete the script, using the comments as a guide.
a
5. n ep tude
Under the comment labelled “A. Get total number of rows”, retrieve the value of the
o
s is S
numrows query parameter in the URL for the page and store it in a variable called
@
$numrows.
c c i th
r i e
6. toฺ to us
Under the comment labelled “B. Determine which page of results to display”, use the PHP
e r
rob ense
isset() method to determine if the pagenum query parameter is present in the URL used
c i (
to access the page. If it is present, store the parameter value in a variable called
Ric lic
$pagenum. If it is not present, the page will display the first page of query results, so set
e r to $pagenum to 1.
R ob 7. Under the comment labelled “C. Page number of the last page”, calculate the number of
pages of results based on the number of rows in the Country table ($numrows) and the
number of rows per page ($page_rows). Store the page number of the last page in a
variable called $last_page.
8. Under the comment labelled “D. Ensure that current page number is within a valid range”,
test the value of $pagenum and adjust it if necessary so that it is neither less than one, or
greater than the value of $last_page.
9. Under the comment labelled “E. Set the LIMIT clause for the query”, create a string variable
called $sql_limit that uses the values of the $pagenum and the $page_rows variables
to create a SQL LIMIT clause in the following format:
LIMIT <offset>, <number of rows>
10. Under the comment labelled “F. Output result navigation links”, create the anchor tags for
the “Prev” and “Next” links. Set the links’ href attributes to the results.php page, with
suitable values for the pagenum query parameter depending on which direction the user is
navigating through the query results. Also set the numrows query parameter, which
specifies the total number of rows in the result set.
14. Leave the Linux terminal window and Firefox browser open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# cd /labs/php/web/activity6-4
# ls
conn.php layout.php results_soln.php
index.php results_start.php
2. Copy the results_start.php file to create a new file called results.php.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp results_start.php results.php
ble
# ls
fe r a
conn.php layout.php
index.php results_soln.php
results.php results_start.php
ans
3. Examine the index.php file. What is the purpose of this script? n - t r
o
an
Enter the following command at the Linux terminal prompt and receive the results shown:
s
# cat index.php
r ) ha ฺ
<!doctype html>
m ฺb uide
ฺco nt G
<html>
<head>
a r
<title>Activity 6-4 (PHP)</title>
o n ep tude
</head>
@ s is S
<body>
c c i th
i
<h1>Activity 6-4 (PHP)</h1>
r e
e r toฺ to us
rob e"conn.php";
<?php
(
require_once
i n se
ccCount the
Ri// lic rows in the resultset
e r to $sql="SELECT COUNT(*) FROM Country";
R ob $result = $dbh->query($sql);
$row = $result->fetch(PDO::FETCH_NUM);
$rows = $row[0];
echo "<p>There are " . $rows . " rows in the Country table.</p>";
echo "<a href='results.php?numrows=" . $rows . "'>View results</a>";
?>
</body>
</html>
− Answer: The index.php script queries the Country table in the world database
to retrieve a count of the number of rows. It displays a link to the results.php
page, passing the number of rows as a query parameter.
4. Open results.php in a text editor. This file contains the beginnings of a script that
displays Country table data in a tabular format on the page, ten rows at a time. Users can
navigate different “pages” of data by using the “Prev” and “Next” links. The following steps
tell you how to complete the script, using the comments as a guide.
The contents of the results.php script are as follows:
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
<?php
require_once "conn.php";
require_once "layout.php";
se
$sql = "SELECT " .
( r o
$sql_limit;
i n
cc "<p>" e
lic. $sql . "</p>";
Ri$sth
echo
= $dbh->query ($sql);
e r to $items = $sth->fetchAll (PDO::FETCH_ASSOC);
?>
5. Under the comment labelled “A. Get total number of rows”, retrieve the value of the
numrows query parameter in the URL for the page and store it in a variable called
$numrows.
Enter the following code under the comment labelled “A. Get total number of rows”:
...
<?php
require_once "conn.php";
require_once "layout.php";
$pagenum to 1.
Enter the following code under the comment labelled “B. Determine which page of results to
display”:
...
// B. Determine which page of results to display
if (isset($_GET['pagenum'])) {
$pagenum = $_GET['pagenum'];
} else {
$pagenum = 1;
ble
}
fe r a
...
t r a ns
7. Under the comment labelled “C. Page number of the last page”, calculate then
o - of
number
pages of results based on the number of rows in the Country table ($numrows) n
a last and the
number of rows per page ($page_rows). Store the page number ofsthe page in a
variable called $last_page.
r ) ha ฺ
Enter the following code under the comment labelled “C.ฺPage
m b number ide of the last page”:
o
ฺc nt G u
...
a r
// C. Page number of the last page
o n ep tude
s is S
$last_page = ceil($numrows/$page_rows);
@
...
c i
c“D. Ensure h current page number is within a valid range”,
tthat
8. Under the comment labelled r i e
s it if necessary so that it is neither less than one, or
r toฺ and
test the value of $pagenum
e t o uadjust
greater than o
r bvalue ofs$last_page.
the e
i ( e n
isR icc afollowing
Enter the
within
liccode under the comment labelled “D. Ensure that current page number
valid range”:
e r to ...
R ob // D. Ensure that current page number is within a valid range
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last_page) {
$pagenum = $last_page;
}
...
9. Under the comment labelled “E. Set the LIMIT clause for the query”, create a string variable
called $sql_limit that uses the values of the $pagenum and the $page_rows variables
to create a SQL LIMIT clause in the following format:
LIMIT <offset>, <number of rows>
Enter the following code under the comment labelled “E. Set the LIMIT clause for the
query”:
...
// E. Set the LIMIT clause for the query
$sql_limit = 'LIMIT ' . ($pagenum - 1) * $page_rows . ',' .
$page_rows;
...
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
...
// F. Output result navigation links
echo "<a href='results.php?pagenum=" . ($pagenum - 1) . "&numrows=" .
$numrows . "'>Prev</a> ";
echo "<a href='results.php?pagenum=" . ($pagenum + 1) . "&numrows=" .
$numrows . "'>Next</a>";
...
11. Save your work and launch the index.php page in the Firefox browser by visiting the
following URL: http://localhost/php/activity6-4/index.php. ble
The page displays the following: fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
(ro ctheenresults
13. Navigatei through
c
s by clicking the “Prev” and “Next” links and verify that the query
results
R li rows at a time.
ic display ten
b rtoLeave the Linux terminal window and Firefox browser open for the next practice.
e14.
Ro
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 30 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/java/web/activity6-5 fe r a
directory. ans
2. Copy the results_start.jsp files to create a new file called results.jsp. n - t r
o
3. an
Examine the index.jsp file. What is the purpose of this page?
s
4. ha ฺ
Open results.jsp in a text editor. This file contains the beginnings of a script that
r )
ฺb uide
displays Country table data in a tabular format on the page ten rows at a time. Users can
m
r ฺco nt G
navigate different “pages” of data by using the “Prev” and “Next” links. The following steps
tell you how to complete the script, using the comments as a guide.
a
5. n ep tude
Under the comment labelled “A. Get total number of rows”, use the built-in page param
o
s is S
object to retrieve the value of the numRows query parameter in the URL for the page and
@
c i th
store it in a variable called numRows.
c
r i e
6. toฺ to us
Under the comment labelled “B. Determine which page of results to display”, retrieve the
e r
rob ense
URL pageNumber parameter and use the <fmt:parseNumber> tag to convert it to a
c i (
numeric value.
7.
Ric lic
Under the comment labelled “C. Page number of the last page”, complete the variable
e r to assignment for the lastPageCalc variable by calculating the number of pages of results
R ob based on the number of rows in the Country table (numRows) and the number of rows per
page (rowsPerPage). Note how the code provided for you calculates the actual value of
lastPage based on the value of lastPageCalc.
8. Under the comment labelled “D. Ensure that current page number is within a valid range”,
test the value of pageNumber and adjust it, if necessary, so that it is neither less than zero
nor greater than the value of lastpage.
9. Under the comment labelled “E. Set the limit clause for the query”, create a variable called
limit that uses the values of the pageNumber and rowsPerPage variable to create a
SQL LIMIT clause in the following format:
LIMIT <offset>, <number of rows>
10. Under the comment labelled “F. Output result navigation links”, create the anchor tags for
the “Prev” and “Next” links. Set the links’ href attributes to the results.jsp page, with
suitable values for the pageNumber query parameter depending on which direction the
user is navigating through the query results. Also set the numrows query parameter that
specifies the total number of rows in the result set.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Tasks
1. In a Linux terminal window, list the contents of the /labs/java/web/activity6-5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
directory.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/java/web/activity6-5
# ls
index.jsp results_soln.jsp results_start.jsp WEB-INF
2. Copy the results_start.jsp files to create a new file called results.jsp.
Enter the following commands at the Linux terminal prompt and receive the results shown:
ble
# cp results_start.jsp results.jsp
fe r a
cp: overwrite `results.jsp'? y
ans
# ls
index.jsp results.jsp results_start.jsp n - t r
results_soln.jsp WEB-INF
a no
3. Examine the index.jsp file. What is the purpose of this page? as
r
Enter the following command at the Linux terminal promptband) hreceiveฺthe results shown:
m ฺ uide
# cat index.jsp
r o
ฺc nt G prefix="c" %>
a
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
ep tu%>de
o n
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql"
s is S
<%@ include file="/WEB-INF/conn.inc"
prefix="sql" %>
i @
call rows h define max number of pages --%>
tto
r i c e
us
<%-- Get a count of
r to*ฺ FROM o
<sql:query dataSource="${conn}"
e t
var="countRs" scope="session">
rob ense
SELECT Country
i (
</sql:query>
R cc Passlinumber
i<%-- c of rows to results page --%>
e r t o <c:import url="results.jsp?numRows=${countRs.rowCount}" />
b
Ro − Answer: The index.jsp page queries the Country table in the world database
to retrieve a count of the number of rows. It then redirects to the results.jsp
page, passing the number of rows as a query parameter.
4. Open results.jsp in a text editor. This file contains the beginnings of a script that
displays Country table data in a tabular format on the page ten rows at a time. Users can
navigate different “pages” of data by using the “Prev” and “Next” links. The following steps
tell you how to complete the script, using the comments as a guide.
The contents of the results.jsp page are as follows:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ include file="/WEB-INF/conn.inc" %>
<!DOCTYPE html>
<html>
<head>
<title>Activity 6-5 (Java)</title>
</head>
<body>
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
<!-- Number of rows in a page -->
fe r a
<c:set var="rowsPerPage" value="10" />
ans
n - t r
<!-- C. Page number of the last page -->
<c:set var="lastPageCalc">
a no
h a s
</c:set>
ฺ b r) duseeฺCeiling(N) ->
<!-- There is no 'round up' function in
${(N+(1-(N%1))%1)-1} -->
c o m Gui
JSTL,
(lastPageCalc % 1)) % 1) - 1} e
arฺ devalue="${(lastPageCalc
<fmt:formatNumber var="lastPageCeil" nt
"p maxFractionDigits="0"
+ (1 -
s
<fmt:parseNumber var="lastPage" u
on s Stvalue="${lastPageCeil}"
/>
/>
@
ci currenti
th page number is within a valid range --
r i c e
toฺ to us
<!-- D. Ensure that
>
e r
i ( robE. eSetnsthe
<!-- e limit clause for the query -->
c lic
to Ric <!-- Execute the query -->
e r <c:set var="query" value="SELECT Code, Name, Continent, Population
<table border="1">
<tr>
<%-- Get column names for header row --%>
<c:forEach items="${rs.columnNames}" var="columnName">
<th><c:out value="${columnName}" /></th>
</c:forEach>
</tr>
<%-- Iterate through each row --%>
<c:forEach items="${rs.rowsByIndex}" var="row">
<%-- Create a row in the HTML table for each row in the
resultset --%>
<tr>
</table>
</body>
</html>
5. Under the comment labelled “A. Get total number of rows”, use the built-in page param
object to retrieve the value of the numRows query parameter in the URL for the page and
ble
store it in a variable called numRows.
fe r a
Enter the following code under the comment labelled “A. Get total number of rows”:
ans
... n - t r
<!-- A. Get total number of rows -->
a no
<c:set var="numRows" value="${param.numRows}" />
h a s
r) ddisplay”,
...
6. Under the comment labelled “B. Determine which page of ฺ b
results ito e ฺ retrieve the
c om t Gu tag to convert it to a
URL pageNumber parameter and use the <fmt:parseNumber>
r ฺ
numeric value.
e p a den
Enter the following code under the comment
s S tu “B. Determine which page of results to
on slabelled
display”:
c c i@ thi
...
o i
ฺr which u se page of results to display -->
e t
<!-- B. rDetermine
tovar="pageNumber" value="${param.pageNumber}" />
o b e
ns
<fmt:parseNumber
... i (r
c c e
li labelled “C. Page number of the last page”, complete the variable
ic the comment
R
7. Under
toassignment for the lastPageCalc variable by calculating the number of pages of results
e r
R ob based on the number of rows in the Country table (numRows) and the number of rows per
page (rowsPerPage). Note how the code provided for you calculates the actual value of
lastPage based on the value of lastPageCalc.
Enter the following code under the comment labelled “C. Page number of the last page”:
...
<!-- C. Page number of the last page -->
<c:set var="lastPageCalc">
${numRows/rowsPerPage}
</c:set>
<!-- There is no 'round up' function in JSTL, use Ceiling(N) ->
${(N+(1-(N%1))%1)-1} -->
<fmt:formatNumber var="lastPageCeil" value="${(lastPageCalc + (1
- (lastPageCalc % 1)) % 1) - 1} " maxFractionDigits="0" />
<fmt:parseNumber var="lastPage" value="${lastPageCeil}" />
...
...
s o ne Stud
10. Under the comment labelled “F.
c i @ t h is navigation links”, create the anchor tags for
Output result
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
Ric
toLeave the Linux terminal window and Firefox browser open for the next practice.
e r
12.
R ob
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 30 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/python/web/activity6-6 directory. fe r a
2. Copy the results_start.py file to create a new file called results.py.
t r a ns
3. Examine the index.py file. What is the purpose of this program? o n -
4. Open results.py in a text editor. This file contains the beginnings
n
ofaa script that displays
s
Country table data in a tabular format on the page ten rows athaatime. Users can navigate
different “pages” of data by using the “Prev” and “Next” links. ฺ b r)The following
d e ฺ steps tell you
how to complete the script, using the comments as o amguide. u i
5. Under the comment labelled “A. Get total number r ฺ c
a ofdrows”, t G
n use the cgi.FieldStorage()
e p e
method to retrieve the URL query parameters
s n its value
ostore and
S tuin a variable
store them in a variable called params.
Inspect the numrows parameter and
i @ h i s called numrows.
t which page of results to display”, use the
6. Under the comment labelled
ฺ r i cc“B.toDetermine
s e
params.has_key()
e r t o to u
function determine if the pagenum query parameter is present in
the URL used to
r o b $pagenum.
access the
s e page. If it is present, store the parameter value as an integer in
a variable (called
i set $pagenum
cso n
ce to 1.
If it is not present, the page will display the first page of query
i c
results,
R l i
7.toUnder the comment labelled “C. Page number of the last page”, calculate the number of
be r pages of results based on the number of rows in the Country table (numrows) and the
Ro number of rows per page (pagerows). Store the page number of the last page in a variable
called lastpage.
Note: Convert the value of pagerows to a floating point value before performing the
calculation by using the float() method.
8. Under the comment labelled “D. Ensure that current page is within a valid range”, test the
value of pagenum and adjust it, if necessary, so that it is neither less than one nor greater
than the value of lastpage.
9. Under the comment labelled “E. Set the LIMIT clause for the query”, create a string variable
called sqllimit that uses the values of the pagenum and the pagerows variables to
create a SQL LIMIT clause in the following format:
LIMIT <offset>, <number of rows>
10. Under the comment labelled “F. Output result navigation links”, create the anchor tags for
the “Prev” and “Next” links. Set the links’ href attributes to the results.py page, with
suitable values for the pagenum query parameter depending on which direction the user is
navigating through the query results. Also set the numrows query parameter that specifies
the total number of rows in the result set.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# cd /labs/python/web/activity6-6
# ls
conn.py index.py layout.py results_soln.py results_start.py
2. Copy the results_start.py file to create a new file called results.py.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp results_start.py results.py
# ls
ble
conn.py layout.py results.py results_start.py
fe r a
index.py results_soln.py
ans
3. Examine the index.py file. What is the purpose of this program?
n - t r
o
an
Enter the following command at the Linux terminal prompt and receive the results shown:
# cat index.py s
ha ฺ
#!/usr/bin/python
r )
ฺb uide
m
ฺco nt G
import conn
import sys
a r
o n ep tude
s is S
print('''Content-Type: text/html
@
c c i th
<html>
r i us e
e r toฺ to 6-6</title></head>
<head><title>Activity
''') rob
<body>
i ( e n se
cc lic
Riprint("<h1>Activity 6-6 (Python)</h1>")
e r to
R ob # Connect to the database
dbconn = conn.dbconnect()
sql = '''
SELECT COUNT(*) FROM Country
'''
print('''
</body>
</html>
''')
import conn
import sys
import math
reload(sys) # Reload does the trick!
import cgi
ble
from layout import *
fe r a
ans
print('''Content-Type: text/html
n - t r
o
<html>
s an
ha ฺ
<head><title>Activity 6-6 (Python)</title></head>
<body>
''') r )
ฺb uide
m
a r
print("<h1>Activity 6-6 (Python)</h1>") ฺco nt G
# Set the default encodingon
ep tude
@ s is S
i
sys.setdefaultencoding('UTF8')
c c th
r i e
e r t ฺ to ofusrows
# A. Get totalonumber
#c i rob enwhich
(Determine se page of results to display
Ric
B.
lic
e r to
R ob # Number of rows per page
pagerows = 10
print('''
</body>
</html>
''')
5. Under the comment labelled “A. Get total number of rows”, use the cgi.FieldStorage()
method to retrieve the URL query parameters and store them in a variable called params.
Inspect the numrows parameter and store its value in a variable called numrows.
ble
Enter the following code under the comment labelled “A. Get total number of rows”: fe r a
ans
...
# A. Get total number of rows n - t r
params = cgi.FieldStorage()
a no
numrows = int(params['numrows'].value)
h a s
r) ddisplay”,
...
6. Under the comment labelled “B. Determine which page of ฺ b
results ito e ฺ use the
params.has_key() function to determine if theฺc
r om t query
pagenum G u parameter is present in
the URL used to access the page. If it is present,
e p a store
d e n parameter value as an integer in
the
a variable called $pagenum. If it is not n
s o present,S tupage will display the first page of query
the
results, so set $pagenum to 1.
c i@ t s
hi labelled “B. Determine which page of results to
i
Enter the following code under
ฺr c e
the comment
display”: r t o t o us
e
...
i ( rob enwhich se page of results to display
ccparams.has_key('pagenum'):
lic
# B. Determine
R iif
e r to else: pagenum = int(params['pagenum'].value)
R ob pagenum = 1
...
7. Under the comment labelled “C. Page number of the last page”, calculate the number of
pages of results based on the number of rows in the Country table (numrows) and the
number of rows per page (pagerows). Store the page number of the last page in a variable
called lastpage.
Note: Convert the value of pagerows to a floating point value before performing the
calculation by using the float() method.
Enter the following code under the comment labelled “C. Page number of the last page”:
...
# C. Page number of the last page
lastpage = int(math.ceil(numrows/float(pagerows)))
...
8. Under the comment labelled “D. Ensure that current page is within a valid range”, test the
value of pagenum and adjust it, if necessary, so that it is neither less than one nor greater
than the value of lastpage.
R cc
iprint("<a c
lihref='results.py?pagenum=%d&numrows=%d'>Next</a>"
((pagenum-1), numrows))
%
ble
fe r a
14. Click the “Display results” link and examine the URL to ensure that the number of rows in
ans
the Country table is passed as a query parameter.
n - t r
o
The page displays the following:
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
15. Navigate through the results by clicking the “Prev” and “Next” links and verify that the query
results display ten rows at a time.
16. Leave the Linux terminal window and Firefox browser open for the next practice.
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/php/web/activity6-7 fe r a
directory. ans
2. Copy the index_start.php file to create a new file called index.php. n - t r
o
3. an
Open index.php in a text editor. This file contains the beginnings of a script that displays
s
script, using the comments as a guide. r ) ha ฺ
the results of a query in tabular format. The following steps tell you how to complete the
m ฺb uide
4.
r ฺco nt G
Under the comment labelled “A. Get column to order by”, use the PHP isset() method to
check if the orderby query parameter is present in the page request URL. If orderby is
a
o n ep tude
present, use the value to create a string variable called $orderby that contains a SQL
s is S
LIMIT clause using the value of the orderby query parameter as the name of the column
@
c i th
to order by. If not, set $orderby to be an empty string.
c
r i e
5.
e r toฺ to us
Under the comment labelled “B. Execute the query”, create a string variable called $sql
that queries the Country table of the world database and returns the Code, Name,
( rob ense
Continent, and Population columns of the first 15 rows, applying any column ordering
i
c lic
Ric
specified in the orderby query parameter.
e
6.
r to Under the comment labelled “C. Create table headers as hyperlinks”, and within the
foreach loop that examines each of the columns in the first row of the result set, create
R ob hyperlinked column headers for the table using <th></th> and <a href=""> tags. The
href attribute for each hyperlinked column must reload the existing page with an orderby
query parameter that specifies the name of the column to sort results by.
7. Save your work and launch the index.php page in the Firefox browser by visiting the
following URL: http://localhost/php/activity6-7/index.php.
8. Click on each of the columns in turn and verify that the results are ordered by the column
you clicked.
9. Close the Linux terminal window and Firefox web browser.
# cd /labs/php/web/activity6-7
# ls
conn.php index_soln.php index_start.php
2. Copy the index_start.php file to create a new file called index.php.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp index_start.php index.php
# ls
ble
conn.php index.php index_soln.php index_start.php
fe r a
3. Open index.php in a text editor. This file contains the beginnings of a script that displays
ans
the results of a query in tabular format. The following steps tell you how to complete the
n - t r
script, using the comments as a guide. o
The contents of the index.php file are as follows: s an
<!doctype html>
r ) ha ฺ
<html>
m ฺb uide
<title>Activity 6-7 (PHP)</title> rฺc
<head> o tG
e p a d e n
</head>
<body>
s o n Stu
i
<h1>Activity 6-7 (PHP)</h1>
c @ this
t o ฺ ric use
<?php
b e r to
require_once ns e
(ro ce"conn.php";
c i
c A. Getlicolumn to order by
to Ri//
e r
R ob // B. Execute the query
}
$result .= "</tr>\n";
$result .= "<td>$val_html</td>\n";
}
}
// Close tag for current row
$result .= "</tr>\n";
// Close table tag
$result .= "</table>\n";
print $result;
ble
?>
fe r a
4. Under the comment labelled “A. Get column to order by”, use the PHP isset() method to ans
check if the orderby query parameter is present in the page request URL. If orderby is n - t r
no
present, use the value to create a string variable called $orderby that contains a SQL
a
h a s
LIMIT clause using the value of the orderby query parameter as the name of the column
to order by. If not, set $orderby to be an empty string.
ฺ b r) deฺ
c o m Gui
Enter the following code under the comment labelled “A. Get column to order by”:
...
// A. Get column to order by ep
arฺ dent
if (isset($_GET['orderby']))
s { tu
o. n$_GET['orderby'];
S
$orderby = " ORDER BY
c i@ thi
" s
} else {
ฺ r i c se
r t
$orderby = ""; o to u
}
b e e
...
c i (ro cens
5. Under li labelled “B. Execute the query”, create a string variable called $sql
Ric the comment
e r toContinent, and Population columns of the first 15 rows, applying any column ordering
that queries the Country table of the world database and returns the Code, Name,
"</th></a>";
}
...
7. Save your work and launch the index.php page in the Firefox browser by visiting the
following URL: http://localhost/php/activity6-7/index.php.
The page displays as follows:
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/java/web/activity6-8 fe r a
directory. ans
2. Copy the index_start.jsp file to create a new file called index.jsp n - t r
o
3. an
Open index.jsp in a text editor. This file contains the beginnings of a page that displays
s
page, using the comments as a guide. r ) ha ฺ
the results of a query in tabular format. The following steps tell you how to complete the
m ฺb uide
4.
r ฺco nt G
Under the comment labelled “A. Get column to order by”, use the built-in param object to
check if the orderby query parameter is present in the page request URL. If orderby is
a
o n ep tude
present, use the value to create a string variable called orderby that contains a SQL
s is S
LIMIT clause using the value of the orderby query parameter as the name of the column
@
c i th
to order by. If not, set orderby to be an empty string.
c
r i e
5.
e r toฺ to us
Under the comment labelled “B. Execute the query”, create a string variable called query
that queries the Country table of the world database and returns the Code, Name,
( rob ense
Continent, and Population columns of the first 15 rows, applying any column ordering
i
c lic
Ric
specified in the orderby query parameter.
e
6.
r to Under the comment labelled “C. Create table headers as hyperlinks” and within the
<c:forEach> loop that examines each of the columns in the first row of the result set,
R ob create hyperlinked column headers for the table using <th></th> and <a href="">
tags. The href attribute for each hyperlinked column must reload the existing page with an
orderby query parameter that specifies the name of the column to sort results by.
7. Save your work and launch the index.jsp page in the Firefox browser by visiting the
following URL: http://localhost:8080/activity6-8/index.jsp.
8. Click on each of the columns in turn and verify that the results are ordered by the column
you clicked.
9. Close the Linux terminal window and Firefox web browser.
Tasks
1. In a Linux terminal window, list the contents of the /labs/java/web/activity6-8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
directory.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cd /labs/java/web/activity6-8
# ls
index_soln.jsp index_start.jsp WEB-INF
2. Copy the index_start.jsp file to create a new file called index.jsp
Enter the following commands at the Linux terminal prompt and receive the results shown:
ble
# cp index_start.jsp index.jsp
fe r a
# ls
ans
index.jsp index_soln.jsp index_start.jsp WEB-INF
n - t r
3.
no
Open index.jsp in a text editor. This file contains the beginnings of a page that displays
a
the results of a query in tabular format. The following steps tell you how to complete the
page, using the comments as a guide. h a s
ฺ b
The contents of the index.jsp page are as follows:r) deฺ
c o m Gui prefix="c" %>
p rฺ ent
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
a
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
e
on s Stu%> d
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
s
<%@ include file="/WEB-INF/conn.inc"
R ob <c:choose>
<c:when test="${not empty connexception}">
<p>Error connecting to the database</p>
</c:when>
<c:otherwise>
<p>Connected to the database</p>
</c:otherwise>
</c:choose>
</c:forEach>
</tr>
<%-- Iterate through each row --%>
<c:forEach items="${rs.rowsByIndex}" var="row">
<%-- Create a row in the HTML table for each row in the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
resultset --%>
<tr>
<%-- Create a column in the HTML table for each column in
the result row --%>
<c:forEach items="${row}" var="column">
<td><c:out value="${column}"/></td>
</c:forEach>
</tr>
</c:forEach>
</table> ble
fe r a
</body>
an s
</html>
n - t r
4. no
Under the comment labelled “A. Get column to order by”, use the built-in param object to
a
h a s
check if the orderby query parameter is present in the page request URL. If orderby is
present, use the value to create a string variable called orderby that contains a SQL
ฺ b r) deฺ
LIMIT clause using the value of the orderby query parameter as the name of the column
o m Gui
to order by. If not, set orderby to be an empty string.
c
a rฺ ent
Enter the following code under the comment labelled “A. Get column to order by”:
p
...
s o ne Stud
<!-- A. Get column@ to orderis
c i t h by -->
<c:choose>
<c:when o
t ฺ ric useempty param.orderby}" >
test="${not
b e
<c:setr var="orderby"
to value=" ORDER BY ${param.orderby}" />
r o n s e
i ( <c:otherwise>
</c:when>
c c l i c e
to Ri <c:set var="orderby" value="" />
e r </c:otherwise>
R ob ...
</c:choose>
5. Under the comment labelled “B. Execute the query”, create a string variable called query
that queries the Country table of the world database and returns the Code, Name,
Continent, and Population columns of the first 15 rows, applying any column ordering
specified in the orderby query parameter.
Enter the following code under the comment labelled “B. Execute the query”:
...
<!-- B. Execute the query -->
<c:set var="query" value="SELECT Code, Name, Continent,
Population FROM Country ${orderby} LIMIT 15" />
<sql:query dataSource="${conn}" var="rs">
${query}
</sql:query>
<c:out value="${query}" /><br />
...
6. Under the comment labelled “C. Create table headers as hyperlinks” and within the
<c:forEach> loop that examines each of the columns in the first row of the result set,
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
9. Close the Linux terminal window and Firefox web browser.
Assumptions
• You have a terminal window open and are logged in as the Linux root user.
• You have the Firefox web browser open from the previous practice.
Duration
This practice should take you approximately 20 minutes to complete.
Tasks ble
1. In a Linux terminal window, list the contents of the /labs/python/web/activity6-9fe r a
directory.
t r a ns
2. Copy the index_start.py file to create a new file called index.py.
o n -
3. Open index.py in a text editor. This file contains the beginnings of aa n that displays
page
the results of a query in tabular format. The following steps tell h you s
a how to complete the
page, using the comments as a guide.
ฺ b r ) e ฺ
4. Under the comment labelled “A. Get column to order om by”, use the i d
u cgi.FieldStorage()
method to retrieve the URL query parametersaand r ฺcstore n t
them G in a variable called params.
Use the params.has_key object to check
n p
e tud
if the e
orderby query parameter is present in
o
the page request URL. If orderbysis present, S
s use the value to create a string variable
i @ h i
t order by. If using
called orderby that contains
parameter as the nameฺof r i cthec column
a SQL
s
LIMIT
eto
clause
not,
the value of the orderby query
set orderby to be an empty
string. r t o t o u
r o be labelleds e
5. Under the (comment
c i c e ntable of“B.theExecute the query”, create a string variable called sql that
R ic
queries the li
Country world database and returns the Code, Name,
and Population columns of the first 15 rows, applying any column ordering
ertospecified in the orderby query parameter.
Continent,
b
Ro 6. Under the comment labelled “C. Create table headers as hyperlinks” and within the for
loop that examines each of the columns in the first row of the result set, create hyperlinked
column headers for the table using the <th></th> and <a href=""> tags. The href
attribute for each hyperlinked column must reload the existing page with an orderby query
parameter that specifies the name of the column to sort results by.
7. Save your work.
8. Make the files in the /labs/python/web/activity6-9 directory executable.
9. Open the index.jsp page in the Firefox browser by visiting the following URL:
http://localhost/cgi-bin/activity6-9/index.py.
10. Click on each of the columns in turn and verify that the results are ordered by the column
you clicked.
11. Close the Linux terminal window and Firefox web browser.
# cd /labs/python/web/activity6-9
# ls
conn.py index_soln.py index_start.py results.py
2. Copy the index_start.py file to create a new file called index.py.
Enter the following commands at the Linux terminal prompt and receive the results shown:
# cp index_start.py index.py
# ls
ble
conn.py index.py index_soln.py index_start.py results.py
fe r a
3. Open index.py in a text editor. This file contains the beginnings of a page that displays
ans
the results of a query in tabular format. The following steps tell you how to complete the
n - t r
page, using the comments as a guide. o
The contents of the index.py file are as follows: s an
#!/usr/bin/python
r ) ha ฺ
m ฺb uide
import conn
import sys
a r ฺco nt G
import math
reload(sys) # Reload doesothe n ep t u de
@ s is S trick!
import cgi
c c i th
r i s e
r toฺ to utext/html
print('''Content-Type:
e
<html>
i ( r ob nse
i c c l i c e
<head><title>Activity 6-9 (Python)</title></head>
to R <body>
e r ''')
dbconn = conn.dbconnect()
cursor = dbconn.cursor(buffered=True)
cursor.execute(sql)
def show_in_table(cursor):
# Get column names
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
results = ""
results += "<table border=1>"
for i in range(len(colnames)):
# C. Create table headers as hyperlinks
results += "</tr>"
# Create a new row in the HTML table for each row in the resultset ble
results += "<tr>"
fe r a
count = 0
ans
# Populate the HTML table columns with the column values in the
n - t r
resultset o
for k in range(len(row)):
s an
col = row[count]
results += "<td>" + str(col) + "</td>"
r ) ha ฺ
count += 1
m ฺb uide
results += "</tr>"
a r ฺco nt G
results += "</table>"
o n ep tude
return results
@ s is S
c c i th
r i e
toฺ to us
# Output query results
r
print(show_in_table(cursor))
e
ob nse
cursor.close()
i ( r e
i c c
print('''
l i c
to R </body>
e r </html>
R ob ''')
4. Under the comment labelled “A. Get column to order by”, use the cgi.FieldStorage()
method to retrieve the URL query parameters and store them in a variable called params.
Use the params.has_key object to check if the orderby query parameter is present in
the page request URL. If orderby is present, use the value to create a string variable
called orderby that contains a SQL LIMIT clause using the value of the orderby query
parameter as the name of the column to order by. If not, set orderby to be an empty
string.
Enter the following code under the comment labelled “A. Get column to order by”:
...
# A. Get column to order by
params = cgi.FieldStorage()
if params.has_key('orderby'):
orderby = " ORDER BY " + params['orderby'].value
else:
orderby= ""
...
h a s
Enter the following code under the comment labelled “C. Create table headers as
hyperlinks”:
ฺ b r) deฺ
c o m Gui
rฺ column
...
# Create header row for table from
p a e n tnames
one s Stud
results += "<tr>"
for i in range(len(colnames)): s
# C. Create tablei@
i c c t hi hyperlinks
headers as
e
t o ฺ r
results += "<th><a
s colnames[i] ++ colnames[i]
href='?orderby="
u
+ "'>" +
+=r "</tr>"to
"</th>"
resultsbe
... (ro
i e n se
7. Save
c work.lic
icyour
R
be8.rtoMake the files in the /labs/python/web/activity6-9 directory executable.
Ro Enter the following commands at the Linux terminal prompt and receive the results shown:
# chmod +x *
# ls -al
...
drwxr-xr-x 2 root root 4096 Aug 26 16:39 .
drwxr-xr-x. 9 root root 4096 Aug 17 09:47 ..
-rwxr-xr-x 1 root root 820 Aug 21 17:12 conn.py
-rwxr-xr-x 1 root root 1448 Aug 26 16:39 index.py
-rwxr-xr-x 1 root root 1742 Aug 21 17:26 index_soln.py
-rwxr-xr-x 1 root root 1448 Aug 21 17:27 index_start.py
-rwxr-xr-x 1 root root 1761 Aug 21 17:24 results.py
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
11. Close the Linux terminal window and Firefox web browser.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 7:
Tables om
and G i
Views
u
r ฺ c
a 7 den t
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• You have a Linux terminal window open and are logged in as the Linux root user
(password: oracle).
• You have a mysql command-line session open and are logged in as the MySQL root
user (password: oracle).
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take you approximately 10 minutes to complete.
Tasks
1. Log into the mysql command-line client and change the current database to world.
2. Use the CREATE TABLE…AS syntax to create a new table called CitySelect based on a
query of all rows and columns in the City table.
ble
3. Compare the number of rows in the original City table and the new CitySelect table. Is
fe r a
there a difference?
ans
4. Compare the definition of the original City table to the definition of the new CitySelect
n - t r
table. Are there any differences between these table definitions? o
5. an
Create a new table called CityLike by issuing a CREATE TABLE…LIKE statement.
s
6. ) ha ฺ
Compare the number of rows in the original City table and the new CityLike table. Is
r
there a difference?
m ฺb uide
7. ฺco nt G
Compare the definition of the original City table to the definition of the new CityLike
a r
8. o ep tude
table. Are there any differences between these table definitions?
n
Leave the mysql command-line client open for the next practice.
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> USE world
Reading table information
...
Database changed
bl e
2. Use the CREATE TABLE…AS syntax to create a new table called CitySelect based on a
fe r a
query of all rows and columns in the City table. n s
n -
Enter the following statement at the mysql prompt and receive the results shown: tra
mysql> CREATE TABLE CitySelect a no
-> AS SELECT * FROM City;
h a s
Query OK, 4079 rows affected (#.## sec)
Records: 4079 Duplicates: 0 Warnings: 0 ฺb
r) deฺ
c o m Gui
3. Compare the number of rows in the original City
a r ฺ table t
and
n the new CitySelect table. Is
there a difference?
o n ep tude
S and receive the results shown:
s mysqlisprompt
Enter the following statements at the
i @ h
mysql> SELECT COUNT(*)
ฺ r i cc se t
FROM City;
+----------+
| COUNT(*)e|rt
o to u
rob
| i ( 4079 |en
+----------+ se
R cc lic
i+----------+
berto mysql> SELECT COUNT(*) FROM CitySelect;
Ro +----------+
| COUNT(*) |
+----------+
| 4079 |
+----------+
− Answer: The number of rows in both tables is identical.
4. Compare the definition of the original City table to the definition of the new CitySelect
table. Are there any differences between these table definitions?
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SHOW CREATE TABLE City\G
*************************** 1. row ***************************
Table: City
Create Table: CREATE TABLE `City` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`CountryCode` char(3) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int(11) NOT NULL DEFAULT '0',
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. In the sakila database, create a new view called short_films with the film_id,
title, and language_id columns from the film table for all films of 60 minutes or less
duration.
2. Execute the SHOW TABLES command to verify the addition of the short_films view.
ble
3. Execute a query that displays the contents of the new view.
fe r a
4. Leave the mysql command-line client open for the next practice.
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Enter the following statements at the mysql prompt and receive the results shown:
mysql> USE sakila
Reading table information
...
Database changed
i (
| store |
ccrows inlicset (#.## sec)
+----------------------------+
Ri27
e r to − The view short_films appears in the list of tables.
R ob 3. Execute a query that displays the contents of the new view.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT * FROM short_films;
+---------+-----------------------+-------------+
| film_id | title | language_id |
+---------+-----------------------+-------------+
| 2 | ACE GOLDFINGER | 1 |
| 3 | ADAPTATION HOLES | 1 |
| 8 | AIRPORT POLLOCK | 1 |
| 15 | ALIEN CENTER | 1 |
| 18 | ALTER VICTORY | 1 |
| 66 | BENEATH RUSH | 1 |
| 83 | BLUES INSTINCT | 1 |
| 97 | BRIDE INTRIGUE | 1 |
| 102 | BUBBLE GROSSE | 1 |
| 110 | CABIN FLASH | 1 |
...
| 972 | WHISPERER GIANT | 1 |
| 981 | WOLVES DESIRE | 1 |
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. In the short_films view, change the name of the film with an ID of 102 to ‘INCEPTION
ABYSS’. Verify the change with a suitable query.
2. Insert a new row into the short_films view with the following details, leaving the
film_id field blank:
ble
• Title: AVATAR VENDETTA
fe r a
• Language ID: 3
ans
3. Display the last AUTO_INCREMENT value MySQL used for the insertion. n - t r
4. no
Query the short_films view to see if the insertion appears. Does the new row appear in
a
a s
the view? Does it appear in the underlying film table? Explain this behavior.
h
5. Set the length of the newly added film to 45 minutes by updating
ฺ b r) the d e ฺ table. Verify the
film
update by issuing a suitable query against the short_films om t Gview.u i
6. Delete the row with film_id 1,001 from short_films, r ฺ c
a denand verify that the row is gone
e punderlying
both from the short_films view and n
s o the
S tu film table, with suitable queries.
7. Leave the mysql command-line
c i @client open
t h isfor the next practice.
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
b
Ro | LAST_INSERT_ID() |
+------------------+
| 1001 |
+------------------+
1 row in set (#.## sec)
− The new row has an ID of 1001.
4. Query the short_films view to see if the insertion appears. Does the new row appear in
the view?
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT * FROM short_films;
+---------+-----------------------+-------------+
| film_id | title | language_id |
+---------+-----------------------+-------------+
| 2 | ACE GOLDFINGER | 1 |
| 3 | ADAPTATION HOLES | 1 |
| 8 | AIRPORT POLLOCK | 1 |
| 15 | ALIEN CENTER | 1 |
| 18 | ALTER VICTORY | 1 |
| 66 | BENEATH RUSH | 1 |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take you approximately five minutes to complete.
Tasks
1. Check the short_films view with a suitable CHECK TABLE statement.
2. Add the WITH CHECK OPTION setting to the short_films view.
3. Re-execute the INSERT statement from Step 2 in the preceding practice, and note any
ble
difference in the results you get.
fe r a
4. Leave the mysql command-line client open for the next practice.
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Select all view information for the short_films view from the INFORMATION_SCHEMA
database.
2. Issue a suitable query to show the CREATE VIEW statement used to create short_films.
3. Show the table structure of the short_films view.
bl e
4. List all the tables and views in the sakila database, along with their table type (base table ra
or view). n s fe
5. Leave the mysql command-line client open for the next practice.
n - tra
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
e r t o
b character_set_client: utf8
Ro collation_connection: utf8_general_ci
1 row in set (#.## sec)
3. Show the table structure of the short_films view.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> DESCRIBE short_films;
+-------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+-------+
| film_id | smallint(5) unsigned | NO | | 0 | |
| title | varchar(255) | NO | | NULL | |
| language_id | tinyint(3) unsigned | NO | | NULL | |
+-------------+----------------------+------+-----+---------+-------+
3 rows in set (#.## sec)
+----------------------------+------------+
| actor | BASE TABLE |
| actor_copy | BASE TABLE |
| actor_info | VIEW |
...
| sales_by_store | VIEW |
| short_films | VIEW |
| staff | BASE TABLE |
| staff_list | VIEW |
| store | BASE TABLE | ble
+----------------------------+------------+
fe r a
27 rows in set (#.## sec)
ans
5. Leave the mysql command-line client open for the next practice. n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Create a view called districts, containing the city, district, and country for all cities in the
sakila database. You must join the address, city, and country tables to create the
view and each city must appear only once.
• Is the view updatable? Is it insertable?
ble
• If you allow duplicate cities in the view, is the view updatable and insertable?
fe r a
2. Create a view called customer_payments, containing the first name, last name, email
ans
address, and total amount paid for each customer (even customers who have not made any
n - t r
o
an
payments). Is this view updatable or insertable? Use the customer_payments view to
display information for the top 10 paying customers. s
3. ) ha ฺ
Create a view called actor_categories, containing the first name, last name, and film
r
ฺb uide
category name for all actors based on the categories of films they have acted in. You must
m
a r ฺco nt G
join five tables to get the view. You must also use the DISTINCT keyword, because some
n ep tude
actors have acted in several movies in each category. Use the new view to find the four
actors who have starred in the fewest categories of movies.
o
@ s is S
4. Exit the mysql client.
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE VIEW districts AS
-> SELECT DISTINCT city, district, country
-> FROM address JOIN city USING(city_id)
-> JOIN country USING(country_id);
Query OK, 0 rows affected (#.## sec)
Is the view updatable? Is it insertable?
− Answer: The view is not updatable or insertable because of the aggregation ble
performed by the DISTINCT keyword. You must use DISTINCT because two cities
fe r a
(Lethbridge and Woodridge) each have two addresses associated with them, and
ans
therefore show up twice in the join result. If you do not use DISTINCT, these cities
n - t r
will appear in the view.
a no
h a s
If you allow duplicate cities in the view, is the view updatable and insertable?
− Answer: If you omit the DISTINCT keyword, the cities
ฺ b r) of Lethbridge
d e ฺ and Woodridge
appear twice in the view. The view is updatable,
o m Gu
but is i
insertable only for a single
base table at a time, and then only for the
a c
rฺ country n t column. The city and
district columns belong to base
n ptables that e
e tud constraints on fields not
have
present in the districts view. o
s is S
c i @
2. Create a view called customer_payments th that contains the first_name, last_name,
r i c e
s customer (even customers who have not made any
email, and total amount
payments). e r toฺ paidtoforueach
( rob estatement
Enter thei following n se at the mysql prompt and receive the results shown:
R cc CREATE
imysql> lic VIEW customer_payments AS
erto
-> SELECT first_name, last_name, email,
b -> SUM(amount) AS total
Ro -> FROM customer LEFT JOIN payment USING(customer_id)
-> GROUP BY customer_id;
Query OK, 0 rows affected (#.## sec)
Is this view updatable or insertable?
− Answer: The customer_payments view is not updatable because it contains
aggregated values in the total column. Because it is not updatable, it is not
insertable either.
Use the customer_payments view to display information for the top 10 paying customers.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT * FROM customer_payments
-> ORDER BY total DESC LIMIT 10;
+------------+-----------+----------------------------------+--------+
| first_name | last_name | email | total |
+------------+-----------+----------------------------------+--------+
| KARL | SEAL | KARL.SEAL@sakilacustomer.org | 221.55 |
| ELEANOR | HUNT | ELEANOR.HUNT@sakilacustomer.org | 216.54 |
| CLARA | SHAW | CLARA.SHAW@sakilacustomer.org | 195.58 |
| RHONDA | KENNEDY | RHONDA.KENNEDY@sakilacustomer.org| 194.61 |
| MARION | SNYDER | MARION.SNYDER@sakilacustomer.org | 194.61 |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
3. Create a view called actor_categories that contains the first name, last name, and film
category name for all actors based on the categories of films they have acted in. You must
join five tables to get the view. You must also use the DISTINCT keyword, because several
actors have acted in several movies of each category.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> CREATE VIEW actor_categories AS
-> SELECT DISTINCT first_name, last_name, name AS `category`
-> FROM actor
ble
-> JOIN film_actor USING(actor_id)
fe r a
-> JOIN film USING(film_id)
-> JOIN film_category USING(film_id)
ans
-> JOIN category USING(category_id);
n - t r
Query OK, 0 rows affected (#.## sec)
a no
h a s categories of
Use the new view to find the four actors who have starred in the fewest
ฺ b )
movies. Enter the following statement at the mysql prompt rand receive
e ฺ the results shown:
mysql> SELECT first_name, last_name,
c o m Guid
-> COUNT(*) AS `different categories`
-> FROM actor_categories p a rฺ ent
-> GROUP BY 1, 2 ORDER BY
s one3 sLIMIT
S tu4;d
hi
+------------+-----------+----------------------+
| first_name | last_name
c c i@ | different
t categories |
ฺ r i us|e
t|oDEAN
+------------+-----------+----------------------+
| JUDY
| JULIAob
e r t o 8 |
r s e
| FAWCETT | 8 |
i ( e |nDEE
c c
| EMILY
l i c |
i+------------+-----------+----------------------+
9 |
R | KENNETH | PESCI | 9 |
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 8:
Working omwith i
uStrings
r ฺ c
a 8 den t G
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
1. Log into the mysql command line client and change the current database to sakila.
2. From the address table, use the CONCAT() function to list the address ID, address,
district, city ID, and postal code all on one line (separated by commas) for each address in
the district of Buenos Aires. ble
3. Execute a query that lists all rows in the address table that have a NULL value in the fe r a
address2 column. Make a note of the address_id for each row that the query returns. ans
n - t r
4. Execute a query that uses the CONCAT() function to list address_ID, address, o
an
address2, and district all on one line (separated by commas) for the addresses with
s
r ) ha ฺ
the IDs you discovered in the preceding step. What is the result, and why?
5. ฺb uide
Rewrite the query in the preceding step using an alternative concatenation function that
m
ฺco nt G
ignores null values and allows you to pass in a string to use as a column separator.
6. a r
Execute a query of the category table that displays the first three letters of the category
n ep tude
name using the column alias “short_category.” Use the UCASE() function to show the
o
names in upper case.
@ s is S
7. c c i th
Execute a query of the film_list table, to list the first actor in the comma-separated list
r i e
two steps: e r toฺ to us
of actors in the actors column using the column alias “starring.” Complete this query in
i ( rob ense
a. Use the INSTR() function to retrieve the position of the first comma.
c lic
to Ric
b. Use the value returned by INSTR() to calculate the end position as an argument to
e r SUBSTRING() to retrieve the portion of the string before the first comma.
R ob 8. Keep the Linux terminal window open and logged into the mysql client for the next
practice.
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
Enter the following statement at the mysql prompt and receive the results shown:
mysql> USE sakila
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
ble
fe r a
Database changed
ans
mysql>
n - t r
2. no
From the address table, use the CONCAT() function to list the address ID, address,
a
the district of Buenos Aires. h a s
district, city ID, and postal code all on one line (separated by commas) for each address in
ฺ b r) deฺ
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT CONCAT(address_id, ', ฺ', c o m Gu',i ',
-> district, ', ', city_id,p', a e n t
r', postal_code)
address,
ob
| 327, 1427 A Corua (La Corua) Place, Buenos Aires, 45, 85799 |
R | 364, 1623 Kingstown Drive, Buenos Aires, 20, 91299
| 410, 88 Nagaon Manor, Buenos Aires, 524, 86868
|
|
| 450, 203 Tambaram Street, Buenos Aires, 161, 73942 |
| 536, 166 Jinchang Street, Buenos Aires, 165, 86760 |
| 566, 1229 Varanasi (Benares) Manor, Buenos Aires, 43, 40195 |
| 591, 773 Dallas Manor, Buenos Aires, 424, 12664 |
+-----------------------------------------------------------------+
10 rows in set (#.## sec)
+------------+----------+
| address_id | address2 |
+------------+----------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
+------------+----------+
4 rows in set (#.## sec)
− The addresses with IDs of 1, 2, 3, and 4 have a value of NULL in the address2 ble
column. fe r a
ans
4. Execute a query that uses the CONCAT() function to list address_ID, address,
address2, and district all on one line (separated by commas) for the addresses with n - t r
no
the IDs you discovered in the preceding step. What is the result, and why?
a
a s
Enter the following statement at the mysql prompt and receive the results shown:
h
mysql> SELECT CONCAT(address_id, ', ', address, ฺ b r) ',deฺ
-> ', address2, ', ', district)
c o m Gui
-> FROM address
p a rฺ ent
ne Stud
-> WHERE address_id IN(1,2,3,4);
s o
+-------------------------------------------------------------------+
i @ this ', ', address2, ', ', district) |
| CONCAT(address_id, ', ', address,
c
| NULL
t o ric use
+-------------------------------------------------------------------+
ฺ |
e r t o
| NULLrob
| NULL |
( cen s e |
|c
c i
NULL
l i |
R i+-------------------------------------------------------------------+
R ob − Answer: The resulting string for each address is NULL. This is because the
CONCAT() function, like many other MySQL functions, returns NULL if one or more
of its arguments is NULL.
5. Rewrite the query in the preceding step using an alternative concatenation function that
ignores null values and allows you to pass in a string to use as a column separator.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT CONCAT_WS(', ', address_id, address, address2,
district)
-> FROM address
-> WHERE address_id IN(1,2,3,4);
+----------------------------------------------------------+
| CONCAT_WS(', ', address_id, address, address2, district) |
+----------------------------------------------------------+
| 1, 47 MySakila Drive, Alberta |
| 2, 28 MySQL Boulevard, QLD |
| 3, 23 Workhaven Lane, Alberta |
| 4, 1411 Lillydale Drive, QLD |
+----------------------------------------------------------+
4 rows in set (#.## sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
| BOB FAWCETT |
| NICK WAHLBERG |
| JODIE DEGENERES |
| GARY PHOENIX |
| KIRSTEN PALTROW |
...
| UMA WOOD |
| CARMEN HUNT |
| IAN TANDY |
+----------------------+ ble
997 rows in set (#.## sec)
fe r a
− Note: You need to subtract one from the comma position to avoid seeing the
ans
comma in the list of results.
n - t r
o
8.
an
Keep the Linux terminal window open and logged into the mysql client for the next
practice. s
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Assumptions
You have a Linux terminal prompt open and are logged into the mysql client from the previous
practice. The current database is sakila.
Duration
This practice should take approximately 20 minutes to complete.
Tasks ble
1. Concatenate the first and last names of all actors in the actor table whose last names fe r a
start with ‘AL’. Use the LIKE clause with the appropriate “wild card” operator to identify ans
matching last names. n - t r
o
2.
an
Concatenate the first and last names of all actors in the actor table whose last names end
s
with ‘ING’. Use the LIKE clause with the appropriate “wild card” operator to identify
matching last names. r ) ha ฺ
3. m ฺb uide
Concatenate the first and last names of all actors in the actor table whose last names
r ฺco nt G
contain the string ‘AVI’. Use the LIKE clause with the appropriate “wild card” operator to
a
identify matching last names.
o n ep tude
4. s is S
Concatenate the first and last names of all actors in the actor table whose last names do
@
i th
not have an ‘A’ or an ‘E’ as the second letter. Use the NOT LIKE clause with the appropriate
c c
r i e
toฺ to us
“wild card” operator to identify matching last names.
5. e r
Rewrite the preceding query using the REGEXP (or RLIKE) operator with an appropriate
i ( rob ense
regular expression.
c lic
Ric
6. Concatenate the first and last names of all actors in the actor table whose last names end
with “SON.” Use the RLIKE or REGEXP operator with an appopriate regular expression to
e r to achieve this.
R ob 7. Concatenate the first and last names of all actors in the actor table whose last names
start with a vowel (‘a’, ‘e’, ‘i’, ‘o’, or ‘u’) or end with “IS.” Use the RLIKE or REGEXP operator
with an appopriate regular expression to achieve this.
8. Consider the following regular expression. What common string pattern does it match and
how does it work?
^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$
9. Keep the Linux terminal window open and logged into the mysql client for the next
practice.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT CONCAT_WS(' ', first_name,last_name)
-> FROM actor
-> WHERE last_name LIKE 'AL%';
+--------------------------------------+
| CONCAT_WS(' ', first_name,last_name) |
+--------------------------------------+
| CUBA ALLEN |
ble
| KIM ALLEN
| MERYL ALLEN
|
|
fe r a
+--------------------------------------+
ans
3 rows in set (#.## sec)
n - t r
o
2.
an
Concatenate the first and last names of all actors in the actor table whose last names end
s
with ‘ING’. Use the LIKE clause with the appropriate “wild card” operator to identify
matching last names.
r ) ha ฺ
ฺb uide
Enter the following statement at the mysql prompt and receive the results shown:
m
r ฺco nt G
mysql> SELECT CONCAT_WS(' ', first_name,last_name)
a
-> FROM actor
o n ep tude
-> WHERE last_name LIKE
@ s is S
'%ING';
c c i
+--------------------------------------+
th
ฺ r i s e
| CONCAT_WS(' ', first_name,last_name) |
r
| SCARLETTeBENINGto to u
+--------------------------------------+
|
| EWANro b e
|c i ( BENING
GOODING
c e ns |
GREGORY liGOODING
MICHAEL |
R i|c |
e r to +--------------------------------------+
R ob 3.
4 rows in set (#.## sec)
Concatenate the first and last names of all actors in the actor table whose last names
contain the string ‘AVI’. Use the LIKE clause with the appropriate “wild card” operator to
identify matching last names.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT CONCAT_WS(' ', first_name,last_name)
-> FROM actor
-> WHERE last_name LIKE '%AVI%';
+--------------------------------------+
| CONCAT_WS(' ', first_name,last_name) |
+--------------------------------------+
| JENNIFER DAVIS |
| SUSAN DAVIS |
| SUSAN DAVIS |
+--------------------------------------+
3 rows in set (#.## sec)
t o ฺr
mysql> SELECT CONCAT_WS('
-> FROM ractor u s ',first_name,last_name)
-> o be last_namee o
t NOT RLIKE '^.[ae]';
r WHERE
s
i ( licen',first_name,last_name) |
c c
+-------------------------------------+
i+-------------------------------------+
o R | CONCAT_WS('
− The [aeiou] character class matches any vowel in the first position.
− The | symbol is the “alternation” character, allowing you to match one character
from a list of alternatives.
− The is characters are the alternative characters you are trying to match against.
− The $ symbol anchors the second search expression to the end of the string.
8. Consider the following regular expression. What common string pattern does it match and
how does it work?
ble
^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$
fe r a
Answer: This regular expression matches email addresses.
ans
− The ^ symbol “anchors” the search to the start of the string.
n - t r
o
s an
− The ([a-z0-9_\.-]+) searches for a valid username portion of the email
address, matching one or more lower case letters, numbers, underscores, periods,
) ha ฺ
or hyphens. Note how the period is preceded by a backslash (\). This is because a
r
ฺb uide
period on its own in regular expression syntax matches any character. The
m
a r ฺco nt G
backslash acts as an “escape” character.
address. o n ep tude
− The @ sign is required between the username and domain portion of the email
@ s is S
i th
− The ([\da-z\.-]+) searches for a valid domain name which must match one of
c c
r i e
toฺ to us
more lower case letters, numbers, underscores, or hyphens.
e r
− The \. characters specify that a period must exist between the domain name and
( rob ense
the country-specific Top-level Domain (TLD).
i
c lic
to Ric
− The ([a-z\.]{2,6})$ searches for a valid TLD at the end of the string, which
must contain 2 to 6 letters or periods.
e r
R ob 9. Keep the Linux terminal window open and logged into the mysql client for the next
practice.
Assumptions
You have a Linux terminal prompt open and are logged into the mysql client from the previous
practice. The current database is sakila.
Duration
This practice should take you approximately 15 minutes to complete
Tasks ble
1. Execute a DESCRIBE command to display the structure of the film_text table. fe r a
2. Add a FULLTEXT index to the description column of the film_text table and verify that ans
the new index exists by issuing a SHOW CREATE TABLE statement. n - t r
o
3. an
Test the new full-text search capability on the description column of the film_text table
s
default search mode (IN NATURAL LANGUAGE MODE.) r ) ha ฺ
by executing a query that matches all films with the word “Explorer” in the title using the
4. m ฺb uide
Modify the preceding query to use “Epic Drama” as the text to search against. Why don’t
r ฺco nt G
the query results contain only films with the text “Epic Drama” in their description?
a
5. ep tude
Rewrite the query from the preceding step to use IN BOOLEAN MODE and ensure that it
o n
s is S
matches only film descriptions that contain the exact string “Epic Drama”.
@
6. c c i th
Execute a query that matches all films with a description that contains the word “Robot” but
r i e
e r toฺ to us
not the word “Challenge.”
7.
8. i ( rob ense
Remove the FULLTEXT index from the description column in the film_text table.
Exit the mysql client and close the Linux terminal window.
c lic
to Ric
e r
R ob
Create Table: o
t ric TABLE
Table: film_text
ฺ
CREATE u se`film_text` (
`film_id`
b e rsmallint(6)
to NOT
NOT NULL,
( r
`title` o n s e
varchar(255) NULL,
c i c e
Ric PRIMARYliKEY (`film_id`),
`description` text,
ert oEnter the following statement at the mysql prompt and receive the results shown:
b
Ro mysql> SELECT * FROM film_text
-> WHERE MATCH(description)
-> AGAINST ('"Epic Drama"' IN BOOLEAN MODE);
+---------+-------------------+-------------------------+
| film_id | title | description |
+---------+-------------------+-------------------------+
| 1 | ACADEMY DINOSAUR | A Epic Drama of a Fem...|
| 53 | BANG KWAI | A Epic Drama of a Mad...|
| 384 | GROSSE WONDERFUL | A Epic Drama of a Cat...|
| 597 | MOONWALKER FOOL | A Epic Drama of a Fem...|
| 802 | SKY MIRACLE | A Epic Drama of a Mad...|
| 864 | SUNDANCE INVASION | A Epic Drama of a Lum...|
+---------+-------------------+-------------------------+
6 rows in set (#.## sec)
− The presence of the IN BOOLEAN MODE clause and the double quotes("")
around the search term signifies that the match must be exact.
+-------------------+----------------------------------------------+
| title | description |
+-------------------+----------------------------------------------+
| AGENT TRUMAN | ...a Robot And a Boy who must Escape... |
| ALLEY EVOLUTION | ...a Robot And a Composer who must Battle... |
| ANGELS LIFE | ...Battle a Robot in Berlin |
| BEAUTY GREASE | ...Sink a Robot in An Abandoned Mine Shaft |
| BEDAZZLED MARRIED | ...a Robot who must Meet... |
| BLINDNESS GUN | ...a Robot And a Dentist who must Meet... |
| BONNIE HOLOCAUST | ...a Crocodile And a Robot who must Find... |
ble
| BRIDE INTRIGUE | ...a Robot And a Monkey who must Vanquish... |
fe r a
| CADDYSHACK JEDI | ...Fight a Robot in Soviet Georgia |
ans
| CALIFORNIA BIRDS | ...a Robot who must Battle... |
n - t r
no
...
+---------+------------------------+-------------------------------+
s a
73 rows in set (#.## sec)
) h a
− The - operator next to the word “Challenge” in the search
ฺ b r text
e ฺ
omits any results that
contain this word.
c o m Guid
7. Remove the FULLTEXT index from the description
a r ฺ column
n t in the film_text table.
Enter the following statements at the mysql
o n dereceive the results shown:
epprompttuand
mysql> SHOW CREATE TABLE s
i s S
c i @ film_text\G
t h
***************************
r i c e 1. row ***************************
ฺ TABLE
toCREATE
Table: film_text
r t o us `film_text` (
Create Table:
e
i ( robvarchar(255)
`film_id`
e n se NOT
smallint(6) NOT NULL,
i c c`description`
`title`
l i c text, NULL,
mysql>
8. Exit the mysql client and close the Linux terminal window.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 9:
Working omwith i
uNumeric and
r ฺ c
a denData t G
Temporal
p
e
on Chaptertu9
s s S
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The world sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Log into the mysql command-line client and enter a SQL expression to add the following
together: 2, 3, and NULL.
2. Use a SQL expression to add the following together: 2, 3, and ‘6’ (in single quotation
marks).
3. Evaluate the following MySQL statement and explain the results: ble
fe r a
SELECT 1.1 + 2.2 = 3.3, 1.1E0 + 2.2E0 = 3.3E0;
ans
4. Issue a statement that finds the three countries with the largest gross national product (GNP
n - t r
o
an
column) in the Country table of the world database.
s
5. Use a numeric expression to find the average (mean) of the three gross national product
ha ฺ
value.
r )
values you found in the preceding step. Ensure that you include the decimal part of the
ฺb uide
m
a r ฺco nt G
6. Modify the statement in the previous step to exclude the decimal portion of the gross
o n ep tude
national product values in the query expression. Explain the difference in the result.
s is S
7. Use a suitable MySQL function to approximate the value of the average you calculated in
@
i th
the preceding step, to the nearest integer.
c c
r i e
toฺ to us
8. Calculate the FLOOR() and CEILING() values of the gross national product averages
r
from the preceding step.
e
( rob ense
9. Execute a query that lists the name and the difference between the current and old gross
i
c lic
Ric
national product values for the first 20 countries in the Country table. Exclude any
countries with NULL values in the GNP or GNPOld columns.
e r to
10. Modify the statement in the preceding step to display the absolute value of the difference
R ob between current and old gross national product values.
11. Modify the statement in the preceding step to remove the decimal part of the difference in
gross national product values, without affecting the integer part.
12. Keep the Linux terminal window open and logged into the mysql client for the next
practice.
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT 2+3+NULL;
+----------+ ble
| 2+3+NULL |
fe r a
+----------+
ans
| NULL |
n - t r
+----------+
1 row in set (#.## sec)
a no
− Adding NULL to a value gives a NULL result. h a s
2. Use a SQL expression to add the following together: 2, 3, ฺ b r) ‘6’ (indesingle
and ฺ quotation
marks). om t Gu i
Enter the following statement at the mysql prompt r ฺ
a andc n the results shown:
receive
e p d e
mysql> SELECT 2+3+'6';
s on s Stu
+---------+
c c i@ thi
| 2+3+'6' |
o ฺ r i u se
+---------+
| r
11 e t to
|
b e
(rino setce(#.##
+---------+
1c i ns sec)
R
row
li
i−c MySQL converts the string ‘6’ into a number before performing the calculation.
r t o
e3. Evaluate the following MySQL statement and explain the results:
b
Ro SELECT 1.1 + 2.2 = 3.3, 1.1E0 + 2.2E0 = 3.3E0;
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT 1.1 + 2.2 = 3.3, 1.1E0 + 2.2E0 = 3.3E0;
+-----------------+-----------------------+
| 1.1 + 2.2 = 3.3 | 1.1E0 + 2.2E0 = 3.3E0 |
+-----------------+-----------------------+
| 1 | 0 |
+-----------------+-----------------------+
1 row in set (#.## sec)
− Answer: The first expression in the statement is true. The second expression in the
statement uses approximate values and therefore cannot be guaranteed to give an
exact-value result.
4. Issue a statement that finds the three countries with the largest gross national product (GNP
column) in the Country table of the world database.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT Name, GNP FROM world.Country ORDER BY GNP DESC LIMIT 3;
+---------------+------------+
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
5. Use a numeric expression to find the average (mean) of the three gross national product
values you found in the preceding step. Ensure that you include the decimal part of the
value.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT (8510700.00 + 3787042.00 + 2133367.00)/3;
+------------------------------------------+
| (8510700.00 + 3787042.00 + 2133367.00)/3 |
+------------------------------------------+
ble
| 4810369.666667 |
fe r a
+------------------------------------------+
1 row in set (#.## sec)
t r a ns
6. Modify the statement in the previous step to exclude the decimal portion of o n
the
-
gross
national product values in the query expression. Explain the differenceain n the result.
Enter the following statement at the mysql prompt and receiveh a s
the results shown:
ฺ b r ) e ฺ
i d
comnt Gu
mysql> SELECT (8510700 + 3787042 + 2133367)/3;
+---------------------------------+
o n ep | tude
| s is S
4810369.6667
@
c i
+---------------------------------+
c th
1 row in set (#.##
ฺ r i sec) e
s input values leads to reduced accuracy in the result.
uthe
− Answer: Less
e r toaccuracy t o in
r
7. Use a suitable
i ( obMySQLnfunction
se to approximate the value of the average you calculated in
cc step,
the preceding
i e
lic to the nearest integer.
R
Enter the following statement at the mysql prompt and receive the results shown:
berto
Ro
mysql> SELECT ROUND((8510700 + 3787042 + 2133367)/3);
+---------------------------------------+
| ROUND((8510700 + 3787042 +2133367)/3) |
+---------------------------------------+
| 4810370 |
+---------------------------------------+
1 row in set (#.## sec)
8. Calculate the FLOOR() and CEILING() values of the gross national product averages
from the preceding step.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT FLOOR((8510700 + 3787042 + 2133367)/3);
+---------------------------------------+
| FLOOR((8510700 + 3787042 +2133367)/3) |
+---------------------------------------+
| 4810369 |
+---------------------------------------+
1 row in set (#.## sec)
national product values for the first 20 countries in the Country table. Exclude any
countries with NULL values in the GNP or GNPOld columns.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT Name, (GNP - GNPOld) AS GNPdiff
-> FROM world.Country
-> WHERE GNP IS NOT NULL
-> AND GNPOld IS NOT NULL
-> LIMIT 20;
ble
+----------------------+-----------+
fe r a
| Name | GNPdiff |
ans
+----------------------+-----------+
| Aruba | 35.00 | n - t r
o
| Angola
| Albania
| -1336.00 |
| 705.00 | s an
| United Arab Emirates | 1120.00 |
r ) ha ฺ
| Argentina | 16928.00 |
m ฺb uide
ฺco nt G
| Armenia | 186.00 |
| Antigua and Barbuda | 28.00 |
a r
| Australia
| Austria |
o ep tude
| -41729.00 |
n
5835.00 |
| Azerbaijan |
@ s is S
27.00 |
c c i th
| Burundi
r i | -79.00 |
e
| Belgium
| Benin
e r toฺ to us|
|
5756.00 |
216.00 |
i ( rob ense
| Burkina Faso | 224.00 |
c lic
| Bangladesh | 886.00 |
e r | Bahrain | 269.00 |
ob
| Bahamas | 180.00 |
R | Belize
| Bermuda
|
|
14.00 |
138.00 |
+----------------------+-----------+
20 rows in set (#.## sec)
10. Modify the statement in the preceding step to display the absolute value of the difference
between current and old gross national product values.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT Name, ABS((GNP - GNPOld)) AS GNPdiff
-> FROM world.Country
-> WHERE GNP IS NOT NULL
-> AND GNPOld IS NOT NULL
-> LIMIT 20;
+----------------------+----------+
| Name | GNPdiff |
+----------------------+----------+
| Aruba | 35.00 |
| Angola | 1336.00 |
| Albania | 705.00 |
| United Arab Emirates | 1120.00 |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
| Belgium | 5756.00 |
| Benin | 216.00 |
| Burkina Faso | 224.00 |
| Bangladesh | 886.00 |
| Bulgaria | 2009.00 |
| Bahrain | 269.00 |
| Bahamas | 180.00 |
| Belize | 14.00 |
| Bermuda | 138.00 |
+----------------------+----------+ ble
20 rows in set (#.## sec)
fe r a
11. Modify the statement in the preceding step to remove the decimal part of the difference in ans
gross national product values, without affecting the integer part. n - t r
a no
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT Name, TRUNCATE(ABS((GNP - GNPOld)),0) h a sAS GNPdiff
-> FROM world.Country
ฺ b r) deฺ
-> WHERE GNP IS NOT NULL
c o m Gui
-> AND GNPOld IS NOT NULL
-> LIMIT 20; p a rฺ ent
+----------------------+---------+
|s one s|Stud
i@ thi
| Name GNPdiff
r i c c
+----------------------+---------+
| se 35 |
| Aruba
r t o ฺ u
| Angola
b e e to || 1336 |
| Albania
i ( ro ArabenEmirates
s 705 |
c
| United
lic
| 1120 |
to Ri||c Argentina
Armenia
|
|
16928 |
186 |
e r
ob
| Antigua and Barbuda | 28 |
R | Australia
| Austria
|
|
41729 |
5835 |
| Azerbaijan | 27 |
| Burundi | 79 |
| Belgium | 5756 |
| Benin | 216 |
| Burkina Faso | 224 |
| Bangladesh | 886 |
| Bulgaria | 2009 |
| Bahrain | 269 |
| Bahamas | 180 |
| Belize | 14 |
| Bermuda | 138 |
+----------------------+---------+
20 rows in set (#.## sec)
12. Keep the Linux terminal window open and logged into the mysql client for the next
practice.
Assumptions
You have a Linux terminal prompt open and are logged into the mysql client from the previous
practice.
Duration
This practice should take you approximately ten minutes to complete.
ble
Tasks fe r a
1. Display the current date (default format) with the column title TODAY. ans
n - t r
2. Display the date that is 21 days from today. o
3. an
Show the current date/time, and the date/time in exactly five hours from now.
s
4. ha ฺ
Show the date and time in ten years from the current date and time. Use the default format
r )
to display the result.
m ฺb uide
5.
a r ฺco nt G
Display the current date and in the format “Today is {day} the {date} of {month}, {year}. For
example: “Today is Friday the 13th of November, 2015.”
6. o n ep tude
Exit the mysql client and close the Linux terminal window.
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
+---------------------+-------------------------------------+
| NOW() | Today |
+---------------------+-------------------------------------+
| 2016-05-16 14:34:21 | Today is Monday the 16 of May, 2016 |
+---------------------+-------------------------------------+
1 row in set (#.## sec)
6. Exit the mysql client and close the Linux terminal window.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 10:
Subqueriesom t Gu i
r ฺ c
a 10den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Questions
1. Where in a SQL statement can you place a scalar subquery?
2. The following query selects those continents that have countries in which more than 50% of
the population speak English. Is this an example of using a correlated subquery? Why or
why not?
ble
SELECT DISTINCT Continent FROM Country
fe r a
WHERE Code IN (
ans
SELECT CountryCode FROM CountryLanguage
WHERE Language='English' AND Percentage > 50
n - t r
);
a no
3. The following statement uses a correlated subquery to find the names
h a s of customers who
have made a payment of more than 11 currency units.
ฺ b r) deฺ
SELECT first_name, last_name FROM customer
c o m Gui
WHERE EXISTS(
p a rฺ ent
one s Stud
SELECT * FROM payment
WHERE amount > 11
s
);
c c i@ thi
AND customer_id = customer.customer_id
e r to
R ob
Overview
In this practice, you place subqueries in the SELECT column designation and in the FROM clause
of a statement.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Duration
This practice should take you approximately 10 minutes to complete.
Tasks
1. Log in to the mysql command-line client in a Linux terminal window.
2. In the sakila database, create and execute a correlated subquery that displays the name
and the number of cities of all countries beginning with the letter “V”. e
Hint: Use the city and country tables in the sakila database to achieve this. r a bl
s fe
3. Create a noncorrelated subquery that calculates the average total payments per customer.
- t r an
Hint: This information is available in the payment table of the sakila database. The total
on
an
payment per customer is an aggregate, and its average is another aggregate.
s
4.
ha ฺ
Keep the mysql command-line client open for the next practice.
r )
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Solution Steps
1. Log in to the mysql command-line client in a Linux terminal window.
Enter the following command at the Linux terminal prompt and receive the results shown:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
2. In the sakila database, create and execute a correlated subquery that displays the name
and the number of cities of all countries beginning with the letter “V”.
Hint: Use the city and country tables in the sakila database to achieve this.
ble
Enter the following commands at the Linux terminal prompt and receive the results shown:
fe r a
mysql> USE sakila
ans
Reading table information ...
n - t r
o
an
Database changed
| Vietnamb e r t o
( r o n s eU.S. || 6 |
c i
| Virgin
e
Islands,
c
1 |
e r toCreate a noncorrelated subquery that calculates the average total payments per customer.
R ob 3.
Hint: This information is available in the payment table of the sakila database. The total
payment per customer is an aggregate, and its average is another aggregate.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT AVG(totals) FROM (
-> SELECT SUM(amount) AS totals
-> FROM payment GROUP BY customer_id
-> ) AS temp;
+-------------+
| AVG(totals) |
+-------------+
| 112.548431 |
+-------------+
1 row in set (#.## sec)
4. Keep the mysql command-line client open for the next practice.
Overview
In this practice, you place subqueries in the WHERE clause.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Using the sakila database, perform a query by using a subquery that retrieves the
customer ID for all customers who have made a payment equal to the value of the largest
payment.
Hint: This information is available in the payment table. e
2. Perform a query by using a subquery that retrieves the first name and last name of all r a bl
customers who are marked as inactive, but have rented items. s fe
Hint: This information is available in the customer and rental tables. Inactive customers - t r an
on
an
have a zero value in the customer table’s active column.
3. Keep the mysql command-line client open for the next practice. as
r ) h ฺ
m b
ฺ uide
r o
ฺc nt G
a
ep tude
o n
s is S
c i @ th
r i c e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Solution Steps
1. Using the sakila database, perform a query by using a subquery that retrieves the
customer ID for all customers who have made a payment equal to the value of the largest
payment.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
erto
Enter the following statement at the mysql prompt and receive the results shown:
b
Ro
mysql> SELECT first_name, last_name
-> FROM customer WHERE active=0
-> AND customer_id IN (
-> SELECT customer_id FROM rental
-> );
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| SANDRA | MARTIN |
| JUDITH | COX |
| SHEILA | WELLS |
| ERICA | MATTHEWS |
| HEIDI | LARSON |
| PENNY | NEAL |
| KENNETH | GOODEN |
| HARRY | ARCE |
| NATHAN | RUNYON |
| THEODORE | CULP |
| MAURICE | CRAWLEY |
| BEN | EASTER |
| CHRISTIAN | JUNG |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Perform a query by using a subquery that retrieves the ID of every customer who has
returned more than five rentals within 24 hours.
Hint: This information is available in the rental table.
2. Without using any table joins, create a multilevel subquery that shows the first name and e
last name of each actor in the shortest films in the database.
r a bl
Hint: First, find the shortest film in the film table. Then find all films of that length, before s fe
using the actor and film_actor tables to identify the actors who acted in those films. - t r an
on
an
3. Exit the MySQL server and close the Linux terminal prompt.
s
ha ฺ
r )
ฺb uide
m
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT customer_id, COUNT(*)
-> FROM (
-> SELECT customer_id, rental_date, return_date
-> FROM rental
-> WHERE return_date < rental_date + INTERVAL 24 HOUR
-> ) AS t
-> GROUP BY customer_id
ble
-> HAVING COUNT(*) >= 5;
fe r a
+-------------+----------+
ans
| customer_id | count(*) |
+-------------+----------+
n - t r
| 28 | 5 |
a no
|
|
84 |
87 |
6 |
5 | h a s
| 131 | 5 |
ฺ b r) deฺ
| 167 | 6 |
c o m Gui
| 196 | 5 |
p a rฺ ent
one s Stud
| 228 | 5 |
| 374 | 6 |
s
|
|
565 |
592 |
5 |
c
5 |c i@ thi
o ฺ r i
+-------------+----------+
u se
e r t
10 rows in set (#.## sec) to
b
roany table e
c i (
2. Without using
c e nsjoins, create a multilevel subquery that shows the first name and
c of each
last iname
R li actor in the shortest films in the database.
erto
Hint: First, find the shortest film in the film table. Then find all films of that length, before
b using the actor and film_actor tables to identify the actors who acted in those films.
Ro Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT first_name, last_name
-> FROM actor WHERE actor_id IN (
-> SELECT DISTINCT actor_id
-> FROM film_actor WHERE film_id IN (
-> SELECT film_id FROM film
-> WHERE length = (
-> SELECT MIN(length)
-> FROM film
-> )
-> )
-> );
+------------+--------------+
| first_name | last_name |
+------------+--------------+
| JOHNNY | LOLLOBRIGIDA |
| HELEN | VOIGHT |
| BURT | DUKAKIS |
| TOM | MIRANDA |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
| KIRSTEN | AKROYD |
| SIDNEY | CROWE |
| GROUCHO | DUNST |
| RENEE | TRACY |
| JULIANNE | DENCH |
| FRANCES | TOMEI |
| WHOOPI | HURT |
| JADA | RYDER |
| ANGELA | WITHERSPOON |
| FAY | WOOD | ble
| CHRIS | DEPP |
fe r a
| HARVEY | HOPE |
ans
| HUMPHREY | WILLIS |
n - t r
| KENNETH | HOFFMAN | o
| MENA | HOPPER |
s an
| MICHAEL
| JULIA
| BOLGER
| FAWCETT
|
|
r ) ha ฺ
+------------+--------------+
m ฺb uide
28 rows in set (#.## sec)
a r ฺ co nt G
3. Exit the MySQL server and close the Linuxp
e
a. Enter the following at the mysqloclient: Stu
n de
terminal prompt.
@ s is
mysql> EXIT
c c i th
r i e
toฺ to us
Bye
#
b e r
rofollowing
b. Enter (the n e
scommand at the Linux terminal prompt
c i c e
R i#c exit li
berto
Ro
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 11:
Modifying om tTablei
u Data
r ฺ c
a 11den G
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take you approximately five minutes to complete.
Solution Steps
1. Log in to the mysql command-line client and create a new database called dbtest.
2. In the dbtest database, create a table called numbers by issuing the following statement:
CREATE TABLE numbers (n SMALLINT UNSIGNED);
ble
3. Insert the value 5 into the numbers table, and then query the table to confirm the insertion. fe r a
n s
n - t ra
4. Insert the value 5 + 20 into the table, and then query the table to confirm the insertion.
o insertion.
5. Insert the value 1.23e4 into the table, and then query the table to confirm the
n
6. Insert the value POW(5,5) into the table, and then query the table to a
s confirm the insertion.
h a
ฺ b r) dcommand-line
7. Keep the Linux terminal window open and connected to the mysql
e ฺ client for
the next practice.
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
e r to
R ob
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE DATABASE dbtest;
Query OK, 1 row affected (#.## sec)
ble
2. In the dbtest database, create a table called numbers by issuing the following statement:
fe r a
CREATE TABLE numbers (n SMALLINT UNSIGNED);
ans
n - t r
no
Enter the following statements at the mysql prompt and receive the results shown:
a
mysql> USE dbtest;
h a s
Database changed
ฺ b r) deฺ
Query OK, 0 rows affected (#.## sec) om
mysql> CREATE TABLE numbers (n SMALLINT UNSIGNED);
u i
a ฺ c
rthen n G
tthe table to confirm the insertion.
p
3. Insert the value 5 into the numbers table, and e
query
s
Enter the following statements at the
ne prompt
omysql S tudand receive the results shown:
c i @ VALUES
t h is (5);
ric us(#.##e sec)
mysql> INSERT INTO numbers
Query OK, 1 row ฺaffected
t o
er * FROM tonumbers;
mysql> o b e
c i (r cens
SELECT
i|c n | li
+------+
R
erto | 5 |
+------+
b
Ro +------+
1 row in set (#.## sec)
4. Insert the value 5 + 20 into the table, and then query the table to confirm the insertion.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> INSERT INTO numbers VALUES (5+20);
Query OK, 1 row affected (#.## sec)
+-------+
| n |
+-------+
| 5 |
| 25 |
| 12300 |
+-------+
3 rows in set (#.## sec)
6. Insert the value POW(5,5) into the table, and then query the table to confirm the insertion.
ble
Enter the following statements at the mysql prompt and receive the results shown:
fe r a
ans
mysql> INSERT INTO numbers VALUES (POW(5,5));
Query OK, 1 row affected (#.## sec)
n - t r
a no
mysql> SELECT * FROM numbers;
+-------+ h a s
| n |
ฺ b r) deฺ
+-------+
c o m Gui
|
|
5 |
25 | p a rฺ ent
| 12300 |
s one s Stud
i@ thi
| 3125 |
+-------+
i c c
o ฺ r
4 rows in set (#.## sec)
t u se
e r window
7. Keep the Linuxbterminal to open and connected to the mysql command-line client for
(ro cens
the next ipractice.
e
ic c li
R
berto
Ro
Duration
This practice should take you approximately five minutes to complete.
Tasks
1. In the sakila database, create a copy of the actor table called actor_copy.
2. Delete the actor with ID 140 from the actor_copy table, and then issue a suitable query
to confirm the deletion. e
3. Delete all actors with the last name “Kilmer” from the actor_copy table. r a bl
s fe
4. Using a single query on the actor_copy table, sort the actors by first name in reverse
alphabetical order and display the full names of the five actors whose first names appear at - t r an
the end of the alphabet. no n
s a
Hint: The first name of the final actor sorted alphabetically is “Zero.”
h a
5. r) deฺ
Delete the row in the actor_copy table containing the actor named “Zero” without
ฺ b
c o m Gui
specifying Zero’s first name, last name, or ID in the statement. Reissue the query in the
preceding step to confirm the deletion.
p a rฺ ent
6. Keep the Linux terminal window opens oand S tudto the mysql command-line client for
neconnected
Enter the following statements at the mysql prompt and receive the results shown:
Duration
This practice should take you approximately ten minutes to complete.
Tasks
1. Using the actor_copy table, change all occurrences of the first name “PENELOPE” to
“Penelope.” Issue a suitable query to verify the name change.
2. Use the LOWER() function to set all occurrences of the first name “KENNETH” to “kenneth”
for all actors. Issue a suitable query to verify the name change. ble
fe r a
3. Set the value of the SQL_SAFE_UPDATES variable to 1.
ans
Note: For beginners, a useful startup option is --safe-updates (or --i-am-a-dummy,
n - t r
no
which has the same effect). It is helpful for cases when you might have issued a DELETE
a
h a s
FROM tbl_name statement but forgotten the WHERE clause. Normally, such a statement
deletes all rows from the table. With --safe-updates, you can delete rows only by
ฺ b r) deฺ
specifying the key values that identify them. This helps to prevent accidental deletion of
data. c o m Gui
4. p a rฺ ent
Attempt to set all occurrences of the first name “JULIA” to “Julia” for all actors. Why did you
receive an error?
s one s Stud
5. i@ thi
Change the first name of the actor with ID 199 from “JULIA” to “Julia.” Issue a suitable
c c
r i se
query to verify the change. Why is there no error on this occasion?
o ฺ u
6. e r t to
Set the SQL_SAFE_UPDATES option to 0.
b e
c i (ro terminal
7. Keep the Linux
c e nswindow open and connected to the mysql command-line client for
c practice.li
the inext
R
berto
Ro
b e rt +----------+------------+-----------+---------------------+
R o |
|
69 | kenneth
88 | kenneth
| PALTROW
| PESCI
| 2013-08-22 10:07:49 |
| 2013-08-22 10:07:49 |
| 94 | kenneth | TORN | 2013-08-22 10:07:49 |
| 169 | kenneth | HOFFMAN | 2013-08-22 10:07:49 |
+----------+------------+-----------+---------------------+
4 rows in set (0.00 sec)
3. Set the value of the SQL_SAFE_UPDATES variable to 1.
Note: For beginners, a useful startup option is --safe-updates (or --i-am-a-dummy,
which has the same effect). It is helpful for cases when you might have issued a DELETE
FROM tbl_name statement but forgotten the WHERE clause. Normally, such a statement
deletes all rows from the table. With --safe-updates, you can delete rows only by
specifying the key values that identify them. This helps to prevent accidental deletion of
data.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SET SQL_SAFE_UPDATES=1;
Query OK, 0 rows affected (0.00 sec)
R ic
Query
li
0 rows
7. oKeep the Linux terminal window open and connected to the mysql command-line client for
bert the next practice.
Ro
Duration
This practice should take you approximately ten minutes to complete.
Tasks
1. Using a REPLACE statement, add the new actor “John Johnson” to the actor_copy table
with an ID of 250. Verify the addition with a suitable query.
2. Use a REPLACE statement to replace the actor added in the preceding step with one named
ble
“Pete Peterson.” Verify the change with a suitable query.
fe r a
3. Keep the Linux terminal window open and connected to the mysql command-line client for
ans
the next practice.
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT * FROM actor_copy WHERE actor_id > 200;
Empty set (0.00 sec)
b e e
i
mysql> ( roSELECTe * sFROM actor_copy WHERE actor_id > 200;
n
R cc lic
i+----------+------------+-----------+---------------------+
erto +----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update |
b
Ro | 250 | Pete | Peterson | 2016-05-16 14:52:32 |
+----------+------------+-----------+---------------------+
1 row in set (0.00 sec)
− In this instance, the row with this ID already exists, so REPLACE performs an
UPDATE. The update affects two rows. (The original is removed and the new row is
inserted.)
3. Keep the Linux terminal window open and connected to the mysql command-line client for
the next practice.
Duration
This practice should take you approximately 15 minutes to complete.
Tasks
1. In the dbtest database, create a table called current_users to represent users who are
currently logged in. Use the following CREATE TABLE statement:
ble
CREATE TABLE current_users (
fe r a
userid INT UNSIGNED,
ans
username VARCHAR(100),
login_time TIMESTAMP,
n - t r
visits INT UNSIGNED DEFAULT 1,
a no
);
PRIMARY KEY (userid)
h a s
2. Enter the following statement to simulate a user logging ฺin: b r) deฺ
c o m Gui
INSERT INTO current_users (userid, username)
p a rฺ ent
tud
VALUES (100, 'Tobias');
e the results.
onnote
3. Query the current_users table and s is S
4. Reissue the preceding INSERT c i @ t
statement hto simulate the user attempting to log in a second
time.
t o ฺ ric use
b e r INSERTtostatement so that it performs a REPLACE instead, and verify
5. Modify the preceding
(
that it works
i n se query.
rowith a esuitable
icc the userliclogging in again with the following SQL statement:
6. Simulate
R
berto INSERT INTO current_users (userid, username)
Ro
VALUES (100, 'Tobias')
ON DUPLICATE KEY UPDATE visits=visits+1;
7. Execute a suitable query to verify any differences in behavior between the preceding
INSERT statement and the earlier REPLACE statement.
8. Keep the Linux terminal window open and connected to the mysql command-line client for
the next practice.
− You cannot insert a second row with the same primary key value as before. This
supports the idea that a user cannot log in more than once.
5. Modify the preceding INSERT statement so that it performs a REPLACE instead, and verify
that it works with a suitable query.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> REPLACE INTO current_users (userid, username)
-> VALUES (100, 'Tobias');
Query OK, 2 rows affected (0.00 sec) ble
fe r a
mysql> SELECT * FROM current_users;
ans
+--------+----------+---------------------+--------+
n - t r
| userid | username | login_time | visits |
+--------+----------+---------------------+--------+ a no
| 100 | Tobias | 2016-05-16 14:54:39 | 1 |
h a s
+--------+----------+---------------------+--------+
ฺ b r) deฺ
1 row in set (0.00 sec)
o i
m is unchanged.
u
− The login_time field changes. Every other
a c
rฺ SQL
field
n t G
p e
tud
6. Simulate the user logging in again with the following statement:
INSERT INTO current_usersso
ne username)
(userid,S
VALUES (100, 'Tobias')
c i @ this
ON DUPLICATE KEYrUPDATE
o ฺ ic uvisits=visits+1;
se
Enter the followinge t
rstatementtoat the mysql prompt and receive the results shown:
b
roINSERTenINTOse current_users (userid, username)
i
mysql> (
R icc -> l ic (100, 'Tobias')
VALUES
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. In the sakila database, inspect the next AUTO_INCREMENT value for the actor_copy
table by issuing a suitable SHOW CREATE TABLE statement.
2. Empty the actor_copy table of all data with a TRUNCATE statement. Verify row removal
with a suitable query. e
3. Inspect the next value for the AUTO_INCREMENT column of the actor_copy table. r a bl
s fe
4. Insert a record into the actor_copy table, specifying a first_name of “Lars,” and a
last_name of “Larsson.” Leave the other values unspecified. Verify the addition with a - t r an
suitable query. no n
5. s a
Inspect the next value for the AUTO_INCREMENT column of the actor_copy table again.
h a
6. Drop the actor_copy table from the sakila database. br) ฺ
ฺ i d e
7. Keep the Linux terminal window open and connected
r ฺ c om t G u command-line client for
to the mysql
the next practice.
e p a den
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
Enter the following statements at the mysql prompt and receive the results shown:
mysql> USE sakila;
Reading table information ...
Database changed
i c ci SELECT
mysql> l i ce * FROM actor_copy;
t o R Empty set (0.00 sec)
r
R obe3. Inspect the next value for the AUTO_INCREMENT column of the actor_copy table.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SHOW CREATE TABLE actor_copy\G
*************************** 1. row ***************************
Table: actor_copy
Create Table: CREATE TABLE `actor_copy` (
`actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP,
PRIMARY KEY (`actor_id`),
KEY `idx_actor_last_name` (`last_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
− The AUTO_INCREMENT table option is not present in the output.
( r o n s e
KEY `idx_actor_last_name` (`last_name`)
c i in set
) ENGINE=InnoDB
crow l i ce(0.00 sec)
AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
R i1
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. In the sakila database, create a copy of the film table called film_copy.
2. Insert a new record into the film_copy table, with the title “MySQL Training” and a
language_id of 1. Leave the other fields unspecified.
3. Use a suitable function to inspect the AUTO_INCREMENT value generated for the film_id
ble
column.
fe r a
4. Issue a REPLACE statement to change the title of the newly added film to “MySQL for
ans
Developers.”
n - t r
5. Update the rental_duration of the newly added film to 4.
a no
6. a
Increase the replacement_cost of the newly added film by 20%.
h s
7. b r) deฺ
Verify that the preceding updates have succeeded by issuing a suitable query.
ฺ
8. Delete the newly added film. c o m Gui
9. Drop the film_copy table. p a rฺ ent
oneterminal d open for the next practice.
tuwindow
10. Exit the mysql client, but keep the Linux
s S
c i @ this
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
Ric
value. li it instead of SHOW CREATE TABLE.
e
4.
r toDevelopers.”
Issue a REPLACE statement to change the title of the newly added film to “MySQL for
R ob Enter the following statement at the mysql prompt and receive the results shown:
mysql> REPLACE INTO film_copy(film_id, title, language_id)
-> VALUES (1001, 'MySQL for Developers', 1);
Query OK, 2 rows affected (0.04 sec)
5. Update the rental_duration of the newly added film to 4.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> UPDATE film_copy SET rental_duration = 4
-> WHERE film_id = 1001;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6. Increase the replacement_cost of the newly added film by 20%.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> UPDATE film_copy
-> SET replacement_cost = replacement_cost * 1.2
-> WHERE film_id = 1001;
Query OK, 1 row affected, 1 warning (0.05 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
+-------+------+-------------------------------------------------------+
1 row in set (0.00 sec)
− Note the warning about data truncation. The calculated value contains more digits
than that the replacement_cost column can accommodate.
7. Verify that the preceding updates have succeeded by issuing a suitable query.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT * FROM film_copy WHERE film_id = 1001\G
*************************** 1. row ***************************
ble
film_id: 1001
fe r a
title: MySQL for Developers
description: NULL
ans
release_year: NULL
n - t r
language_id: 1
a no
original_language_id: NULL
rental_duration: 4
h a s
rental_rate: 4.99
ฺ b r) deฺ
length: NULL
c o m Gui
replacement_cost: 23.99
rating: G
p a rฺ ent
special_features: NULL
s one s Stud
last_update: 2013-08-22 10:33:28
1 row in set (0.00 sec)
c c i@ thi
8. Delete the newly added o ฺ i
rfilm. use
e t
rstatementtoat the mysql prompt and receive the results shown:
b
Enter the following
se film_copy WHERE film_id = 1001;
roDELETEenFROM
i (
R cc OK,li1crow affected (0.01 sec)
mysql>
iQuery
be9.rtoDrop the film_copy table.
Ro Enter the following statement at the mysql prompt and receive the results shown:
mysql> DROP TABLE film_copy;
Query OK, 0 rows affected (0.05 sec)
10. Exit the mysql client, but keep the Linux terminal window open for the next practice.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> EXIT
Bye
#
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 12:
om t Gu
Transactions i
r ฺ c
a 12den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Questions
1. What is a transaction?
2. What are the implications of disabling AUTOCOMMIT mode?
3. What does the acronym ACID stand for?
4. Name the four transaction isolation levels.
ble
5. Which three types of inconsistencies can occur when two simultaneous transactions access
fe r a
the same table?
ans
6. The Serializable locking level allows nonrepeatable reads.
n - t r
o
a. True
s an
b. False
r ) ha ฺ
7.
ฺb uide
Name the two locking modifiers that MySQL supports.
m
8.
a r ฺco nt G
Both locking types lock selected ___________ to prevent problems that might occur when
multiple transactions access the same table simultaneously.
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
transaction. You can explicitly commit or roll back a transaction by using the COMMIT and
ROLLBACK statements, respectively.
3. Atomic, Consistent, Isolated, Durable
4. Serializable, Repeatable Read, Read Committed, Read Uncommitted
5. Dirty reads, nonrepeatable reads, phantom reads
6. b. False. This isolation level specifies that all transactions occur in a completely isolated
fashion. It is as if all transactions execute serially, one after the other.
ble
7. LOCK IN SHARE MODE and FOR UPDATE.
fe r a
8. Both locking types lock selected rows to prevent problems that might occur when multiple ans
transactions access the same table simultaneously. n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. At a Linux terminal prompt, log in to the mysql client as root (password: oracle).
2. Using a SHOW statement, list all storage engines. Determine which engine is the default,
and which supports transactions.
3. Display the value of AUTOCOMMIT for the current session. What effect does the value have
ble
on SQL statements that modify table data?
fe r a
4. Identify the storage engines used by the city and film_text tables in the sakila
ans
database.
n - t r
5. no
Delete a record from the city table in the sakila database within an explicit transaction.
a
h a s
Confirm that it has been removed and then roll back the transaction. Verify that the record
is restored. Perform the following steps to achieve this:
ฺ b r) deฺ
• Explicitly start a new transaction.
c o m Gui
• Find the ID of the Canadian city of London.
p a rฺ ent
• Delete that city, and issue a SELECT
o neto ensure d removal.
tuits
• Roll back the transaction. @ s is S
c i
• Confirm that the rowriiscrestored
t h
t o ฺ u seto the table.
e
6. Delete a record from
b r the film_text
to table within an explicit transaction and confirm its
removal. Roll e
(robackcethensteps
s
transaction and take note of any differences from the preceding task.
Perform
c c i li
the following to achieve this:
R i
• Explicitly start a new transaction.
berto • Find the ID of the film_text row for the film titled “Rock Instinct.”
Ro • Delete the row for that film text, and issue a SELECT to confirm its removal.
• Roll back the transaction.
• Check if the row is restored, noting any differences from the preceding task.
7. Repeat the tasks in step 5 to delete the Canadian city of London; however, this time
COMMIT the statement, instead of rolling it back.
8. Keep the Linux terminal window open and connected to the mysql command-line client for
the next practice.
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2. Using a SHOW statement, list all storage engines. Determine which engine is the default,
and which supports transactions.
Enter the following statement at the mysql prompt, and receive the results shown:
mysql> SHOW ENGINES\G
ble
*************************** 1. row ***************************
fe r a
Engine: MyISAM
ans
Support: YES
Comment: MyISAM storage engine n - t r
o
Transactions: NO
XA: NO s an
Savepoints: NO
r ) ha ฺ
ฺb uide
*************************** 2. row ***************************
m
ฺco nt G
Engine: CSV
Support: YES
a r
Comment: CSV storage engine
o n ep tude
Transactions: NO
XA: NO
@ s is S
c c i th
Savepoints: NO
r i e
e r toฺ to us
*************************** 3. row ***************************
Engine: MRG_MYISAM
i ( rob ense
Support: YES
c lic
Comment: Collection of identical MyISAM tables
to RicTransactions: NO
e r XA: NO
ob
Savepoints: NO
R *************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it
disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign
keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 6. row ***************************
Engine: MEMORY
Support: YES
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL ble
XA: NULL
fe r a
Savepoints: NULL
ans
*************************** 9. row ***************************
n - t r
Engine: PERFORMANCE_SCHEMA
Support: YES
a no
Comment: Performance Schema
h a s
Transactions: NO
ฺ b r) deฺ
XA: NO
Savepoints: NO
c o m Gui
9 rows in set (#.## sec)
p a rฺ ent
3. Display the value of AUTOCOMMIT for the
s o necurrent
S ud What effect does the value have
tsession.
on SQL statements that modify@ table data?is
c c i th prompt, and receive the results shown:
r
Enter the following statement
ฺ i e
at the mysql
us
mysql> SELECT e r to@@AUTOCOMMIT;
t o
( rob en|se
+--------------+
i
R cc lic
| @@AUTOCOMMIT
i+--------------+
erto +--------------+
| 1 |
b
Ro 1 row in set (#.## sec)
− Answer: AUTOCOMMIT is enabled. This means that as soon as you submit any SQL
statements that modify table data, the changes are persisted and cannot be rolled
back. To be able to control whether changes are committed to the database or
rolled back, you must explicitly start a transaction.
4. Identify the storage engines used by the city and film_text tables in the sakila
database.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> USE sakila
Database changed
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
*************************** 2. row ***************************
Name: film_text
Engine: MyISAM
Version: 10 ble
Row_format: Dynamic
fe r a
Rows: 1000
ans
Avg_row_length: 119
n - t r
Data_length: 119616
Max_data_length: 281474976710655
a no
Index_length: 10240
h a s
Data_free: 0
ฺ b r) deฺ
Auto_increment: NULL
Create_time: 2015-11-10 15:12:20
c o m Gui
Update_time: 2015-11-10 15:12:20
p a rฺ ent
one s Stud
Check_time: NULL
s
Collation: utf8_general_ci
Checksum: NULL
c c i@ thi
Create_options:
o ฺ r i u se
r t
Comment:
e to
b e
2 rows in set (#.## sec)
i (ro The
− Answer:
c c e s table uses InnoDB, and film_text uses MyISAM.
ncity
R
5. Delete
i the city table in the sakila database within an explicit transaction.
ic a record lfrom
bertoConfirm that it has been removed and then roll back the transaction. Verify that the record
Ro
is restored. Perform the following steps to achieve this:
• Explicitly start a new transaction.
• Find the ID of the Canadian city of London.
• Delete that city, and issue a SELECT to ensure its removal.
• Roll back the transaction.
• Confirm that the row is restored to the table.
a. To explicitly start a transaction, enter the following statement at the mysql prompt and
receive the results shown:
mysql> START TRANSACTION;
Query OK, 0 rows affected (#.## sec)
b. To find the ID of the Canadian city London, enter the following statements at the
mysql prompt and receive the results shown:
mysql> SELECT country_id FROM country
-> WHERE country = 'Canada';
+------------+
| country_id |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
| 737 |
+---------+
1 row in set (#.## sec)
ble
mysql> ROLLBACK;
fe r a
Query OK, 0 rows affected, 1 warning (#.## sec)
ans
b. View the warning generated as a result of issuing ROLLBACK in the previous step by
n - t r
issuing the following command:
n o
mysql> SHOW WARNINGS\G s a
h a
r) deฺ
*************************** 1. row ***************************
Level: Warning
m ฺ b i
Code: 1196
ฺ c o G u
Message: Some non-transactional changed
a r n t
tables couldn't be rolled back
1 row in set (#.## sec)
o n ep tude
c. Enter the following statement to s see if ithe
s S
row has been restored:
i @
mysql> SELECT * FROMc film_text h
t WHERE film_id = 737;
r
Empty set (#.## ฺsec) i c s e
rtonot return
t o u
e
− The rowbdoes when you roll back the transaction. This is because
i ( ro eisnnot
film_text sea transactional table, because it is based on the MyISAM storage
R iccengine. lic
erto
7. Repeat the tasks in step 5 to delete the Canadian city of London; however, this time,
b COMMIT the statement, instead of rolling it back.
Ro a. Explicitly start a transaction with the following statement at the mysql prompt:
mysql> START TRANSACTION;
Query OK, 0 rows affected (#.## sec)
b. To delete the city with the ID 313 as previously seen, and verify its removal, issue the
following statements at the mysql prompt:
mysql> DELETE FROM city WHERE city_id = 313;
Query OK, 1 row affected (#.## sec)
Note: This practice uses two Linux terminal windows. The instructions refer to the terminal
windows as T1 and T2.
Duration
This practice should take you approximately 30 minutes to complete.
Tasks
ble
1. Open a second Linux terminal window and log in to the mysql command-line client.
fe r a
2. Change the mysql> prompt in both terminal windows so that you can easily identify each
ans
session. These instructions refer to the original terminal window as T1 and the second
n - t r
terminal window as T2.
a no
h a s
Note: If you are using MySQL Workbench for this practice, open a second SQL Editor tab
and perform the following steps to change the prompt.
ฺ b r) deฺ
c o m Gui
a. Change the mysql client prompt in the second terminal window to T2>:
mysql> PROMPT T2>
p a rฺ ent
one s Stud
PROMPT set to 'T2> '
T2>
s
b. Switch to the first terminal
i c c i@ t hi change the prompt to T1>:
window and
e
mysql> PROMPT o
r t ฺr
T1> us
PROMPT seteto 'T1> 'to
T1>
i ( rob ense
3. In T1,
R iccstart a newlictransaction.
be4.rtoIn T2, change the current database to sakila and start another transaction:
Ro 5.
6.
In T1, select all rows from the country table where the country_id is greater than 100.
In T2, insert a new row into the Country table for Belgium.
7. In T1, reissue the SELECT statement you issued in step 5. What do the results tell you
about the current isolation level?
8. In T2, commit the transaction.
9. In T1, reissue the previous SELECT statement you issued in step 7. What do the results tell
you about the current isolation level?
10. Roll back T1 and reissue the previous select statement. Is the Belgium row visible? Why?
11. Change the isolation level in T1 to READ COMMITTED.
12. Start new transactions in T1 and T2.
13. In T1, select all rows from the country table where the country_id is greater than 100.
14. In T2, insert a new row for the country Ghana.
15. In T1, reissue the query that selects all rows from the country table where the
country_id is greater than 100. Explain your results in the context of the current isolation
level.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
In the second Linux terminal window, enter the following statement at the mysql prompt
and receive the results shown:
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
...
2. Change the mysql> prompt in both terminal windows so that you can easily identify each
session. These instructions refer to the original terminal window as T1 and the second e
terminal window as T2.
r a bl
Note: If you are using MySQL Workbench for this practice, open a second SQL Editor tab s fe
and perform the following steps to change the prompt.
- t r an
a. Change the mysql client prompt in the second terminal window to T2>: no n
s a
mysql> PROMPT T2>
h a
PROMPT set to 'T2> '
ฺ b r) deฺ
T2>
o mprompt u i
b. Switch to the first terminal window and change
a r ฺ c the
n t Gto T1>:
mysql> PROMPT T1>
o n ep tude
PROMPT set to 'T1> '
@ s is S
T1>
c c i th
r i us e
Enter the followinge toฺ toat the
3. In T1, start a new transaction.
rstatement mysql prompt and receive the results shown:
r o b s e
c c e n
( TRANSACTION;
T1> i START
R i c
Query OK, l i
0 rows affected (#.## sec)
be4.rtoIn T2, change the current database to sakila and start another transaction:
Ro Enter the following statements at the mysql prompt and receive the results shown:
T2> USE sakila
Reading table information ...
Database changed
Enter the following statement at the mysql prompt in T2 and receive the results shown::
T2> INSERT INTO country (country) VALUES ('Belgium');
Query OK, 1 row affected (#.## sec)
7. In T1, reissue the SELECT statement you issued in step 5. What do the results tell you
about the current isolation level?
Enter the following statement at the T1 prompt and receive the results shown:
ble
T1> SELECT * FROM country WHERE country_id > 100;
+------------+----------------------+---------------------+
fe r a
| country_id | country | last_update |
ans
+------------+----------------------+---------------------+
n - t r
|
|
101 | United Arab Emirates | 2006-02-15 04:44:00 |
102 | United Kingdom | 2006-02-15 04:44:00 | a no
| 103 | United States | 2006-02-15 04:44:00 |
h a s
| 104 | Venezuela r) deฺ
| 2006-02-15 04:44:00 |
ฺ b
| 105 | Vietnam
m Gui
| 2006-02-15 04:44:00 |
c o
|
| 107 | Yemen
p rฺ ent
106 | Virgin Islands, U.S. | 2006-02-15 04:44:00 |
a
| 2006-02-15 04:44:00 |
one s Stud
| 108 | Yugoslavia | 2006-02-15 04:44:00 |
| 109 | Zambia s | 2006-02-15 04:44:00 |
c i@ thi
+------------+----------------------+---------------------+
c
o ฺ r i
9 rows in set (#.## sec)
u s e
− Answer: e t
r new rowtois not visible. You can tell that the isolation level is not “Read
The
r o b
Uncommitted,” se you cannot see the row you added at the T2 prompt in step
because
i ( e
c6, and neither n
8. Ric lic transaction has been committed.
In T2, commit the transaction.
e r toEnter the following statement at the mysql prompt in T2 and receive the results shown:
R ob T2> COMMIT;
Query OK, 0 rows affected (#.## sec)
9. In T1, reissue the previous SELECT statement you issued in step 7. What do the results tell
you about the current isolation level?
Enter the following statement at the mysql prompt in T1 and receive the results shown:
T1> SELECT * FROM country WHERE country_id > 100;
+------------+----------------------+---------------------+
| country_id | country | last_update |
+------------+----------------------+---------------------+
| 101 | United Arab Emirates | 2006-02-15 04:44:00 |
| 102 | United Kingdom | 2006-02-15 04:44:00 |
| 103 | United States | 2006-02-15 04:44:00 |
| 104 | Venezuela | 2006-02-15 04:44:00 |
| 105 | Vietnam | 2006-02-15 04:44:00 |
| 106 | Virgin Islands, U.S. | 2006-02-15 04:44:00 |
| 107 | Yemen | 2006-02-15 04:44:00 |
| 108 | Yugoslavia | 2006-02-15 04:44:00 |
| 109 | Zambia | 2006-02-15 04:44:00 |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
e11.
r toChange the isolation level in T1 to READ COMMITTED.
R ob Enter the following statements at the mysql prompt and receive the results shown:
T1> SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Query OK, 0 rows affected (#.## sec)
− Note: The SET TRANSACTION ISOLATION LEVEL statement affects the isolation
level only for the current transaction. To persist the change in isolation level, you
must run SET [scope] TRANSACTION LEVEL, where scope is either SESSION
or GLOBAL and affects the value of the tx_isolation variable for the scope.
12. Start new transactions in T1 and T2.
a. Enter the following statement at the mysql prompt in T1 and receive the results
shown:
T1> START TRANSACTION;
Query OK, 0 rows affected (#.## sec)
b. Enter the following statement at the mysql prompt in T2 and receive the results
shown:
T2> START TRANSACTION;
Query OK, 0 rows affected (#.## sec)
i|c ci l
101 ce| United Arab Emirates | 2006-02-15 04:44:00 |
+------------+----------------------+---------------------+
i
t o R| 102 | United Kingdom | 2006-02-15 04:44:00 |
Ro |
|
104 | Venezuela
105 | Vietnam
| 2006-02-15 04:44:00 |
| 2006-02-15 04:44:00 |
| 106 | Virgin Islands, U.S. | 2006-02-15 04:44:00 |
| 107 | Yemen | 2006-02-15 04:44:00 |
| 108 | Yugoslavia | 2006-02-15 04:44:00 |
| 109 | Zambia | 2006-02-15 04:44:00 |
| 110 | Belgium | 2016-05-18 10:29:29 |
+------------+----------------------+---------------------+
10 rows in set (#.## sec)
− Answer: The READ COMMITTED isolation level prevents you from seeing
uncommitted changes in T2.
16. Commit the transaction in T2.
Enter the following statement at the mysql prompt in T2 and receive the results shown:
T2> COMMIT;
Query OK, 0 rows affected (#.## sec)
to Ric|
|
110 | Belgium
111 | Ghana
| 2016-05-18 10:29:29 |
| 2016-05-18 10:32:52 |
e r
ob
| 112 | Jamaica | 2016-05-18 10:38:23 |
R +------------+----------------------+---------------------+
12 rows in set (#.## sec)
− Answer: The READ UNCOMMITTED isolation level causes the new row to appear in
T1 even before you commit the transaction in T2.
24. Commit the transaction in T2.
Enter the following statement at the mysql prompt in T2 and receive the results shown:
T2> COMMIT;
Query OK, 0 rows affected (#.## sec)
25. Roll back the transaction in T1.
Enter the following statement at the mysql prompt and receive the results shown:
T1> ROLLBACK;
Query OK, 0 rows affected (#.## sec)
+------------+----------------------+---------------------+
| 101 | United Arab Emirates | 2006-02-15 04:44:00 |
| 102 | United Kingdom | 2006-02-15 04:44:00 |
| 103 | United States | 2006-02-15 04:44:00 |
| 104 | Venezuela | 2006-02-15 04:44:00 |
| 105 | Vietnam | 2006-02-15 04:44:00 |
| 106 | Virgin Islands, U.S. | 2006-02-15 04:44:00 |
| 107 | Yemen | 2006-02-15 04:44:00 |
| 108 | Yugoslavia | 2006-02-15 04:44:00 |
ble
|
|
109 | Zambia
110 | Belgium
| 2006-02-15 04:44:00 |
| 2016-05-18 10:29:29 |
fe r a
| 111 | Ghana | 2016-05-18 10:32:52 |
ans
| 112 | Jamaica | 2016-05-18 10:38:23 |
n - t r
+------------+----------------------+---------------------+
12 rows in set (#.## sec) a no
a
− The new record has been permanently committed to thehdatabase
s and, therefore,
the ROLLBACK statement has no effect.
ฺ b r) deฺ
27. Revert T1 to the default prompt and exit the mysql o m session.
client u i
a c
rฺ ineT1nand G
t receive the results shown:
Enter the following statements at the mysqlp prompt
T1> PROMPT s one s Stud
Returning to default iPROMPT
c @ ofthimysql>
r i c se
mysql> EXIT toฺ u
b e r to
Bye
(ro cens e
#
c i
ic T2 to thelidefault prompt and exit the mysql client session.
28. Revert
R
e r toEnter the following statements at the mysql prompt in T2 and receive the results shown:
R ob T2> PROMPT
Returning to default PROMPT of mysql>
mysql> EXIT
Bye
#
29. Close the T1 and T2 terminal windows.
Enter the following command at the T1 and T2 Linux terminal prompts:
# exit
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 13:
Query m Gu
oOptimizationi
a r ฺ c n t
p e
one s Stud
Chapter 13
s
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• You have created the dbtest database as described in the practices for the lesson
titled “Modifying Table Data.”
• The employees2.sql database creation script is available in the
/stage/databases/employees_db directory.
• MySQL Enterprise Monitor is installed, configured, and bookmarked in your web
ble
browser as described in the practice titled “Course Environment Overview.”
fe r a
• The sakila sample database is installed.
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Questions
1. Under what circumstances can adding indexes to a table make table operations slower?
2. True or false: Indexing improves performance of SELECT queries only; it deteriorates
performance of queries that change data.
3. “For my purposes, it doesn’t matter whether a query returns a result within one second or e
one minute, so I do not care about indexing.” Do you agree or disagree with the preceding
r a bl
statement, and why? s fe
4. What are the main reasons not to index table columns?
- t r an
5. The following is an excerpt from the description of the film table: no n
s a
mysql> DESCRIBE film;
h a
r|) Null|deKey|
+-------------+-----------------------------------+-----+----+--------+
| Field | Type
ฺ b ฺ Default|
i
om t |GNOu | PRI| NULL |
+-------------+-----------------------------------+-----+----+--------+
| film_id | smallint(5) unsigned
r ฺ c
a den || NO
| title | varchar(255)
n e p u
| MUL| NULL |
| description | text
... o
s is S t YES | | NULL |
| rating
i @ t h
| enum('G','PG','PG-13','R','NC-17')|
c YES | | G |
...
ฺr i c e
t o us
+-------------+-----------------------------------+-----+----+--------+
r t o
e
robWould
− If the rating esituation
column had a PRIMARY KEY index based on it, how many rows would
(
iti have? e n s
the be different if it had a UNIQUE key instead?
c c l i c
6. Ri should you consider when making index prefix values as short as possible?
What
toWhat statement would create a 10-character prefix index on the description field of the
e r
7.
R ob film table?
8. How can the LIMIT clause help improve the performance of queries?
9. What can you do to speed up data insertion operations?
clause.
3. This statement disregards the fact that the server is not idle during the time it takes to
handle a query. If the statement takes one minute to execute, that means the server is busy
for that time, and is less able to handle other requests. Speeding up a query by using an
index improves the performance of other operations happening at the same time. For these
reasons, you must disagree with this statement.
4. The following are reasons not to use indexes:
ble
• Indexes consume extra disk space.
fe r a
• Indexes negatively affect the performance of data modification operations if:
ans
− The data modification statement does not have a WHERE clause
n - t r
no
− The data modification statement uses a WHERE clause, but there are other indexes
a
on the table
h a s
− The data modification statement uses a WHERE clause
ฺ b r) in thedeabsence
ฺ of any
indexes om t Gu i
r ฺ
5. The film table would have at most five rowsaif the rating
c n field was a PRIMARY KEY (one
e p d e
row per value in the enum). If the rating
s onin the
field
S
was
tu column.key,
a UNIQUE it could have the same
five rows, plus any rows with null values
i @ h i s rating
t indexes also allow nulls.
This is because the
rating column allows nulls,
ฺ r i ccand UNIQUE
s e
6. You must consider rhow to unique t o u prefix value is. The optimizer cannot efficiently use an
each
e
i ( rob idx_desc_prefix
index with a low degree
e n se
of uniqueness.
7. CREATE
i c c INDEX
l i c ON film(description(10));
t o R allows the server to terminate query processing earlier than it would otherwise and
8. LIMIT
b r
e the server returns less information over the network to the client.
Ro 9. Use multiple-row INSERT statements instead of several single-row INSERT statements.
Also, consider LOAD DATA INFILE, which is even faster than multiple-row INSERT
statements.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. At a Linux terminal prompt, connect to the MySQL server with the mysql command-line
client.
2. Create a new table called Indexed in the dbtest database with two columns:
- c1 of data type INT
ble
- c2 of data type VARCHAR (width of 50)
fe r a
Index both these columns with a single index.
ans
3. Show the CREATE TABLE statement for the Indexed table, and confirm the proper column n - t r
o
attributes and indexes (shown as keys).
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
# mysql -u root -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
2. Create a new table called Indexed in the dbtest database with two columns:
- c1 of data type INT
- c2 of data type VARCHAR (width of 50)
ble
Index both these columns with a single index.
fe r a
Enter the following statements at the mysql prompt, and receive the results shown: ans
n - t r
no
mysql> USE dbtest
Reading table information
s a
...
h a
Database changed
ฺ b r) deฺ
mysql> CREATE TABLE Indexed (
c o m Gui
-> c1 INT,
p a rฺ ent
->
->
c2 VARCHAR(50),
INDEX (c1, c2)
s o ne Stud
-> );
c i @ (#.##t h issec)
t o ฺ ric use
Query OK, 0 rows affected
3. Show the CREATE rTABLE statementtoas keys).
for the Indexed table, and confirm the proper column
b e e
c (ro cestatement
attributes and indexes
i following ns at the mysql prompt, and receive the results shown:
(shown
Entercthe
R i li
erto *************************** 1. row ***************************
mysql> SHOW CREATE TABLE Indexed\G
b
Ro Table: Indexed
Create Table: CREATE TABLE `Indexed` (
`c1` int(11) DEFAULT NULL,
`c2` varchar(50) DEFAULT NULL,
KEY `c1` (`c1`,`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (#.## sec)
− Note the KEY called c1, and the index that uses both columns.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Add a primary key to the Indexed table, in the c1 column.
2. Show the CREATE TABLE statement for the Indexed table, and confirm the creation of the
primary key.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Remove the primary key from the Indexed table.
2. Show the CREATE TABLE statement for the Indexed table, and confirm the removal of the
primary key.
3. Drop the dbtest database.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
i (
R icc lic
berto
Ro
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Using an EXPLAIN statement, display information from the optimizer about the query
execution plan for the following query of the sakila database:
SELECT * FROM actor WHERE first_name = 'Whoopi'
2. Add an index on the first_name column in the actor table. ble
3. Verify that the optimizer can use the new index by reissuing the previous EXPLAIN fe r a
statement. ans
n - t r
4.
no
Using an EXPLAIN statement, display information from the optimizer about the query
execution plan for the following statement: a
h a s
r) ondetheฺ actor table.
SELECT * FROM actor WHERE actor_id = 22
b
5. Use a SHOW… statement to display information about all ฺindexes
m customers i
ฺ c o G u
and year, then you would need to aggregate a r
6. If you wanted to find the total amounts paid by video
p ude
this
t
rental
n
information for each
for a particular month
query. If many users
n e t
o s Sthis could cause a performance impact.
were executing similar queries simultaneously,
s
Instead, create a summary table
i c c @
e hi
i called tpayment_summary to improve the performance of
r
oฺ to us AS
these queries by entering the following
tpayment_summary
statement:
b e r
se
CREATE TABLE
SELECTro
i ( n
customer_id,
e
R cc licamount FROM payment_date) yearmonth,
iEXTRACT(YEAR_MONTH
SUM(amount)
Enter the following statement at the mysql prompt, and receive the results shown:
mysql> EXPLAIN SELECT * FROM actor WHERE actor_id=22\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: actor
type: const
possible_keys: PRIMARY
key: PRIMARY
ble
key_len: 2
ref: const fe r a
rows: 1
ans
Extra: NULL
n - t r
1 row in set (#.## sec)
n o
s
− Note that the query has to read a single row, and that the optimizer a will use the
PRIMARY key. h a
5. Use a SHOW… statement to display information aboutm br) on
all ฺindexes i d etheฺ actor table.
ฺ c oand receiveG uthe results shown:
r
Enter the following statement at the mysql prompt,
a den t
mysql> SHOW INDEX FROM actor\Gep
s on1. rowS tu
***************************
Table: actor i@
c thi s ***************************
Non_unique: 0ric e
Key_name: r t ฺ
oPRIMARY
t o us
o b e 1 e
Seq_in_index:
i ( r
Column_name:
e n s
actor_id
c
ic Cardinality: c
li A200
Collation:
R
berto Sub_part: NULL
Ro Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: actor
Non_unique: 1
Key_name: idx_actor_last_name
Seq_in_index: 1
Column_name: last_name
Collation: A
Cardinality: 200
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (#.## sec)
6. If you wanted to find the total amounts paid by video rental customers for a particular month
and year, then you would need to aggregate this information for each query. If many users
ble
were executing similar queries simultaneously, this could cause a performance impact.
fe r a
Instead, create a summary table called payment_summary to improve the performance of
ans
these queries.
n - t r
no
Enter the following statement at the mysql prompt, and receive the results shown:
a
mysql> CREATE TABLE payment_summary AS
h a s
-> SELECT customer_id,
ฺ b )
ryearmonth,
e ฺ
-> EXTRACT(YEAR_MONTH FROM payment_date)
om t Gu i d
-> SUM(amount) amount
r ฺ
a denc
-> FROM payment
e p
-> GROUP BY 1,2
-> ORDER BY 2,1; s on s Stu
Query OK, 2466 rows affected
c i sec)
i@ 0 t(#.##
hWarnings:
ฺ r
Records: 2466 Duplicates: i c s e 0
t o u
r ym ontothe yearmonth column of payment_summary.
e
7. Create an index called
b
(
Enter thei following n se at the mysql prompt, and receive the results shown:
ro estatement
R cc CREATE
imysql> lic INDEX ym ON payment_summary(yearmonth);
e r to Query OK, 0 rows affected (#.## sec)
| yearmonth | SUM(amount) |
+-----------+-------------+
| 200602 | 514.18 |
+-----------+-------------+
1 row in set (#.## sec)
10. Query the payment_summary table to find the largest total amount spent by a single
customer.
Enter the following statement at the mysql prompt, and receive the results shown:
ble
mysql> SELECT customer_id, sum(amount) FROM payment_summary
fe r a
-> GROUP BY 1 ORDER BY 2 DESC LIMIT 1;
ans
+-------------+-------------+
n - t r
no
| customer_id | sum(amount) |
+-------------+-------------+
s a
| 526 | 221.55 |
h a
+-------------+-------------+
1 row in set (#.## sec)
ฺ b r) deฺ
o m window u i open for the next
11. Exit the mysql client program and keep the Linux
a cterminal
rฺ ent G
practice. p
Enter the following at the mysql prompt
s S tudthe message shown:
oneandsreceive
mysql> EXIT
c c i@ thi
Bye
o ฺ r i u se
#
e r t to
b e
c i (ro cens
R ic li
berto
Ro
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Execute the following commands at a Linux terminal prompt to install the employees
example database:
ble
# cd /stage/databases/employees_db
fe r a
# mysql -uroot -poracle -t < employees2.sql
ans
2. Check the status of the MySQL Enterprise Monitor service and start it if necessary.
n - t r
o
an
Enter the following commands at the Linux terminal prompt and receive the results shown:
s
# service mysql-monitor-server status
r ) ha ฺ
MySQL Enterprise MySQL is not running
m ฺb uide
MySQL Enterprise Tomcat is not running
a r ฺco nt G
ep tude
# service mysql-monitor-server start
Starting mysql service [ OK ] o n
s is S
i @ th
160518 11:49:02 mysqld_safe Logging to
c
r i c e
toฺ to us
'/opt/mysql/enterprise/monitor/mysql/runtime/mysqld.log'.
e r
160518 11:49:02 mysqld_safe Starting mysqld daemon with
i ( rob ense
databases from /opt/mysql/enterprise/monitor/mysql/data/
c lic
Ric
Starting tomcat service [ OK ]
e
3.
r to Open the Firefox web browser and navigate to https://localhost:18443.
R ob Note: Alternatively, you can use the bookmark that you created in the “Course Environment
Overview” activity in the lesson titled “Introduction to MySQL.”
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i
oฺ to us e
e r t
5. b instances”
Select the “MySQL
r o s e option from the Dashboard drop-down list.
ci ( ce n
R i c l i
e r to
R ob
6. Add the local MySQL server running on port 3306 to the list of MySQL instances:
a. Click the “Add MySQL Instance” button.
b. In the dialog box that appears, enter the following information:
− Instance address: localhost
− Port: 3306
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
10. Change the refresh e t
rinterval intothe top-right corner of the page to “Every 10 Seconds.”
b
ro thaneone e
sinstance
c i (
11. If there is more
c n in the list of running instances on the left of the page,
R i
ensure c that you l i
select the local MySQL server instance running on port 3306.
erto
12. Execute the following query against the employees database by using the mysql
b command-line client:
Ro SELECT * FROM employees, salaries
WHERE employees.emp_no = salaries.emp_no
ORDER BY salary DESC LIMIT 10
Note how the query takes several seconds to execute.
ble
fe r a
ans
14. Click the query in the grid to display detailed information about the query. n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Note the execution time, the row statistics, the execution summary and when the query was
first seen. Query Analyzer has determined that the query requires a full table scan, which
frequently results in poor performance.
15. Close the detailed query information dialog box by clicking the Close button.
16. Use the mysql command-line client to execute an EXPLAIN statement to verify that this
query requires a full table scan.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> EXPLAIN SELECT * FROM employees, salaries
-> WHERE employees.emp_no = salaries.emp_no
-> ORDER BY salary DESC LIMIT 10\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
rows: 299600
filtered: 100.00
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: salaries
partitions: NULL
type: ref
possible_keys: PRIMARY,emp_no ble
key: PRIMARY
fe r a
key_len: 4
ans
ref: employees.employees.emp_no
n - t r
rows: 9
filtered: 100.00
a no
Extra: NULL
h a s
The EXPLAIN statement shows that MySQL performs a full
ฺ b ) scan
rtable e ฺon the employees
table which examines nearly 300,000 rows. It joins o m rowG
each of u i d
the employees table to the
salaries table and examines nine rows for a r ฺcjoinednrow.
each t Therefore in total, this query
examines over one million rows each time
o n t u de The ORDER BY clause also results
eitpis executed.
in the creation of a temporary tablesand the use
i s Sof a filesort operation, which both
increase the amount of file I/O. c i @
If many t h
users execute this query repeatedly it will result in a
r i c e
significant performance
r t oฺimpact.
t o us
e
If you create anbindex on e
( ro theenumber
index to ireduce n s theofsalaries table’s salary column, the query can use the
operations required by the ORDER BY and LIMIT clauses.
17. Add
R icancindex tolthe
ic salaries table’s salary column by executing the following SQL at
bertothe mysql prompt:
Ro ALTER TABLE salaries ADD INDEX(salary)
The MySQL server takes several seconds to generate the index.
18. Modify the EXPLAIN statement you created in step 9 to force the MySQL server to use the
index on the salaries table’s salary column and compare the results with the previous
output:
mysql> EXPLAIN SELECT * FROM employees,
-> salaries FORCE INDEX(salary)
-> WHERE employees.emp_no = salaries.emp_no
-> ORDER BY salary DESC LIMIT 10;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: salaries
partitions: NULL
type: index
possible_keys: NULL
key: salary
key_len: 4
ref: NULL
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
partitions: NULL
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: employees.salaries.emp_no
rows: 1
filtered: 100.00
Extra: NULL
2 rows in set, 1 warning (#.## sec) ble
Note how the use of the index drastically reduces the number of rows that MySQL mustsfe
r a
examine to execute the query and avoids the use of a temporary table and filesort.
- t r an
19. Execute the new query and examine the QRTi column entry for the query inoMySQL
n n
Enterprise Monitor Query Analyzer. The Query Response Time Index a is now acceptable:
a
h ฺs
b r )
ฺ uide
o m
a r ฺc nt G
20. Click the “monitormanager” button ato n
the
de and select “Log Out.”
etopp of thetupage
s allisrunningS
21. Drop the employees database
c i @
and exitth mysql sessions.
i c
ฺr at the
Enter the following command e
sLinux terminal prompt and receive the results shown:
r t o o u
be DATABASEt employees;
mysql> DROP
i o
Query(rOK,
e
6 rowsn seaffected (#.## sec)
R cc EXIT
imysql>
Bye
lic
b rtoStop the MySQL Enterprise Monitor service.
e22.
Ro Enter the following command at the Linux terminal prompt and receive the results shown:
# service mysql-monitor-server stop
Stopping tomcat service . [ OK ]
Stopping mysql service 160518 12:33:04 mysqld_safe mysqld from pid
file /opt/mysql/enterprise/monitor/mysql/runtime/mysqld.pid ended
. [ OK ]
#
23. Close any open Linux terminal windows:
# exit
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 14:
Stored om
Routines u i
r ฺ c
a 14den t G
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Log in to the mysql command-line client and create a new database called
storedroutines.
2. Create a stored procedure in the storedroutines database called hello. The hello
stored procedure should accept no parameter arguments and simply display the message
“Hello world!” when executed. ble
fe r a
3. Execute the stored procedure hello.
ans
4. Create a stored function in the storedroutines database called hello. The hello
n - t r
o
an
stored function must return the string “Hello,” followed by the text value contained within the
input parameter. Use the CHAR(20) data type for the input parameter and CHAR(50)
s
for the return value.
r ) ha ฺ
5. ฺb uide
Invoke the hello stored function by issuing a suitable SELECT statement.
m
6. ฺconext npractice.
Leave the mysql command-line client open forrthe
a t G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
r us
toฺ tohello.
3. Execute the storede procedure
( rob estatement
Enter the following
i n se at the mysql prompt and receive the results shown:
R cc CALL
imysql> lichello();
erto | Hello world! |
+--------------+
b
Ro +--------------+
| Hello world! |
+--------------+
1 row in set (#.## sec)
| Hello, world! |
+----------------+
1 row in set (#.## sec)
6. Leave the mysql command-line client open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take approximately ten minutes to complete.
Tasks
1. Change the current database to sakila and create the film_info stored procedure with
the following code:
ble
DELIMITER //
fe r a
CREATE PROCEDURE film_info(IN f_id int)
BEGIN
ans
SELECT title FROM film
n - t r
WHERE film_id = f_id;
a no
SELECT first_name, last_name FROM actor
WHERE actor_id IN (
h a s
SELECT actor_id FROM film_actor
ฺ b r) deฺ
WHERE film_id = f_id);
c o m Gui
SELECT name FROM category
WHERE category_id IN(
p a rฺ ent
WHERE film_id = f_id); s one s Stud
SELECT category_id FROM film_category
END//
c c i@ thi
DELIMITER ;
o ฺ r i u se
t
r thetostored procedure film_info. This procedure takes a film ID
− This codeecreates
b se and returns the name of the film, the actors appearing in the
roinput parameter
as (an
i
cfilm, andlithee n
c film category.
2.
to Ric
Invoke the film_info stored procedure with a suitable CALL statement.
e r
R ob 3. Leave the mysql command-line client open for the next practice.
mysql> DELIMITER //
mysql> CREATE PROCEDURE film_info(IN f_id int)
-> BEGIN
-> SELECT title FROM film
-> WHERE film_id = f_id; ble
-> SELECT first_name, last_name FROM actor
fe r a
-> WHERE actor_id IN (
ans
-> SELECT actor_id FROM film_actor
n - t r
->
->
WHERE film_id = f_id);
SELECT name FROM category a no
-> WHERE category_id IN(
h a s
-> SELECT category_id FROM film_category
ฺ b r) deฺ
->
-> END//
WHERE film_id = f_id);
c o m Gui
Query OK, 0 rows affected (#.## sec)
p a rฺ ent
mysql> DELIMITER ;
s one s Stud
c
− This procedure is a compound
i c e t hi
i@ statement, which contains three queries and two
subqueries. When
t o ฺ r you
u s
invoke the procedure, you get three resultsets. The
DELIMITER
b e r statement t o changes the delimiter so that you can use semicolons in the
(ro PROCEDURE
CREATE e
ns statement without terminating the statement after each query.
c i li c e
R ic the film_info
2. Invoke stored procedure with a suitable CALL statement.
bertoEnter the following statement at the mysql prompt and receive the results shown:
Ro mysql> CALL film_info(51);
+------------------+
| title |
+------------------+
| BALLOON HOMEWARD |
+------------------+
1 row in set (#.## sec)
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| PARKER | GOLDBERG |
| MENA | TEMPLE |
| GARY | PENN |
| RICHARD | PENN |
| RENEE | BALL |
| ROCK | DUKAKIS |
+------------+-----------+
6 rows in set (#.## sec)
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Create a stored function called actor_name in the sakila database with the following
code:
CREATE FUNCTION actor_name(a_id int)
RETURNS varchar(91)
BEGIN ble
DECLARE f, l varchar(45);
fe r a
SELECT first_name, last_name INTO f, l
ans
FROM actor
n - t r
WHERE actor_id = a_id;
RETURN CONCAT(f, ' ', l); a no
END
h a s
ฺ b r) namedeofฺthat actor.
− Given an actor’s ID, the stored function returns the full
2. Invoke the actor_name stored function with a suitable o mSELECT u i
statement.
3. In the storedroutines database, create a a
c
rฺ encalled
procedure
G
t select_params that
p
necode S d
displays three values passed into it. The
s o tufollows:
is as
i
CREATE PROCEDURE select_params
c @ INT,t h isINOUT param3 INT)
(IN param1 INT, OUT
ฺ
SELECT param1,oparam2,
t ric uparam3;
se
param2
b e r different
to user variables by entering the following statement:
4. Assign values to three
(ro = n100, s e
c
SETc i @value1ce
l i @value2 = 200, @value3 = 300;
i
R the select_params stored procedure by using the three variables assigned.
5. oCall
e r t
b 6. In the storedroutines database, create a procedure called change_params with the
Ro following code:
CREATE PROCEDURE change_params
(IN param1 INT, OUT param2 INT, INOUT param3 INT)
SET param1 = 1 , param2 = 2, param3 = 3;
7. Execute change_params, passing in the variables declared in step 4 as parameters:
CALL change_params(@value1,@value2, @value3);
8. Inspect the values that have been assigned to the three variables by the change_params
stored procedure, and explain the result:
SELECT @value1, @value2, @value3;
9. Leave the mysql command-line client open for the next practice.
e r t o
b +---------+---------+---------+
Ro | 100 | 2 | 3 |
+---------+---------+---------+
1 row in set (#.## sec)
− Answer: The change_params stored procedure affects the OUT parameter
argument @value2 and the INOUT parameter argument @value3, but does not
change the IN parameter argument @value1.
9. Leave the mysql command-line client open for the next practice.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Execute SHOW CREATE PROCEDURE for the select_params procedure created in the
preceding practice.
2. Execute SHOW CREATE FUNCTION for the hello function created in the first practice.
3. Issue a statement that displays the status of all stored procedures in the sakila database.
bl e
4. Issue a statement that displays the status of all stored functions in the sakila database. ra
5. Query the INFORMATION_SCHEMA database for information about all stored routines n
ins fe
the
sakila database. n - tra
Leave the mysql command-line client open for the next practice. a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
ฺ r i
sql_mode: cc se t
r to to u
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER
e
rob eFunction:
se CREATE DEFINER=`root`@`localhost` FUNCTION
ROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
i (
Create n
R cc
i`hello`(inputlic CHAR(20)) RETURNS char(50) CHARSET latin1
to character_set_client: utf8
RETURN CONCAT('Hello, ', input, '!')
e r
R ob collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (#.## sec)
3. Issue a statement that displays the status of all stored procedures in the sakila database.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SHOW PROCEDURE STATUS WHERE Db='sakila'\G
*************************** 1. row ***************************
Db: sakila
Name: film_info
Type: PROCEDURE
Definer: root@localhost
Modified: 2016-05-18 15:36:24
Created: 2016-05-18 15:36:24
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 3. row ***************************
Db: sakila
Name: film_not_in_stock
Type: PROCEDURE
Definer: root@localhost
Modified: 2016-05-10 13:11:36 ble
Created: 2016-05-10 13:11:36
fe r a
Security_type: DEFINER
ans
Comment:
n - t r
character_set_client: utf8 o
collation_connection: utf8_general_ci
s an
Database Collation: latin1_swedish_ci
r ) ha ฺ
*************************** 4. row ***************************
Db: sakila
m ฺb uide
Name: rewards_report
Type: PROCEDURE
a r ฺco nt G
n ep tude
Definer: root@localhost
o
s is S
Modified: 2016-05-10 13:11:36
@
i th
Created: 2016-05-10 13:11:36
c c
r i e
toฺ to us
Security_type: DEFINER
rob ense
character_set_client: utf8
c i (
collation_connection: utf8_general_ci
Ric lic
Database Collation: latin1_swedish_ci
4 rows in set (#.## sec)
e r
4.
toIssue a statement that displays the status of all stored functions in the sakila database.
R ob Enter the following statement at the mysql prompt and receive the results shown:
mysql> SHOW FUNCTION STATUS WHERE Db='sakila'\G
*************************** 1. row ***************************
Db: sakila
Name: actor_name
Type: FUNCTION
Definer: root@localhost
Modified: 2016-05-18 15:37:41
Created: 2016-05-18 15:37:41
Security_type: DEFINER
Comment:
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
Db: sakila
Name: get_customer_balance
Type: FUNCTION
Definer: root@localhost
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Connect to the storedroutines database.
2. Attempt to drop a nonexistent stored function called no_such_function.
3. Repeat the previous step using the IF EXISTS clause. Note any differences between the
outputs of the preceding steps.
ble
4. Display any warnings associated with the preceding task.
fe r a
5. Display the code for the hello stored function.
ans
6. Drop the hello function. Note any differences from earlier attempts to drop stored n - t r
functions in this practice. a no
7. Re-create the hello function with the following code: h a s
ฺ b r) deฺ
m Gui
CREATE FUNCTION hello (input CHAR(20))
RETURNS CHAR(100)
ฺ
RETURN CONCAT('Hello, ', input, '! rWelcomec o t MySQL training.');
p a d e n to
oneSELECT
8. Invoke the hello function with a suitable
s S tustatement.
c i @ this
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
| Note o
i ( r | b1305e|nsFUNCTION
e storedroutines.no_such_function does not exist |
i1ccrow in set lic (#.## sec)
+-------+------+---------------------------------------------------------+
R
be5.rtoDisplay the code for the hello stored function.
Ro Enter the following statement at the mysql prompt and receive the results:
mysql> SHOW CREATE FUNCTION hello\G
*************************** 1. row ***************************
Function: hello
sql_mode:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER
ROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION
`hello`(input CHAR(20)) RETURNS char(50) CHARSET latin1
RETURN CONCAT('Hello, ', input, '!')
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
1 row in set (#.## sec)
− The output displays the function name, the SQL modes that are used, and the code
used to create the function.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Create the docase function in the storedroutines database by using the following SQL
code:
CREATE FUNCTION docase (1st_Num DOUBLE(10,2),
2nd_Num DOUBLE(10,2), formula_type CHAR(15))
ble
RETURNS CHAR(30)
BEGIN fe r a
CASE formula_type
ans
WHEN 'Multiplication' THEN
n - t r
o
an
RETURN CONCAT(1st_Num,' * ',2nd_Num,' = ',
1st_Num * 2nd_Num);
WHEN 'Division' THEN s
ha ฺ
r )
ฺb uide
RETURN CONCAT(1st_Num,' / ',2nd_Num,' = ',
m
1st_Num / 2nd_Num);
WHEN 'Addition' THEN
a r ฺco nt G
ep tude
RETURN CONCAT(1st_Num,' + ',2nd_Num,' = ',
e r
2. toTest the docase stored function with a suitable SELECT statement, performing an addition.
R ob 3. Test the docase stored function with a suitable SELECT statement, performing a division.
4. Test the docase stored function with a statement that attempts to perform an operation
called “invalid,” noting any issues.
5. Create the dorepeat stored procedure in the storedroutines database.
6. Test the dorepeat stored procedure with a suitable CALL statement.
7. Create the dowhile stored procedure in the storedroutines database.
8. Test the dowhile stored procedure with a suitable CALL statement. Are the outputs from
dorepeat and dowhile different? If so, why?
9. Create the doloopif stored procedure in the storedroutines database.
10. Test the doloopif stored procedure with a suitable CALL statement.
11. Leave the mysql command-line client open for the next practice.
mysql> DELIMITER //
mysql> CREATE FUNCTION docase (1st_Num DOUBLE(10,2),
-> 2nd_Num DOUBLE(10,2), formula_type CHAR(15))
-> RETURNS CHAR(30)
-> BEGIN
-> CASE formula_type
-> WHEN 'Multiplication' THEN
-> RETURN CONCAT(1st_Num,' * ',2nd_Num,' = ',
-> 1st_Num * 2nd_Num);
-> WHEN 'Division' THEN ble
-> RETURN CONCAT(1st_Num,' / ',2nd_Num,' = ',
fe r a
-> 1st_Num / 2nd_Num);
ans
-> WHEN 'Addition' THEN
n - t r
->
->
RETURN CONCAT(1st_Num,' + ',2nd_Num,' = ',
1st_Num + 2nd_Num); a no
-> WHEN 'Subtraction' THEN
h a s
-> RETURN CONCAT(1st_Num,' - ',2nd_Num,' = ',
ฺ b r) deฺ
->
-> ELSE
c o m Gui
1st_Num - 2nd_Num);
o ฺ i
r; use
mysql> DELIMITER
e t
r demonstratesto the use of the CASE flow control statement. The function
b
− This function e
c i (ro thecefirstnstwo parameters to be of data type DOUBLE(10,2). If you provide a
expects
i
Ric decimal lnumber for either of the first two parameters, the function truncates the
e r to value to two digits after the decimal point. The third parameter is the type of
Ro
-> SELECT CONCAT('The x variable is ', var_x)
-> AS Repeat_Counter;
-> UNTIL var_x > p1
-> END REPEAT;
-> SELECT CONCAT('The final REPEAT number is ',var_x)
-> AS Repeat_Results;
-> END//
Query OK, 0 rows affected (#.## sec)
mysql> DELIMITER ;
− This procedure demonstrates the use of the REPEAT flow control statement. The
procedure accepts a single integer parameter. The procedure counts from 0,
incrementing by 1, until it reaches the value of its parameter. The procedure then
displays the final value and exits.
6. Test the dorepeat stored procedure with a suitable CALL statement.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CALL dorepeat (10);
+---------------------+
+---------------------+
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
| Repeat_Counter |
+---------------------+
| The x variable is 2 |
+---------------------+
1 row in set (#.## sec)
...
+----------------------+
| Repeat_Counter |
+----------------------+ ble
| The x variable is 11 |
fe r a
+----------------------+
ans
1 row in set (#.## sec)
n - t r
+-------------------------------+
a no
| Repeat_Results |
h a s
+-------------------------------+
ฺ b r) deฺ
| The final REPEAT number is 11 |
+-------------------------------+
c o m Gui
1 row in set (#.## sec)
p a rฺ ent
Query OK, 0 rows affected o
s ne sec)
(#.## S tud
− The final value is 11.ci@ t h is
7. Create the dowhile o
t ฺ ric procedure
stored u se in the storedroutines database.
b e rstatementstoat the mysql prompt and receive the results shown:
Enter the following
i ( roDELIMITER
e n se//
R cc CREATE
mysql>
imysql> lic PROCEDURE dowhile (p1 INT)
erto
-> BEGIN
b -> DECLARE var_x INT;
Ro ->
->
SET var_x = 0;
WHILE var_x < p1 DO
-> SET var_x = var_x + 1;
-> SELECT CONCAT('The x variable is', var_x)
-> AS While_Counter;
-> END WHILE;
-> SELECT CONCAT('The final WHILE number is ', var_x)
-> AS While_Results;
-> END//
Query OK, 0 rows affected (#.## sec)
mysql> DELIMITER ;
8. Test the dowhile stored procedure with a suitable CALL statement. Are the outputs from
dorepeat and dowhile different? If so, why?
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CALL dowhile (10);
+---------------------+
| While_Counter |
+---------------------+
| While_Counter |
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
+---------------------+
| The x variable is 2 |
+---------------------+
1 row in set (#.## sec)
...
+------------------------------+
| While_Results |
+------------------------------+
| The final WHILE number is 10 | ble
+------------------------------+
fe r a
1 row in set (#.## sec)
ans
n - t r
Query OK, 0 rows affected (#.## sec)
a no
− The final value is 10, because the WHILE loop checks the condition before entering
h a s
the loop, but the REPEAT loop checks the condition after performing the loop code,
incrementing the value of x one extra time. ฺ b r) deฺ
9. c o m Gui
Create the doloopif stored procedure in the storedroutines database:
Enter the following statements at the mysql p a rฺ and
prompt e n t the results shown:
receive
mysql> DELIMITER // s one s Stud
mysql> CREATE PROCEDURE
i c c e t hi (p1 INT)
i@doloopif
-> BEGIN
r t o ฺr INT; us
-> DECLARE
e var_x
b var_xse=0; t o
(
i -> IFo
-> SET
r n LOOP
c
i ->c
->
l i e
loop_test:
c var_x < p1 THEN
be r -> ELSE
Ro ->
->
LEAVE loop_test;
END IF;
-> END LOOP loop_test;
-> SELECT CONCAT('The final LOOP and IF number is ',
-> var_x) AS Results;
-> END//
Query OK, 0 rows affected (#.## sec)
mysql> DELIMITER ;
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Overview
In this practice, you work with conditions and handlers.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Duration
This practice should take approximately ten minutes to complete.
Tasks
1. Create a table called handler_table in the storedroutines database with the
following statement:
CREATE TABLE handler_table (s1 int, primary key (s1));
ble
2. Create the dohandler stored procedure and explain how it works: fe r a
ans
CREATE PROCEDURE dohandler ()
n - t r
no
BEGIN
DECLARE dup_keys CONDITION FOR SQLSTATE '23000';
s a
DECLARE CONTINUE HANDLER FOR dup_keys SET @garbage = 1;
h a
SET @x = 1;
INSERT INTO storedroutines.handler_table VALUES (1);
ฺ b r) deฺ
SET @x = 2;
c o m Gui
a rฺ ent
INSERT INTO storedroutines.handler_table VALUES (1);
p
one s Stud
SET @x = 3;
END
s
3. Test the dohandler stored c
i c i@ with
procedure
e t hi a suitable CALL statement.
4. Check the value of the
r t o ฺr user variable
us assigned in the execution of the dohandler stored
e
@x
t o
procedure.
5. Checkc i
the
robof ethen@garbage
(value se
i c l i c procedure. user variable assigned in the execution of the
t o R
dohandler stored
be r
6. Keep the mysql command-line client open for the next practice.
Ro
Solution Steps
1. Create a table called handler_table in the storedroutines database with the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
following statement:
CREATE TABLE handler_table (s1 int, primary key (s1));
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE TABLE handler_table (s1 int, primary key (s1));
Query OK, 0 rows affected (#.## sec)
− The table contains a single column, s1, defined as a primary key.
2. Create the dohandler stored procedure and explain how it works.
ble
Enter the following statements at the mysql prompt and receive the results shown:
fe r a
mysql> DELIMITER //
ans
mysql> CREATE PROCEDURE dohandler ()
n - t r
-> BEGIN
-> DECLARE dup_keys CONDITION FOR SQLSTATE '23000'; a no
->
h a s
DECLARE CONTINUE HANDLER FOR dup_keys SET @garbage = 1;
-> SET @x = 1;
ฺ b r) deฺ
->
m Gui
INSERT INTO storedroutines.handler_table VALUES (1);
c o
->
->
SET @x = 2;
p a rฺ ent
INSERT INTO storedroutines.handler_table VALUES (1);
one s Stud
-> SET @x = 3;
-> END// s
c i@ thi
Query OK, 0 rows affected (#.## sec)
c
o ฺ i
r; use
mysql> DELIMITER
e t
r procedure to declares both a condition and a handler to demonstrate
b
− The dohandler e
o workntogether.
s
c i
how(rthey c e
i
Ri−c The firstlINSERT INTO statement enters a value into handler_table. The
e r to second INSERT INTO generates an error of type SQLSTATE 23000, which fires
R ob the handler declared with DECLARE CONTINUE HANDLER. This handler sets the
@garbage variable before continuing with the procedure.
3. Test the dohandler stored procedure with a suitable CALL statement.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CALL dohandler();
Query OK, 0 rows affected, 1 warning (#.## sec)
− The dohandler procedure runs without any errors, but raises a warning indicating
the duplicate key.
+------+
| 3 |
+------+
1 row in set (#.## sec)
− The value of @x is 3, which shows that the procedure completed all its steps. If the
procedure did not handle the error correctly, it would have terminated before setting
the value to 3, and the value of @x would be 2.
5. Check the value of the @garbage user variable assigned in the execution of the
ble
dohandler stored procedure.
fe r a
Enter the following statement at the mysql prompt and receive the results shown:
ans
mysql> SELECT @garbage;
n - t r
+----------+
a no
| @garbage |
+----------+
h a s
| 1 |
ฺ b r) deฺ
+----------+
c o m Gui
1 row in set (#.## sec)
− The user-defined variable @garbage p a rฺ the value
has e n t 1, proving that the procedure
one did nottu
d
called the handler. If the procedure
s S call the handler, the value of @garbage
would be NULL.
c i @ this
6. ฺ
Keep the mysql command-line
t o ric client
u seopen for the next practice.
b e r to
(ro cens e
c i li
to Ric
e r
R ob
of stored routines.
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Create the find_table function in the storedroutines database by using the following
SQL code: ble
fe r a
CREATE FUNCTION find_table (tbl_name CHAR(64))
ans
RETURNS INT
n - t r
BEGIN o
DECLARE result BOOL DEFAULT FALSE;
s an
DECLARE show_table CHAR(64);
DECLARE doneFlag BOOL DEFAULT FALSE; r ) ha ฺ
DECLARE q1 CURSOR FOR m ฺb uide
r ฺco nt G
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
a
o n ep tude
WHERE TABLE_SCHEMA = DATABASE();
DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneFlag=true;
OPEN q1;
@ s is S
c c i th
doloop: LOOP
r i e
e r toฺ to us
FETCH q1 INTO show_table;
rob ense
IF doneFlag THEN
c i (LEAVE doloop;
Ric lic
END IF;
Enter the following statement at the mysql prompt and receive the results shown:
mysql> DELIMITER //
mysql> CREATE FUNCTION find_table (tbl_name CHAR(64))
-> RETURNS INT
-> BEGIN
-> DECLARE result BOOL DEFAULT FALSE;
-> DECLARE show_table CHAR(64);
-> DECLARE doneFlag BOOL DEFAULT FALSE;
-> DECLARE q1 CURSOR FOR
ble
-> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
fe r a
-> WHERE TABLE_SCHEMA = DATABASE();
ans
->
->
DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneFlag=true;
OPEN q1;
n - t r
o
an
-> doloop: LOOP
->
->
FETCH q1 INTO show_table;
IF doneFlag THEN s
ha ฺ
-> LEAVE doloop; r )
ฺb uide
-> END IF; m
->
r ฺco nt G
IF show_table = tbl_name THEN
a
->
-> END IF;
o n ep tude
SET result = TRUE, doneFlag = TRUE;
i ( rob ense
Query OK, 0 rows affected (#.## sec)
cc DELIMITER
Rimysql> lic ;
R ob database. If so, the function returns the number 1, to represent “true.” If the table
does not exist in the current database, the function returns 0, representing “false.”
− The function uses a query within a cursor to retrieve a list of tables from the
INFORMATION_SCHEMA database. The cursor processes the results of the query
one record at a time and sets the result variable to true if it finds a match.
2. Execute a SELECT statement that uses the find_table function, providing the parameter
value “no_such_table”.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT find_table ('no_such_table');
+------------------------------+
| find_table ('no_such_table') |
+------------------------------+
| 0 |
+------------------------------+
1 row in set (#.## sec)
− The table does not exist, so find_table returns 0, representing “false.”
+------------------------------+
| 1 |
+------------------------------+
1 row in set (#.## sec)
− In this case, the handler_table table exists, so the function returns the number 1,
representing “true.”
4. Leave the mysql command-line client open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take you approximately 20 minutes to complete.
Tasks
1. Create a function called c_length in the storedroutines database that returns the
length of a right-angled triangle’s longest side (hypotenuse) based on parameters
containing the lengths of the other two sides.
Hint: The square root function in MySQL is SQRT. ble
fe r a
2. Test the c_length function with the values 3 and 4.
ans
3. Review the details of the c_length procedure by using the INFORMATION_SCHEMA
n - t r
database.
a no
4. s
Create a function called factorial that calculates the factorial (n!) of a given value.
h a
5. Test the factorial function with the values 3 and 10.
ฺ b r) deฺ
6. Use SHOW CREATE FUNCTION to view the syntax used
o m to create
u i the factorial
function.
a c
rฺ ent G
p d window.
oneterminal
7. Exit the mysql client and close the Linux
s S tuprompt
c i @ this
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
i ( rob ense
Query OK, 0 rows affected (#.## sec)
cc DELIMITER
Rimysql> lic ;
e r
5. toTest the factorial function with the values 3 and 10.
R ob Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT factorial(3);
+--------------+
| factorial(3) |
+--------------+
| 6 |
+--------------+
1 row in set (#.## sec)
sql_mode:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ER
ROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION
`factorial`(n INT) RETURNS int(11)
BEGIN
DECLARE result INT;
SET result = 1;
WHILE n > 1 DO
SET result = result * n; ble
SET n = n - 1;
fe r a
END WHILE;
ans
RETURN result;
n - t r
END o
character_set_client: utf8
s an
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
r ) ha ฺ
1 row in set (#.## sec)
m ฺb uide
7. Exit the mysql client and close the Linux terminal r o window.
ฺcprompt t G
a. Enter the following statement at the mysql
a
ep prompt: de n
o n
s is S t u
mysql> EXIT
c i @ th
Bye
r i c e
#
e r t oฺ to us
r o b
b. Enter the following
s e
command at the Linux terminal prompt:
ci
# exit( ce n
R i c l i
e r t o
b
Ro
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 15:
Triggers om and i
Scheduled
u
r ฺ
a denc t G
Events
p
e
on Chaptertu15
s s S
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
• The sakila sample database is installed.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take approximately 25 minutes.
Tasks
1. Connect to the MySQL server by using the mysql command-line client and create a new
database called triggertest.
2. In the triggertest database, create a table called dotriggers with a single column
ble
called column1 of INT data type.
fe r a
3. Create a table called audit_triggers that contains the following columns:
ans
• old_column (INT)
n - t r
• new_column (INT)
a no
• date_completed (DATETIME) h a s
4. Create a trigger called dotriggers_ai by executing the ฺ b r) dstatement:
following e ฺ
i
om ONt dotriggers
u
CREATE TRIGGER dotriggers_ai AFTER INSERT
r ฺ
a denc G
FOR EACH ROW
e p
on s Stu date_completed)
INSERT INTO audit_triggers (new_column,
s
hi
VALUES (NEW.column1, NOW());
c c i@ thetdotriggers table.
o i
5. Insert the values 1 – 6 inclusive
ฺr rows into e
usin the previous step by issuing an appropriate SELECT
e t
6. Verify the insertion rof the new t o
statement. rob se
i ( e n
icc the execution
7. Confirm
R
statement.
lic of the dotriggers_ai trigger by issuing an appropriate SELECT
be8.rtoCreate a trigger called dotriggers_au by executing the following statement:
Ro CREATE TRIGGER dotriggers_au AFTER UPDATE ON dotriggers
FOR EACH ROW
INSERT INTO audit_triggers (old_column,
new_column,date_completed)
VALUES (OLD.column1, NEW.column1,NOW());
9. Update the dotriggers table, setting all values less than three to the new value of 10.
10. Verify the update in the previous step with a suitable SELECT statement.
11. Confirm the execution of the dotriggers_au trigger by issuing an appropriate SELECT
statement.
12. Create a trigger called dotriggers_ad by executing the following statement:
CREATE TRIGGER dotriggers_ad AFTER DELETE ON dotriggers
FOR EACH ROW
INSERT INTO audit_triggers(old_column, date_completed)
VALUES (OLD.column1, NOW());
13. Delete all rows from the dotriggers table with values greater than 2 and less than 10.
18. Verify the deletion in the previous step by issuing an appropriate SELECT statement.
19. Confirm that the dotriggers_ad trigger has not executed by issuing an appropriate
SELECT statement.
20. Leave the mysql command-line client open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
c i ( USEchanged
mysql>
c e n
triggertest;
R i c
Database l i
ert o
b mysql> CREATE TABLE dotriggers (column1 INT);
Ro Query OK, 0 rows affected (#.## sec)
3. Create a table called audit_triggers that contains the following columns:
• old_column (INT)
• new_column (INT)
• date_completed (DATETIME)
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE TABLE audit_triggers (
-> old_column INT,
-> new_column INT,
-> date_completed DATETIME
-> );
Query OK, 0 rows affected (#.## sec)
| ci ( 6 |cen
|
c li
Ri+---------+
e r to 6 rows in set (#.## sec)
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE TRIGGER dotriggers_au AFTER UPDATE ON dotriggers
-> FOR EACH ROW
-> INSERT INTO audit_triggers (
-> old_column, new_column, date_completed)
-> VALUES (OLD.column1, NEW.column1, NOW());
Query OK, 0 rows affected (#.## sec)
9. Update the dotriggers table, setting all values less than three to the new value of 10.
ble
Enter the following statement at the mysql prompt and receive the results shown:
fe r a
mysql> UPDATE dotriggers SET column1=10 WHERE column1 < 3;
ans
Query OK, 2 rows affected (#.## sec)
n - t r
Rows matched: 2 Changed: 2 Warnings: 0
a no
h a s
10. Verify the update in the previous step with a suitable SELECT statement.
Enter the following statement at the mysql prompt and receive
ฺ b r) dresults
the
e ฺ shown:
mysql> SELECT * FROM dotriggers;
c o m Gui
+---------+
p a rฺ ent
ne Stud
| column1 |
+---------+
s o
| 10 |
c i @ this
ric use
| 10 |
| 3 |
| r t o ฺ
| 4 e
b e to
|
ro
| i ( 6 | en
5 | s
c c
i+---------+ li c
R
erto
6 rows in set (#.## sec)
b − The query displays all rows in the dotriggers table. The first two rows now have a
Ro value of 10.
11. Confirm the execution of the dotriggers_au trigger by issuing an appropriate SELECT
statement.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT * FROM audit_triggers;
+------------+------------+---------------------+
| old_column | new_column | date_completed |
+------------+------------+---------------------+
| NULL | 1 | 2016-05-18 16:25:33 |
| NULL | 2 | 2016-05-18 16:25:33 |
| NULL | 3 | 2016-05-18 16:25:33 |
| NULL | 4 | 2016-05-18 16:25:33 |
| NULL | 5 | 2016-05-18 16:25:33 |
| NULL | 6 | 2016-05-18 16:25:33 |
| 1 | 10 | 2016-05-18 16:27:03 |
| 2 | 10 | 2016-05-18 16:27:03 |
+------------+------------+---------------------+
8 rows in set (#.## sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
i c l i c| | 5 | 2016-05-18 16:25:33 |
ert
| 1 | 10 | 2016-05-18 16:27:03 |
b | 2 | 10 | 2016-05-18 16:27:03 |
Ro |
|
3 |
4 |
NULL | 2016-05-18 16:28:37 |
NULL | 2016-05-18 16:28:37 |
| 5 | NULL | 2016-05-18 16:28:37 |
| 6 | NULL | 2016-05-18 16:28:37 |
+------------+------------+---------------------+
12 rows in set (#.## sec)
− The audit_triggers table does not contain entries for the rows deleted in step
17 because the dotriggers_ad trigger no longer exists.
20. Leave the mysql command-line client open for the next practice.
Overview
In this practice, you investigate the behavior of triggers in the sakila database.
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Using the INFORMATION_SCHEMA database, display information about all the triggers in
the sakila database. Explain the purpose of the rental_date trigger.
2. Examine the structure of the rental table.
3. Disable the SQL_MODE system variable by setting it to an empty string. Ignore any warning
ble
messages that result from this step.
fe r a
4. Insert a new row into the rental table.
t r a ns
5. Verify the previous insertion by issuing an appropriate SELECT statement. Explain
o n - the
contents of the columns you did not provide values for.
s an
6. Query the film_text table for the film with an ID of 113.
r ) ha ฺ
7. Update the description of the film with an ID of 113 in theฺb
film tableeto read “No longer
available.” o m u id
a c
ฺ film_text
rthe n G
t table.
Note: Make this change in the film table, p not e
8. Reissue the query in step 6 to query o
s nefilm_text
the S tudtable for the film with an ID of 113.
Explain the result.
c i @ this
9. Leave the mysql command-line
t o ฺ ric uclient
se open for the next practice.
b e r to
(ro cens e
c i li
R ic
e r to
R ob
Solution Steps
Tasks
1. Using the INFORMATION_SCHEMA database, display information about all the triggers in
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
erto ...
Reading table information for completion of table and column names
b
Ro Database changed
mysql> DESCRIBE rental\G
*************************** 1. row ***************************
Field: rental_id
Type: int(11)
Null: NO
Key: PRI
Default: NULL
Extra: auto_increment
*************************** 2. row ***************************
Field: rental_date
Type: datetime
Null: NO
Key: MUL
Default: NULL
Extra:
*************************** 3. row ***************************
Field: inventory_id
Type: mediumint(8) unsigned
Null: NO
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Key: MUL
Default: NULL
Extra:
*************************** 5. row ***************************
Field: return_date
Type: datetime
Null: YES
Key:
Default: NULL
Extra: ble
*************************** 6. row ***************************
fe r a
Field: staff_id
ans
Type: tinyint(3) unsigned
n - t r
Null: NO
Key: MUL
a no
Default: NULL
h a s
Extra:
ฺ b r) deฺ
Field: last_update
c o m Gui
*************************** 7. row ***************************
Type: timestamp
p a rฺ ent
one s Stud
Null: NO
Key:
s
Default: CURRENT_TIMESTAMP
c c i@ thi
r i se
Extra: on update CURRENT_TIMESTAMP
o ฺ u
r t
7 rows in set (#.## sec)
e tovariable by setting it to an empty string. Ignore any warning
b
ro resultenfrom
3. Disable the SQL_MODE e
s this step.
system
c
messagesi (that c
R
Enter
li statement at the mysql prompt and receive the results shown:
icthe following
berto mysql> SET SQL_MODE='';
Ro Query OK, 0 rows affected, 1 warning (#.## sec)
Duration
This practice should take approximately 15 minutes.
Tasks
1. Inspect the value of the global event_scheduler system variable. If the
event_scheduler system variable is OFF, enable it.
2. Set the triggertest database that you created in an earlier practice to be the default
database. ble
fe r a
3. Create a table in the triggertest database called timer_table with a single column of
ans
INT data type called timer_count.
n - t r
4. no
Insert a single row into the timer_table table with a value of zero in the timer_count
a
column.
h a s
5.
ฺ b r) deฺ
Create a scheduled event that fires one minute after its creation and every minute
thereafter indefinitely. When the event fires, it must increment the value of the
c o m Gui
timer_count column in the timer_table table by one.
6. p a rฺ ent
Immediately execute SHOW PROCESSLIST and verify that the scheduled event you created
one s Stud
in the previous step is waiting for activation.
s
7. i@ thi
Keep querying the timer_table every minute or so for a few minutes and verify that the
c c
o ฺ r i se
timer_count column is incremented by one every time the event fires.
u
e t
r database,
8. Drop the triggertest to exit the mysql command-line client, and close the Linux
b e
c i (ro cens
terminal window.
R ic li
berto
Ro
Tasks
1. Inspect the value of the global event_scheduler system variable. If the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
R ob ...
Database changed
3. Create a table in the triggertest database called timer_table with a single column of
INT data type called timer_count.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> CREATE TABLE timer_table (timer_count INT);
Query OK, 0 rows affected (#.## sec)
4. Insert a single row into the timer_table table with a value of zero in the timer_count
column.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> INSERT INTO timer_table VALUES(0);
Query OK, 1 row affected (#.## sec)
Enter the following statements at the mysql prompt and receive the results shown:
mysql> DELIMITER //
mysql> CREATE EVENT event_timer
-> ON SCHEDULE EVERY 1 MINUTE
-> ON COMPLETION PRESERVE
-> DO
-> BEGIN
-> UPDATE triggertest.timer_table
-> SET timer_count = timer_count + 1;
ble
-> END //
fe r a
Query OK, 0 rows affected (#.## sec)
ans
mysql> DELIMITER ;
n - t r
6. no
Immediately execute SHOW PROCESSLIST and verify that the scheduled event you created
in the previous step is waiting for activation: a
h a s
ฺ b r) deฺ
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SHOW PROCESSLIST\G
c o m Gui
Id: 4 p a rฺ ent
*************************** 1. row ***************************
User: root
s one s Stud
hi
Host: localhost
db: triggertestci@ t
Command: Query ฺr i c e
Time: 0 rto
t o us
State: b
o e
startinge
i ( r e s
nPROCESSLIST
c cInfo:
c
SHOW
li
to Ri***************************
Id: 5
2. row ***************************
e r
ob
User: event_scheduler
R Host: localhost
db: NULL
Command: Daemon
Time: 9
State: Waiting for next activation
Info: NULL
2 rows in set (#.## sec)
7. Keep querying the timer_table every minute or so for a few minutes and verify that the
timer_count column is incremented by one every time the event fires.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT * FROM timer_table;
+-------------+
| timer_count |
+-------------+
| 1 |
+-------------+
1 row in set (#.## sec)
...
...
mysql> SELECT * FROM timer_table;
+-------------+
| timer_count |
+-------------+
| 3 |
+-------------+
1 row in set (#.## sec)
...
ble
8. Drop the triggertest database, exit the mysql command-line client, and close the Linux
fe r a
terminal window.
ans
Enter the following statement at the mysql prompt and receive the results shown:
n - t r
mysql> DROP DATABASE triggertest;
a no
Query OK, 1 row affected (#.## sec)
h a s
mysql> EXIT;
ฺ b r) deฺ
Bye
c o m Gui
#
p a rฺ ent
Enter the following command at the Linux
o neterminal d and receive the results shown:
tuprompt
s S
# exit
c i @ this
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 16:
Reporting om t Gu i
r ฺ c
a 16den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
locations.
Assumptions
• The sakila and world sample databases are installed.
• The /labs/sql directory contains the logins.sql file.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. Connect to the MySQL server by using either the mysql command-line client in a Linux
terminal session or MySQL Workbench, and make world the current database.
2. Issue a query that uses an aggregation function to display the number of countries in the
Country table.
ble
3. Use an aggregation function to write a single query that displays the number of countries in
fe r a
the Country table that:
ans
• Gained independence earlier than 1945
n - t r
• Gained independence in 1945 or later
a no
• Have no specified independence date
h a s
In the following format:
ฺ b r) deฺ
c o m Gui
+-------------------+---------------+---------------+
| Earlier than 1945 | 1945 or Laterrฺ| Not n t
p a e specified |
one s##St|ud
+-------------------+---------------+---------------+
| ## |
@ s i
## |
i thsurface areas of the countries with the smallest
+-------------------+---------------+---------------+
c cname
r i e
us in the following format:
e r toฺrespectively,
4. Issue queries that display
and largest land mass,
the
t o
and
( rob with
Query 1: Country
i e n sesmallest surface area:
the
R cc lic
i+-------------------------------+--------------------+
e r to |+-------------------------------+--------------------+
Smallest Country | Surface Area |
| Bangladesh | 129155000 |
| Brazil | 170115000 |
...
7. Issue a query that displays a count of the total number of countries in Asia and the number
of Asian countries with populations greater than the average population you determined in
step 5. Display the results in the following format:
+-----------------+-------------------------------+
| Asian Countries | Asian Countries > Average Pop |
+-----------------+-------------------------------+
ble
| ## | ## |
fe r a
+-----------------+-------------------------------+
ans
8. Issue a query that calculates the percentage of all countries in the world with populations
n - t r
o
an
greater than the average you determined in step 5.
9. s
Leave the mysql command-line client open for the next practice.
ha ฺ
r )
ฺb uide
m
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Enter the following command at the Linux terminal prompt and receive the results shown:
# mysql -uroot -poracle
mysql: [Warning] Using a password on the command line interface can be
insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
...
Enter the following statement at the mysql prompt and receive the results shown:
mysql> USE world
ble
Database changed
fe r a
2. Issue a query that uses an aggregation function to display the number of countries in the
ans
Country table.
n - t r
no
Enter the following statement at the mysql prompt and receive the results shown:
a
mysql> SELECT COUNT(*) FROM Country;
h a s
+----------+
ฺ b r) deฺ
m Gui
| COUNT(*) |
+----------+
c o
rฺ ent
| 239 |
p a
one s Stud
+----------+
1 row in set (#.## sec)
s
3. Use an aggregation functionctoi@
i c t hi query that displays the number of countries in
write a single
e
the Country table that:ฺr
e r t o to us
r o b
• Gained independence
s eearlier than 1945
• Gained (
ci independence n
ce in 1945 or later
i c l i
•R Have no specified independence date
r t o
e In the following format:
b
Ro +-------------------+---------------+---------------+
| Earlier than 1945 | 1945 or Later | Not specified |
+-------------------+---------------+---------------+
| ## | ## | ## |
+-------------------+---------------+---------------+
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT
-> SUM(IndepYear < 1945) AS 'Earlier than 1945',
-> SUM(IndepYear >= 1945) AS '1945 or Later',
-> SUM(IndepYear IS NULL) AS 'Not specified'
-> FROM Country;
+-------------------+---------------+---------------+
| Earlier than 1945 | 1945 or Later | Not specified |
+-------------------+---------------+---------------+
| 70 | 122 | 47 |
+-------------------+---------------+---------------+
1 row in set (#.## sec)
6. Issue a query that lists all countries where the population is greater than the average
population you determined in the previous step. Display the results in the following format:
+---------------------------------------+------------+
| Name | Population |
+---------------------------------------+------------+
| Argentina | ######### |
| Bangladesh | ######### |
| Brazil | ######### |
ble
...
fe r a
Enter the following statements at the mysql prompt and receive the results shown:
ans
mysql> SET @avPop = (SELECT AVG(Population) FROM Country);
n - t r
o
an
Query OK, 0 rows affected (#.## sec)
mysql> SELECT s
ha ฺ
-> Name, Population r )
ฺb uide
-> FROM Country
m
-> WHERE Population > @avPop;
a r ฺco nt G
ep tud|e Population |
+---------------------------------------+------------+
| Name
o n
s is S | 37032000 |
+---------------------------------------+------------+
| Argentina
c i @ th
| Bangladesh
ฺ r i c s e | 129155000 |
| Brazil
r t o t o u | 170115000 |
e
rob ense
...
c i (
| Ukraine
c
| 50456000 |
c
Ri| Vietnam
| l i
United States | 278357000 |
e r to | South Africa |
|
40377000 |
79832000 |
R ob +---------------------------------------+------------+
38 rows in set (#.## sec)
7. Issue a query that displays a count of the total number of countries in Asia and the number
of Asian countries with populations greater than the average population you determined in
step 5. Display the results in the following format:
+-----------------+-------------------------------+
| Asian Countries | Asian Countries > Average Pop |
+-----------------+-------------------------------+
| ## | ## |
+-----------------+-------------------------------+
Enter the following statements at the mysql prompt and receive the results shown:
mysql> SELECT SUM(Continent='Asia')
-> AS 'Asian Countries',
-> ( SELECT SUM(Continent='Asia')
-> FROM Country
-> WHERE Population > @avPop
-> ) AS 'Asian Countries > Average Pop'
-> FROM Country;
Duration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
Tasks
1. In the mysql command-line prompt, or MySQL Workbench, change the current database to
sakila.
2. Use a GROUP BY expression to list all film categories and the number of films within each
category in the following format:
ble
+-------------+-----------------+
fe r a
| name | Number of Films |
ans
+-------------+-----------------+
| Action | ## | n - t r
| Animation | ## |
a no
| Children | ## |
h a s
...
ฺ b r) tables
d e ฺ to access this
Note: You will need to join the film_category and category
om t Gu i
information.
r ฺ c nfilm table that begin with each
atitles indthe
3. Issue a query that displays the number ofefilmp
n Stu e
letter of the alphabet in the following
s oformat:
c i @ this
ic u| se
+--------------+-------+
| first_letter |ฺrtotal
o
e t
r | t##
+--------------+-------+ o|
o b e
| B i (r n|s ## |
| A
cCc
i|... c e
li | ## |
R
berto
4. Issue a query that lists the ten customers with the largest number of film rentals of seven
Ro days or more, in descending order of the number of rentals, in the following format:
+------------+-----------+--------------+
| first_name | last_name | Long Rentals |
+------------+-----------+--------------+
| FIRSTNAME | LASTNAME | ## |
...
Notes
− You will need to join the customer and rental tables to access this information.
− Use the DATEDIFF() function on the values of the rental_date and
return_date columns to determine the length of each rental.
5. Leave the mysql command-line client or MySQL Workbench open for the next practice.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> USE sakila
Database changed
2. Use a GROUP BY expression to list all film categories and the number of films within each
category, in the following format:
+-------------+-----------------+
| name | Number of Films |
+-------------+-----------------+
ble
| Action | ## |
fe r a
| Animation | ## |
ans
| Children
...
| ## |
n - t r
a no
Note: You will need to join the film_category and category tables to access this
information.
h a s
b r) deฺ
Enter the following statement at the mysql prompt and receive the results shown:
ฺ
c o m Gui
rฺ as
mysql> SELECT category.name,
-> COUNT(film_category.category_id)
p a e n t
'Number of Films'
one s Stud
-> FROM film_category
-> INNER JOIN category s
c i@ thi
-> ON film_category.category_id=category.category_id
c
o ฺ r i se
-> GROUP BY category.category_id;
u
r t to of Films |
+-------------+-----------------+
| name be | Number
(ro cen| s e
c i
+-------------+-----------------+
li |
Ri|c Animation
| Action 64 |
to
66 |
e r | Children | 60 |
R ob | Classics
| Comedy
|
|
57 |
58 |
| Documentary | 68 |
| Drama | 62 |
| Family | 69 |
| Foreign | 73 |
| Games | 61 |
| Horror | 56 |
| Music | 51 |
| New | 63 |
| Sci-Fi | 61 |
| Sports | 74 |
| Travel | 57 |
+-------------+-----------------+
16 rows in set (#.## sec)
| B | ## |
| C | ## |
...
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT LEFT(title, 1) AS first_letter,
-> COUNT(*) AS total
-> FROM film
-> GROUP BY first_letter;
+--------------+-------+
ble
| first_letter | total |
fe r a
+--------------+-------+
ans
| A
| B
|
|
46 |
63 | n - t r
| C | 92 |
a no
| D | 65 |
h a s
r) deฺ
| E | 32 |
...
ฺ
m Guib
| V
| W
|
|
22 |
43 | c o
rฺ ent
p a
one s Stud
| Y | 3 |
| Z | 3 |
s
+--------------+-------+
c c i@ thi
o ฺ i
25 rows in set (#.## sec)
se with the largest number of film rentals of seven
r ten customers
u
e r t
4. Issue a query that lists the
o of the number of rentals, in the following format:
torder
b
days or more, in descending e
c i (ro cens
R li | last_name | Long Rentals |
i|c first_name
+------------+-----------+--------------+
erto
+------------+-----------+--------------+
b | FIRSTNAME | LASTNAME | ## |
Ro ...
Notes
− You will need to join the customer and rental tables to access this information.
− Use the DATEDIFF() function on the values of the rental_date and
return_date columns to determine the length of each rental.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT customer.first_name, customer.last_name,
-> COUNT(rental.customer_id) AS 'Long Rentals'
-> FROM customer INNER JOIN rental
-> ON customer.customer_id=rental.customer_id
-> WHERE DATEDIFF(rental.return_date, rental.rental_date) > 7
-> GROUP BY customer.first_name, customer.last_name
-> ORDER BY COUNT(rental.customer_id) DESC LIMIT 10;
+------------+-----------+--------------+
| first_name | last_name | Long Rentals |
+------------+-----------+--------------+
| RHONDA | KENNEDY | 15 |
| CALVIN | MARTEL | 12 |
| WESLEY | BULL | 12 |
+------------+-----------+--------------+
10 rows in set (#.## sec)
5. Leave the mysql command-line client or MySQL Workbench open for the next practice.
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take approximately 20 minutes.
Tasks
1. Execute the SQL in the /labs/sql/logins.sql file. This SQL script creates the
logins database and populates the user_logins table.
2. Change the current database to logins and examine the structure of the user_logins
table. ble
fe r a
3. Issue a query that creates a crosstab report, which shows the name of each user and the
ans
number of times they logged in on each weekday. The report should be in the following
format: n - t r
a no
+----------+-----+-----+-----+-----+-----+-----+-----+
| User | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
h a s
+----------+-----+-----+-----+-----+-----+-----+-----+
ฺ b r) deฺ
| Charity | # | # | # | # | # | # |
c o m Gui
# |
| Charlene |
| Derek |
# |
# |
# |
# |
# |
# |
# |
# |
p
# |
a
# |rฺ ent
# |
# |
# |
# |
| Faisal | # | # | # |
s one s Stud
# | # | # | # |
i@ thi
| Felix | # | # | # | # | # | # | # |
| John | # | # |
i c c # | # | # | # | # |
| Ming |
t o ฺ
# | r # |
u
# |se # | # | # | # |
| Nora
b e r| # | # |
to# | # | # | # | # |
(rocolumn e
ntosthe report that displays the total number of logins for each user in
+----------+-----+-----+-----+-----+-----+-----+-----+
i
4. Add ancextra c e
c format,lias follows:
R
bar ichart
berto +----------+-----+-----+-----+-----+-----+-----+-----+------------------+
Ro | User | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Number of Logins |
+----------+-----+-----+-----+-----+-----+-----+-----+------------------+
| Charity | # | # | # | # | # | # | # | *********** |
| Charlene | # | # | # | # | # | # | # | ****** |
...
5. Close the connection to the MySQL server and exit any open Linux terminal prompts.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SOURCE /labs/sql/logins.sql
Query OK, 0 rows affected, 1 warning (#.## sec)
Database changed
Query OK, 0 rows affected (#.## sec)
ble
Query OK, 100 rows affected (#.## sec)
Records: 100 Duplicates: 0 Warnings: 0 fe r a
ans
mysql>
n - t r
o
2.
an
Change the current database to logins and examine the structure of the user_logins
table. s
r ) ha ฺ
Enter the following statements at the mysql prompt and receive the results shown:
m ฺb uide
ฺco nt G
mysql> USE logins;
Database changed
a r
mysql> SHOW TABLES;
o n ep tude
+------------------+
s is S
+------------------+ ci@ th
| Tables_in_logins |
r i c e
| user_logins
e r toฺ to us
+------------------+
|
o
1 row in set
r b (#.##sesec)
c (
i DESC c e n
Ri c
mysql> l i user_logins;
e r to +-----------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
b
Ro
+-----------+-----------------------+------+-----+---------+----------------+
| id | mediumint(8) unsigned | NO | PRI | NULL | auto_increment |
| LoginDate | varchar(255) | YES | | NULL | |
| User | varchar(255) | YES | | NULL | |
+-----------+-----------------------+------+-----+---------+----------------+
3 rows in set (#.## sec)
− The user_logins table stores usernames and login dates.
| Charity | # | # | # | # | # | # | # |
| Charlene | # | # | # | # | # | # | # |
| Derek | # | # | # | # | # | # | # |
| Faisal | # | # | # | # | # | # | # |
| Felix | # | # | # | # | # | # | # |
| John | # | # | # | # | # | # | # |
| Ming | # | # | # | # | # | # | # |
| Nora | # | # | # | # | # | # | # |
+----------+-----+-----+-----+-----+-----+-----+-----+
Enter the following statement at the mysql prompt and receive the results shown: ble
fe r a
mysql> SELECT User,
ans
->
->
COUNT(IF(WEEKDAY(LoginDate)=0,1,NULL)) AS Mon,
COUNT(IF(WEEKDAY(LoginDate)=1,1,NULL)) AS Tue, n - t r
o
->
->
COUNT(IF(WEEKDAY(LoginDate)=2,1,NULL)) AS Wed,
s
COUNT(IF(WEEKDAY(LoginDate)=3,1,NULL)) AS Thu,an
->
) ha ฺ
COUNT(IF(WEEKDAY(LoginDate)=4,1,NULL)) AS Fri,
r
->
ฺb uide
COUNT(IF(WEEKDAY(LoginDate)=5,1,NULL)) AS Sat,
m
ฺco nt G
-> COUNT(IF(WEEKDAY(LoginDate)=6,1,NULL)) AS Sun
-> FROM user_logins
a r
-> GROUP BY User;
o n ep tude
s is S
+----------+-----+-----+-----+-----+-----+-----+-----+
@
| User
c c i
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
th
i e
+----------+-----+-----+-----+-----+-----+-----+-----+
r
| Charity |
e
| Charlene |r toฺ to us
3 |
4 |
3 |
3 |
2 |
1 |
2 |
3 |
2 |
1 |
0 |
0 |
2 |
1 |
i (
| Derekrob ense
| 1 | 1 | 0 | 1 | 3 | 2 | 2 |
c
| Faisal
lic
| 1 | 2 | 1 | 2 | 2 | 3 | 1 |
to Ric| Felix | 4 | 1 | 1 | 1 | 5 | 1 | 3 |
e r | John | 4 | 1 | 2 | 0 | 1 | 0 | 0 |
R ob | Ming
| Nora
|
|
1 |
3 |
0 |
2 |
3 |
2 |
1 |
3 |
4 |
6 |
0 |
0 |
2 |
0 |
+----------+-----+-----+-----+-----+-----+-----+-----+
8 rows in set (#.## sec)
4. Add an extra column to the report that displays the total number of logins for each user in
bar chart format, as follows:
+----------+-----+-----+-----+-----+-----+-----+-----+------------------+
| User | Mon | Tue | Wed | Thu | Fri | Sat | Sun | Number of Logins |
+----------+-----+-----+-----+-----+-----+-----+-----+------------------+
| Charity | # | # | # | # | # | # | # | *********** |
| Charlene | # | # | # | # | # | # | # | ****** |
...
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT User,
-> COUNT(IF(WEEKDAY(LoginDate)=0,1,NULL)) AS Mon,
-> COUNT(IF(WEEKDAY(LoginDate)=1,1,NULL)) AS Tue,
-> COUNT(IF(WEEKDAY(LoginDate)=2,1,NULL)) AS Wed,
-> COUNT(IF(WEEKDAY(LoginDate)=3,1,NULL)) AS Thu,
-> COUNT(IF(WEEKDAY(LoginDate)=4,1,NULL)) AS Fri,
+----------+-----+-----+-----+-----+-----+-----+-----+------------------+
| Charity | 3 | 3 | 2 | 2 | 2 | 0 | 2 | ************** |
| Charlene | 4 | 3 | 1 | 3 | 1 | 0 | 1 | ************* |
| Derek | 1 | 1 | 0 | 1 | 3 | 2 | 2 | ********** |
| Faisal | 1 | 2 | 1 | 2 | 2 | 3 | 1 | ************ |
| Felix | 4 | 1 | 1 | 1 | 5 | 1 | 3 | **************** |
| John | 4 | 1 | 2 | 0 | 1 | 0 | 0 | ******** |
| Ming | 1 | 0 | 3 | 1 | 4 | 0 | 2 | *********** |
| Nora | 3 | 2 | 2 | 3 | 6 | 0 | 0 | **************** |
+----------+-----+-----+-----+-----+-----+-----+-----+------------------+
8 rows in set (#.## sec)
ble
5. Close the connection to the MySQL server and exit any open Linux terminal prompts. fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 17:
NoSQL om t Gu i
r ฺ c
a 17den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
R i−ccmemcached_test.php
lic
The /labs/php/cli/activity17-2
file.
directory contains the
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Use the mysql command-line client to check to see if the test database exists. If the
test database does not exist, then create it.
2. Run the innodb_memcached_config.sql script. This sets up the tables that the
memcached InnoDB plug-in requires to do its work. The script is in the ble
/usr/share/mysql directory. fe r a
ans
3. Verify the existence of the test and innodb_memcache databases. List the tables in the
n - t r
innodb_memcache database. o
4. s an
Examine the structure and contents of the containers table and answer the following
questions:
r ) ha ฺ
ฺb uide
a. Which database and table does the cache use to store items?
m
a r ฺco nt G
b. Which column contains the keys and which column contains the values?
o n ep tude
c. What do the other columns represent? Use the MySQL Reference Manual to help you
determine their use.
@ s is S
5. c i th
List the contents of the test database’s demo_test table. It contains a single key/value
c
r i e
e r toฺ to us
pair. What is the key and what is its value?
rob ense
6. Delete the contents of the demo_test table.
7. i (
Install the daemon_memcached plug-in on the MySQL server with the libmemcached.so
c
Ric
extension. lic
e r
8. to Open a separate terminal window. Verify that the memcached daemon is running and
R ob listening on the default port 11211 by using netstat -tulpn | grep :11211:.
9. Terminate any mysql command-line client sessions and close all terminal windows.
Log in to the MySQL server. Enter the following at the shell prompt and receive the
welcome message shown below:
# mysql –uroot –p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
. . .
mysql>
Check to see if the test database exists and if not, create it. Enter the following at the
mysql prompt and receive the results shown below: ble
fe r a
mysql> SHOW DATABASES;
ans
+--------------------+
| Database | n - t r
+--------------------+
a no
| information_schema |
| logins | h a s
| mysql |
ฺ b r) deฺ
| performance_schema |
c o m Gui
| sakila |
p a rฺ ent
one s Stud
| storedroutines |
| sys |
s
| world |
+--------------------+
c c i@ thi
o ฺ r i
8 rows in set (#.## sec)
u se
t
er DATABASE to test;
mysql> o b e
c i
Query (rOK, 1cerownsaffected (#.## sec)
CREATE
Database changed
Query OK, 0 rows affected (#.## sec)
Database changed
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
c i
List thectables
l i c e innodb_memcache database. Enter the following statement at the
i
R prompt and receive the results shown:
mysql
e r t o
b mysql> USE innodb_memcache
Ro . . .
Database changed
mysql> SHOW TABLES;
+---------------------------+
| Tables_in_innodb_memcache |
+---------------------------+
| cache_policies |
| config_options |
| containers |
+---------------------------+
3 rows in set (#.## sec)
− The script has created the innodb_memcache database, which contains three
tables: cache_policies, config_options and containers.
4. Examine the structure and contents of the containers table and answer the following
questions:
a. Which database and table does the cache use to store items?
b. Which column contains the keys and which column contains the values?
e t
1 row in set (#.## sec)
r and table todoes the cache use to store items?
b
a. Which database e
i (ro The
− cAnswer: c e s uses the database and table defined in the containers
ncache
R li
ic table’s db_schema and db_table columns, respectively. The
ert o innodb_memcached_config.sql script created a demo_test table in the test
b
Ro b.
database to store data items.
Which column contains the keys and which column contains the values?
− Answer: The c1 column in the test.demo_test table contains the keys and the
c2 column contains the values.
c. What do the other columns represent? Use the MySQL Reference Manual to assist
you.
− This information can be found in the MySQL Reference Manual at:
http://dev.mysql.com/doc/mysql/en/innodb-memcached-internals.html
− Answer: The flags column specifies the column in the test.demo_test table
that holds a user-defined numeric value. It can also be used as the column specifier
for some operations (such as incr and prepend) if the memcached value is
mapped to multiple columns.
− Answer: The cas_column and expire_time_column specify the columns in
test.demo_test that hold memcached compare-and-swap and expiration values,
respectively. Unless you want full memcached compliance, you can disregard these
columns.
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
be r
8. Open a separate terminal window. Verify that the memcached daemon is running and
Ro listening on the default port 11211 by using netstat -tulpn | grep :11211:
Enter the following command at the shell prompt and receive results similar to those shown
below:
# netstat -tulpn | grep :11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 1751/mysqld
tcp 0 0 :::11211 :::* LISTEN 1751/mysqld
udp 0 0 0.0.0.0:11211 0.0.0.0:* 1751/mysqld
udp 0 0 :::11211 :::* 1751/mysqld
− The memcached plug-in for InnoDB is now running and listening on the default port
11211.
9. Terminate any mysql command-line client sessions and close all terminal windows.
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Examine the source code in the memcached_test.php file in
/labs/php/cli/activity17-2 and answer the following questions: ble
fe r a
a. What command-line parameters does the program accept?
ans
b. Which statement writes a value to the cache?
n - t r
c. Which statement reads a value from the cache?
a no
d. Does the program use SQL to achieve either of these operations?
h a s
2. r) deฺ
Open a new Linux terminal window. These practice instructions refer to this terminal
ฺ b
window as T1.
c o m Gui
3. rฺ ent
Open another Linux terminal window and log in to the mysql command-line client. These
p a
one s Stud
practice instructions refer to this terminal window as T2.
4. s
In terminal window T1, execute the memcached_test.php program with the following
c c i@ thi
options to write a key/value pair to the cache.
o ฺ r i u e mysql 0001
sSET
r t
# php memcached_test.php
e to window T2, verify that the demo_test table in the test
b e
5. At the mysql
c i (ro caerecord
prompt
ns for the key/value pair you added to the cache in the previous
in terminal
database
R ic
step.
li
contains
be6.rtoIn terminal window T1, execute the memcached_test.php program with the following
Ro options. This uses the key to retrieve the value you set in step 4 from the cache.
# php memcached_test.php GET mysql
7. In terminal window T2, exit the mysql client.
8. In terminal window T2, shut down the MySQL server by using the following command:
# service mysqld stop
9. In terminal window T1, execute the memcached_test.php program with the following
options to attempt to write a new value to the cache:
# php memcached_test.php SET XYZ 4321
10. In terminal window T2, check to see whether the memcached plug-in for InnoDB is still
listening on port 11211 by issuing the following Linux command:
# netstat -tulpn | grep :11211
11. In terminal window T2, start the MySQL server with the following command at the shell
prompt:
# service mysqld start
e r t o t o
o b
if ((strcmp($op,
r s e 'GET') == 0) && count($argv) != 3) {
c} i (
die
ce n
("Incorrect number of arguments. Expected GET <key>\n");
R i c l i
e r to
ob
if ((strcmp($op, 'SET') == 0) && count($argv) != 4) {
R die ("Incorrect number of arguments. Expected SET <key>
<value>\n");
}
erto
SET mysql 0001
b Enter the following command at the T1 Linux terminal prompt and receive the results
Ro shown:
# cd /labs/php/cli/activity17-2/
# php memcached_test.php SET mysql 0001
SET KEY: mysql, VALUE: 0001
SET RESULT: true
− The program executes and confirms the successful addition of the item to the
cache.
5. At the mysql prompt in terminal window T2, verify that the demo_test table in the test
database contains a record for the key/value pair you added to the cache in the previous
step.
Enter the following statements at the mysql prompt in terminal window T2 and receive the
results shown:
mysql> USE test
Database changed
6. In terminal window T1, execute the memcached_test.php program with the following
options. This uses the key to retrieve the value you set in step 4 from the cache.
# php memcached_test.php GET mysql
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# php memcached_test.php GET mysql
GET KEY: mysql
ble
GET VALUE: 0001
fe r a
− The program executes and displays the value associated with the key mysql.
ans
7. In terminal window T2, exit the mysql client. n - t r
a no
Enter the following statement at the mysql prompt in terminal window T2 and receive the
results shown:
h a s
mysql> EXIT
ฺ b r) deฺ
Bye
c o m Gui
#
p a rฺ ent
s o ne server
8. In terminal window T2, shut down the MySQL
S tudby using the following command:
# service mysqld stop
c i @ this
ฺ
Enter the following command
t o ric at the
u e Linux terminal prompt and receive the results
sT2
shown:
b e r to
(ro mysqld:
# service mysqld e
ns stop
c i li c e
R ic
Stopping [ OK ]
erto
9. In terminal window T1, execute the memcached_test.php program with the following
b options to attempt to write a new value to the cache:
Ro # php memcached_test.php SET XYZ 4321
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# php memcached_test.php SET XYZ 4321
PHP Notice: Memcache::connect(): Server localhost (tcp 11211, udp 0)
failed with: Connection refused (111) in /labs/php/cli/activity17-
2/memcached_test.php on line 4
PHP Warning: Memcache::connect(): Can't connect to localhost:11211,
Connection refused (111) in /labs/php/cli/activity17-
2/memcached_test.php on line 4
Could not connect
− The operation fails, because the memcached plug-in is no longer running.
10. In terminal window T2, check to see whether the memcached plug-in for InnoDB is still
listening on port 11211 by issuing the following Linux command:
# netstat -tulpn | grep :11211
prompt:
# service mysqld start
Enter the following command at the T2 Linux terminal prompt and receive the results
shown:
# service mysqld start
Starting mysqld: [ OK ]
#
ble
12. In terminal window T2, log in to the MySQL server with the mysql command-line client.
fe r a
Enter the following command at the T2 Linux terminal prompt and receive the results
an s
shown:
n - t r
# mysql -uroot -p
a no
Enter password: oracle
h a s
. . . ฺ b r) deฺ
Welcome to the MySQL monitor. Commands end with ; or \g.
o m Gui
Type 'help;' or '\h' for help. Type '\c' to clear the current input
c
statement.
p a rฺ ent
mysql>
s one s Stud
13. In terminal window T2, list thec @ tofhthe
icontents i test database’s demo_test table to verify
i c e
that the item you addedฺrin step 4 issstill there.
e
Enter the following r to toatuthe mysql prompt in terminal window T2 and receive the
statements
(
results shown:
i rob ense
R cc USElictest
imysql>
erto ...
Reading table information
b
Ro Database changed
15. In terminal window T1, use the memcached_test.php Java program to verify that the new
item you added in the previous step is stored in the cache by executing the following
command:
# php memcached_test.php GET XYZ
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# php memcached_test.php GET XYZ
ble
GET KEY: XYZ
GET VALUE: 4321 fe r a
− The program executes and displays the value associated with the new key XYZ. ans
n - t r
16. Close terminal window T1.
Enter the following command at the T1 Linux terminal prompt: a no
h a s
# exit
ฺ b r) deฺ
17. In terminal window T2, exit the mysql client, but leave
o m Guterminal
the Linux i window open for
the next practice.
a c
rฺ ent
p
ne prompt
Enter the following statement at the mysql d receive the results shown:
uand
s o S t
mysql> EXIT
c i @ this
Bye
#
t o ฺ ric use
b e r to
(ro cens e
c i li
R ic
berto
Ro
Note: This practice uses two Linux terminal windows. The instructions refer to the terminal
windows as T1 and T2.
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Examine the source code in the MemcachedTest.java file in e
/labs/java/cli/activity17-3 and answer the following questions: r a bl
a. What command-line parameters does the program accept? s fe
b. Which statement writes a value to the cache? - t r an
on
c. Which statement reads a value from the cache?
s an
2. r ha ฺ
d. Does the program use SQL to achieve either of these operations?
)
Open a new Linux terminal window. These practice instructions refer to this terminal
window as T1. m ฺb uide
3. a r ฺco nt G
Open another Linux terminal window and log in to the mysql command-line client. These
n ep tude
practice instructions refer to this terminal window as T2.
o
4. @ s is S
In terminal window T1, compile the test program from the source code in the
c c i th
i e
MemcachedTest.java file in /labs/java/cli/Practice17-3 by using the following
r
statement:
e r toฺ to us
b se
roMemcachedTest.java
# cd /labs/java/cli/activity17-3
c i ( e n
5.
# javac
Ric lic
In terminal window T1, execute the MemcachedTest.java program with the following
e r tooptions to write a key/value pair to the cache:
b
Ro # java MemcachedTest SET mysql 0001
6. At the mysql prompt in terminal window T2, verify that the demo_test table in the test
database contains a record for the key/value pair you added to the cache in the previous
step.
7. In terminal window T1, execute the MemcachedTest.java program with the following
options. This uses the key to retrieve the value you set in step 5 from the cache.
# java MemcachedTest GET mysql
8. In terminal window T2, exit the mysql client.
9. In terminal window T2, shut down the MySQL server by using the following command:
# service mysqld stop
10. In terminal window T1, execute the MemcachedTest.java program with the following
options to attempt to write a new value to the cache:
# java MemcachedTest SET XYZ 4321
bebin these
/* arguments passed into the program, which
shouldro
c i ( e n
c or <key>
format
R i c l i GET
R ob */
String key = "";
String value = "";
if (! ( (args[0].toUpperCase().equals("GET")) ||
(args[0].toUpperCase().equals("SET")) ) ) {
System.out.println("Missing action. The first
parameter must be GET or SET");
System.exit(0);
}
if ( (args[0].toUpperCase().equals("GET")) && (args.length
!=2) ) {
System.out.println("Incorrect number of arguments.
Expected GET <key>");
System.exit(0);
}
if ( (args[0].toUpperCase().equals("SET")) && (args.length
!=3) ) {
System.out.println("Incorrect number of arguments.
Expected SET <key> <value>");
System.exit(0);
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
try {
System.out.println("SET RESULT: " +
mcc.set(key, value));
}
catch (Throwable t)
{
System.out.println("SET operation exception.
Error message : " + t.getMessage());
t.printStackTrace();
} ble
}
fe r a
// GET with correct number of arguments
ans
if (args[0].toUpperCase().equals("GET")) {
n - t r
key = args[1]; o
an
System.out.println("GET KEY: " + key);
s
try {
r ) ha ฺ
System.out.println("GET VALUE: " +
mcc.get(key));
m ฺb uide
}
a
catch (Throwable t) r ฺco nt G
{
o n ep tude
s is S
System.out.println("GET operation exception.
@
i th
Error message : " + t.getMessage());
c c
r i e
toฺ to us
t.printStackTrace();
e r }
rob ense
}
c i (
}
Ric
}
lic
e r to The answers to the questions are as follows:
R ob a. Answer: The program accepts SET <key> <value> to store an item, and GET
<key> to retrieve an item.
b. Answer: The mcc.set(key, value) statement writes a key/value pair to the cache.
c. Answer: The mcc.get(key) statement reads a value from the cache by using its
key.
d. Answer: The program does not use SQL to read or write values to the cache.
2. Open a new Linux terminal window. These practice instructions refer to this terminal
window as T1.
3. Open another Linux terminal window and log in to the mysql command-line client. These
practice instructions refer to this terminal window as T2.
Enter the following command at the T2 Linux terminal prompt and receive the results
shown:
# mysql -uroot -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
. . .
mysql>
4. In terminal window T1, compile the test program from the source code in the
MemcachedTest.java file in /labs/java/cli/Practice17-3 by using the following
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
statement:
# cd /labs/java/cli/activity17-3
# javac MemcachedTest.java
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# cd /labs/java/cli/activity17-3
# javac MemcachedTest.java
#
ble
5. In terminal window T1, execute the MemcachedTest.java program with the following fe r a
options to write a key/value pair to the cache:
ans
n - t r
no
# java MemcachedTest SET mysql 0001
a
Enter the following command at the T1 Linux terminal prompt and receive the results
s
shown: h a
ฺ b r) deฺ
# java MemcachedTest SET mysql 0001
SET KEY: mysql, VALUE: 0001
c o m Gui
SET RESULT: true
p a rฺ ent
− The program executes and confirms
cache: s tud addition of the item to the
one stheSsuccessful
c c i@windowthT2,i verify that the demo_test table in the test
o ฺ
database contains atrecord
i
6. At the mysql prompt in terminal e
r for theuskey/value pair you added to the cache in the previous
e r t o
rob estatement
se at the mysql prompt in terminal window T2 and receive the
step.
(
Enter thei following n
iccshown: lic
results
R
berto mysql> USE test
Ro Database changed
o ne Stud
10. In terminal window T1, execute the MemcachedTest.java
s program with the following
options to attempt to write a new
c i@ thi
value to s
the cache:
# java MemcachedTest
ฺ i c
r SETusXYZ e 4321
Enter the followinge t o
rcommandtoat the T1 Linux terminal prompt and receive the results
shown: (ro
b se
i e n
R ccjava MemcachedTest
i#SET lic SET XYZ 4321
Enter the following command at the T2 Linux terminal prompt and receive the results
shown:
# mysql -uroot -p
Enter password: oracle
Welcome to the MySQL monitor. Commands end with ; or \g.
. . .
Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.
ble
mysql> fe r a
an s
14. In terminal window T2, list the contents of the test database’s demo_test table to verify
n - t r
that the item you added in step 5 is still there. o
an
Enter the following statements at the mysql prompt in terminal window T2 and receive the
s
results shown:
r ) ha ฺ
mysql> USE test
m ฺb uide
Reading table information
... a r ฺco nt G
Database changed
o n ep tude
@ s is S
mysql> SELECT * FROM idemo_test;
r i c c e th
| c2 to|ฺ c3 s | c5 |
+-------+------+------+------+------+
|uc4
| c1
e r t o
ro|b0001 |se 0 |
+-------+------+------+------+------+
c i (
| mysql
c e n 1 | 0 |
li (#.## sec)
Ri1c row in set
+-------+------+------+------+------+
e r to − Even though you shut down the MySQL server, disabling the memcached plug-in,
R ob the item you stored earlier is still in the table.
− Note: The values for c3, c4, and c5 might be different on your system.
15. In terminal window T1, execute the MemcachedTest Java program with the following
options to write a new value to the cache:
# java MemcachedTest SET XYZ 4321
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# java MemcachedTest SET XYZ 4321
SET KEY: XYZ, VALUE: 4321
SET RESULT: true
− This time the operation is successful.
Note: This practice uses two Linux terminal windows. The instructions refer to the terminal
windows as T1 and T2.
Duration
This practice should take approximately 15 minutes to complete.
Tasks
1. Examine the source code in the memcached_test.py file in e
/labs/python/cli/activity17-4 and answer the following questions: r a bl
a. What command-line parameters does the program accept? s fe
b. Which statement writes a value to the cache? - t r an
c. Which statement reads a value from the cache? no n
s a
a
d. Does the program use SQL to achieve either of these operations?
h
2. r) deฺ
Open a new Linux terminal window. These practice instructions refer to this terminal
ฺ b
window as T1.
c o m Gui
3. a rฺ ent
Open another Linux terminal window and log in to the mysql command-line client. These
p
s o ne Stud
practice instructions refer to this terminal window as T2.
4. @ this
In terminal window T1, make the memcached_test.py program executable by executing
c i
t o ฺ ric use
the following commands at the Linux terminal prompt:
e r to
# cd /labs/python/cli/activity17-4/
b
(ro cens
# chmod +x e
memcached_test.py
c i li T1, execute the memcached_test.py program with the following
ic window
5. In terminal
R
erto
options to write a key/value pair to the cache.
b
Ro
# python memcached_test.py SET mysql 0001
6. At the mysql prompt in terminal window T2, verify that the demo_test table in the test
database contains a record for the key/value pair you added to the cache in the previous
step.
7. In terminal window T1, execute the memcached_test.py program with the following
options. This uses the key to retrieve the value you set in step 4 from the cache.
# python memcached_test.py GET mysql
8. In terminal window T2, exit the mysql client.
9. In terminal window T2, shut down the MySQL server by using the following command:
# service mysqld stop
10. In terminal window T1, execute the memcached_test.py program with the following
options to attempt to write a new value to the cache:
# python memcached_test.py SET XYZ 4321
e r to sys.exit()
key.
d. Answer: The program does not use SQL to read or write values to the cache.
2. Open a new Linux terminal window. These practice instructions refer to this terminal
window as T1.
3. Open another Linux terminal window and log in to the mysql command-line client. These
practice instructions refer to this terminal window as T2.
Enter the following command at the T2 Linux terminal prompt and receive the results
ble
shown:
fe r a
ans
# mysql -uroot -p
Enter password: oracle
n - t r
Welcome to the MySQL monitor. Commands end with ; or \g.
a no
. . .
h a s
Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.
ฺ b r) deฺ
c o m Gui
mysql>
p a rฺ ent
4. In terminal window T1, make the memcached_test.py
o n e tud program executable by executing
the following commands at the Linux terminal S
s is prompt:
i @ h
i cc se t
# cd /labs/python/cli/activity17-4/
ฺ r
e r to to u
# chmod +x memcached_test.py
i
optionscto
roba key/value
5. In terminal window
(write T1, s
e n e the memcached_test.py program with the following
execute
− Note: The values for c3, c4, and c5 might be different on your system.
7. In terminal window T1, execute the memcached_test.py program with the following
options. This uses the key to retrieve the value you set in step 5 from the cache.
# python memcached_test.py GET mysql
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# python memcached_test.py GET mysql
ble
GET KEY: mysql
GET VALUE: 0001
fe r a
− The program executes and displays the value associated with the key mysql. ans
n - t r
8. In terminal window T2, exit the mysql client.
n o
Enter the following statement at the mysql prompt in terminal window a
s T2 and receive the
results shown: h a
ฺ b r) deฺ
mysql> EXIT
c o m Gui
Bye
#
p a rฺ ent
ne server
9. In terminal window T2, shut down theoMySQL
s S tudby using the following command:
# service mysqld stop c i @ this
o ฺ
Enter the following command
t ric at the
u e Linux terminal prompt and receive the results
sT2
shown: b e r to
(ro mysqld e
ns stop
c i
# service
c li c e
R iStopping mysqld: [ OK ]
b rtoIn terminal window T1, execute the memcached_test.py program with the following
e10.
Ro options to attempt to write a new value to the cache:
# python memcached_test.py SET XYZ 4321
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# python memcached_test.py SET XYZ 4321
MemCached: MemCache: inet:127.0.0.1:11211: connect: [Errno 111]
Connection refused. Marking dead.
SET KEY: XYZ, VALUE: 4321
SET RESULT: False
− The operation fails, because the memcached plug-in is no longer running.
11. In terminal window T2, check to see whether the memcached plug-in for InnoDB is still
listening on port 11211 by issuing the following Linux command:
# netstat -tulpn | grep :11211
prompt:
# service mysqld start
Enter the following command at the T2 Linux terminal prompt and receive the results
shown:
# service mysqld start
Starting mysqld: [ OK ]
#
ble
13. In terminal window T2, log in to the MySQL server with the mysql command-line client.
fe r a
Enter the following command at the T2 Linux terminal prompt and receive the results
an s
shown:
n - t r
# mysql -uroot -p
a no
Enter password: oracle
h a s
. . . ฺ b r) deฺ
Welcome to the MySQL monitor. Commands end with ; or \g.
o m Gui
Type 'help;' or '\h' for help. Type '\c' to clear the current input
c
statement.
p a rฺ ent
mysql>
s one s Stud
14. In terminal window T2, list thec @ tofhthe
icontents i test database’s demo_test table to verify
i c e
that the item you addedฺrin step 4 issstill there.
e
Enter the following r to toatuthe mysql prompt in terminal window T2 and receive the
statements
(
results shown:
i rob ense
R cc USElictest
imysql>
erto ...
Reading table information
b
Ro Database changed
16. In terminal window T1, use the memcached_test.py program to verify that the new item
you added in the previous step is stored in the cache by executing the following command:
# python memcached_test.py GET XYZ
Enter the following command at the T1 Linux terminal prompt and receive the results
shown:
# python memcached_test.py GET XYZ
GET KEY: XYZ
ble
GET VALUE: 4321
− The program executes and displays the value associated with the new key XYZ. fe r a
ans
17. Close terminal window T1.
n - t r
Enter the following command at the T1 Linux terminal prompt:
a no
# exit
h a s
ฺ r) terminal
18. In terminal window T2, exit the mysql client, but leave thebLinux
d e ฺ window open for
the next practice.
om t Gu i
r ฺ
a and
Enter the following statement at the mysql prompt
c n the results shown:
receive
e p d e
mysql> EXIT
s on s Stu
Bye
c c i@ thi
#
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
Assumptions
You have a Linux terminal window open from the preceding practice.
Duration
This practice should take approximately 15 minutes to complete.
Tasks
ble
1. Examine the /labs/sql/sensor.sql file in your favorite text editor or MySQL
fe r a
Workbench. This SQL script creates a database called sensor with a single table called
ans
sensor_status. The sensor_status table contains a single column called status of
n - t r
a no
JSON data type. The JSON data represents sensor equipment with the ability to record
temperature, pressure, or both and the last readings and the time they were taken.
The following is an extract from the sensor.sql file: h a s
ฺ b r) deฺ
DROP DATABASE IF EXISTS sensor;
CREATE DATABASE sensor;
c o m Gui
USE sensor;
CREATE TABLE sensor_status ( ep
arฺ dent
status JSON NOT NULL
s on s Stu
);
i c c e t hi
i@ (status)
r t o ฺr
INSERT INTO sensor_status
us
VALUES
b e
( '{"device_id":
e
1001,t o "measurement_date": 1460592000, "last_temp":
i r o
( cen),
54, "last_pressure": s 27.8, "capabilities": ["temperature",
c li
Ri(c '{"device_id":
"pressure"]}'
1001, "measurement_date": 1460678400, "last_temp":
e r to 53, "last_pressure": 28.2, "capabilities": ["temperature",
R ob "pressure"]}' ),
( '{"device_id": 1001, "measurement_date": 1460764800, "last_temp":
60, "last_pressure": 30.1, "capabilities": ["temperature",
"pressure"]}' ),
...
2. Execute the labs/sql/sensor.sql script to create and populate the sensor database.
3. The sensor manufacturer adds the capability to power the sensor on and off. Add a new
field called on to the JSON data in the sensor column that describes this state and set its
initial value to a JSON “null”. Verify the change to the table data.
Hint: Use the JSON_INSERT(json_doc, path, val[, path, val] ...) function to
add data to JSON documents without overwriting existing data.
4. The sensor manufacturer removes this capability. Remove the on field from the JSON
document.
Hint: Use the JSON_REMOVE(json_doc, path[, path] ...) function for this
operation.
6. Add a new field called last_humidity to store the humidity readings for device 1001.
7. Add a new virtual generated column to the sensor_status table called device_id that
contains the “device_id” value from the JSON document. You will use this generated
column as an index in the next step.
8. Create an index on the sensor_status table’s device_id generated column called
sensor_idx and use an EXPLAIN statement to verify that the new index is available to the
MySQL optimizer.
ble
9. Leave the mysql command-line client open for the next practice.
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
JSON data type. The JSON data represents sensor equipment with the ability to record
temperature, pressure, or both and the last readings and the time they were taken.
The following is an extract from the sensor.sql file:
DROP DATABASE IF EXISTS sensor;
CREATE DATABASE sensor;
USE sensor;
CREATE TABLE sensor_status (
status JSON NOT NULL
ble
);
fe r a
INSERT INTO sensor_status (status)
VALUES
t r a ns
( '{"device_id": 1001, "measurement_date": n -
1460592000, "last_temp":
o
54, "last_pressure": 27.8, "capabilities":
"pressure"]}' ), an
["temperature",
s
( '{"device_id": 1001, "measurement_date":
) h a "last_temp":
1460678400,
53, "last_pressure": 28.2, "capabilities":
ฺ b r e ฺ
["temperature",
d
"pressure"]}' ), u i
om 1460764800,
( '{"device_id": 1001, "measurement_date":
a r ฺ c n t G "last_temp":
o n ep tude
60, "last_pressure": 30.1, "capabilities": ["temperature",
"pressure"]}' ),
@ s is S
...
c c i th to create and populate the sensor database.
r i
2. Execute the labs/sql/sensor.sql s e script
Enter the followinge r toฺ toatuthe
statements Linux terminal prompt and mysql command-line client
and receive r o
thebresults sshown:
e
c i ( -uroot c e n
R c
i...
# mysql l i -poracle
Database changed
Query OK, 0 rows affected (#.## sec)
| world |
+--------------------+
11 rows in set (#.## sec)
3. The sensor manufacturer adds the capability to power the sensor on and off. Add a new
field called on to the JSON data in the sensor column that describes this state and set its
initial value to a JSON “null”. Verify the change to the table data.
Hint: Use the JSON_INSERT(json_doc, path, val[, path, val] ...) function to
add data to JSON documents without overwriting existing data.
ble
Enter the following statements at the mysql prompt and receive the results shown:
fe r a
ans
mysql> UPDATE sensor_status
-> SET status = JSON_INSERT(status, '$.on', CAST(NULL AS JSON));
n - t r
o
an
Query OK, 24 rows affected (#.## sec)
Rows matched: 24 Changed: 24 Warnings: 0
ha ฺs
mysql> SELECT * FROM sensor_status; r )
ฺb uide
m
+------------------------------------------------------+
| status
a r ฺco nt G |
c c
| ...
l i c e |
e r
R ob Note: You must cast the SQL NULL to its JSON equivalent in the JSON_INSERT()
function; otherwise, it evaluates to NULL.
4. The sensor manufacturer removes this capability. Remove the on field from the JSON
document.
Hint: Use the JSON_REMOVE(json_doc, path[, path] ...) function for this
operation.
Enter the following statements at the mysql prompt and receive the results shown:
mysql> UPDATE sensor_status
-> SET status = JSON_REMOVE(status, '$.on');
Query OK, 24 rows affected (#.## sec)
Rows matched: 24 Changed: 24 Warnings: 0
5. Device 1001 now receives humidity data. Add “humidity” to the capabilities array for this
device.
Hint: Use the JSON_EXTRACT(json_doc, path[, path] ...) function in the WHERE
clause to identify the correct device and use the
JSON_ARRAY_APPEND(json_doc, path, val[, path, val] ...) function to
modify the capabilities of that device.
Enter the following statements at the mysql prompt and receive the results shown:
ble
mysql> UPDATE sensor_status
fe r a
-> SET status = JSON_ARRAY_APPEND(
ans
->
-> )
status, '$.capabilities', "humidity"
n - t r
-> WHERE JSON_EXTRACT(status, '$.device_id') = 1001;
a no
Query OK, 3 rows affected (#.## sec)
Rows matched: 3 Changed: 3 Warnings: 0 h a s
ฺ b r) deฺ
mysql> SELECT * FROM sensor_status;
c o m Gui
| status
p rฺ ent
+---------------------------------------------------------------------------+
a |
e
on s [... d
tu"pressure",
+---------------------------------------------------------------------------+
s
| {"device_id": 1001, ... "capabilities":
S "humidity"], ... |
| {"device_id": 1002,
ฺ i c
...
se
r ... "capabilities":
"capabilities": ["temperature"], ... |
r t o
| {"device_id": 1002,
t o u ["temperature"], ... |
o b e
| {"device_id":
e
1002, ... "capabilities": ["temperature"], ... |
| ...
r
i ( inlisete n s |
c c
+---------------------------------------------------------------------------+
c
Ri
24 rows (#.## sec)
e r
6. toAdd a new field called last_humidity to store the humidity readings for device 1001.
R ob Enter the following statements at the mysql prompt and receive the results shown:
mysql> UPDATE sensor_status
-> SET status = JSON_INSERT(status, '$.last_humidity', 0)
-> WHERE JSON_EXTRACT(status, '$.device_id') = 1001;
Query OK, 3 rows affected (#.## sec)
Rows matched: 3 Changed: 3 Warnings: 0
Assumptions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
You have a Linux terminal window open from the preceding practice.
Duration
This practice should take approximately 30 minutes to complete.
Tasks
1. Use the JSON_LENGTH(json_doc[, path]) function to determine the different number
of elements that appear in the status field’s JSON documents. ble
fe r a
2. Use the JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
ans
function to verify that both the “last_humidity” and “on” paths exist in the status field’s
n - t r
3.
JSON documents. Explain the result.
a no
Use the column->path shorthand syntax for JSON_EXTRACT() to list the device IDs of
those devices that last reported a temperature of 61. h a s
4. ฺ b r) deฺ
List the device IDs of all devices that contain “temperature”, “pressure”, and “humidity” in
the “capabilities” array. c o m Gui
p a rฺ ent
Hint: Use the JSON_CONTAINS(json_doc, val[, path]) function where val uses a
one s Stud
call to JSON_ARRAY([val[, val] ...] to match the required array values.
s
5. i@ thi
List the device IDs that contain “pressure” in the “capabilities” array and the path to that
c c
o ฺ r i
element in the JSON document.
u se
e r t to
Hint: Use the JSON_SEARCH(json_doc, one_or_all, search_str[,
b e
c i (ro cens
escape_char[, path] ...]) function to return the path.
6. Useic li
R the JSON_TYPE(json_val) function to return the JSON data type of both the
e r to “capabilities” array and its first element for each row in the sensor_status table.
R ob 7. List the different keys that appear in the status column’s JSON documents.
Hint: Use the JSON_KEYS(json_doc) function to return the keys.
8. List the device ID and the capabilities (without the surrounding quotation marks) of the
device with a last pressure reading of 29.7 in the following format:
+--------+-------------+-------------+-------------+
| device | capability1 | capability2 | capability3 |
+--------+-------------+-------------+-------------+
| 1003 | temperature | pressure | |
+--------+-------------+-------------+-------------+
Hints: Use the JSON_UNQUOTE(json_val) function to remove the quotation marks
around the string values in the “capabilities” array. Use the MySQL function
COALESCE(value, "") to remove any null values from the display output.
9. Exit all open mysql command-line sessions and Linux terminal prompts.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT DISTINCT JSON_LENGTH(status) FROM sensor_status;
+---------------------+
| JSON_LENGTH(status) |
+---------------------+
| 6 |
| 4 |
| 5 |
+---------------------+
ble
3 rows in set (#.## sec)
fe r a
2. Use the JSON_CONTAINS_PATH(json_doc, one_or_all, path[, path] ...)
ans
function to verify that both the “last_humidity” and “on” paths exist in the status field’s
n - t r
JSON documents. Explain the result. o
s an
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT JSON_CONTAINS_PATH
r ) ha ฺ
-> (status, 'all', '$.last_humidity', ฺ'$.on')
m b ide
-> FROM sensor_status;
r ฺ c o t Gu
+-------------------------------------------+
e p a den
| JSON_CONTAINS_PATH
(status, 'all', '$.last_humidity', s tu
on s S'$.on') |
@
ci e th i
+-------------------------------------------+
|
ฺ r i c s 0 |
|
r t o t o u 0 |
|
o b e e 0 |
|
i ( r e n s 0 |
c li c
Ri|c
| 0 |
to
0 |
e r | 0 |
R ob ...
+-------------------------------------------+
24 rows in set (#.## sec)
− Answer: The function call returns zero (false) because only the “last_humidity” field
appears in the JSON document and you used the “all” modifier. If you used the
“one” modifier, then the result would be one (true) because at least one of the
supplied fields is present in the JSON document.
3. Use the column->path shorthand syntax for JSON_EXTRACT() to list the device IDs of
those devices that last reported a temperature of 61.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT status->'$.device_id'
-> FROM sensor_status
-> WHERE status->'$.last_temp' = 61;
+-----------------------+
| status->'$.device_id' |
+-----------------------+
| 1002 |
| 1003 |
c o m Gui
escape_char[, path] ...]) function to return the path.
p a rฺ ent
Enter the following statement at the mysql prompt and receive the results shown:
b e e to
(ro ||ce"$.capabilities[1]"
+---------+---------------------+
i
| 1001
c ns |
c
| 1001 l i
Ri| 1001 | "$.capabilities[1]" |
"$.capabilities[1]" |
e r to | 1003 | "$.capabilities[1]" |
R ob | 1003
| 1003
| "$.capabilities[1]" |
| "$.capabilities[1]" |
| 1005 | "$.capabilities[1]" |
| 1005 | "$.capabilities[1]" |
| 1005 | "$.capabilities[1]" |
| 1007 | "$.capabilities[1]" |
| 1007 | "$.capabilities[1]" |
| 1007 | "$.capabilities[1]" |
+---------+---------------------+
12 rows in set (#.## sec)
6. Use the JSON_TYPE(json_val) function to return the JSON data type of both the
“capabilities” array and its first element for each row in the sensor_status table.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT JSON_TYPE(status->'$.capabilities') AS capabilities,
-> JSON_TYPE(status->'$.capabilities[0]') AS first_element
-> FROM sensor_status;
+--------------+---------------+
| capabilities | first_element |
+--------------+---------------+
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
n
|
+--------------------------------------------------------------------------------------------------+
- t r
no
3 rows in set (#.## sec)
8. a
List the device ID and the capabilities (without the surrounding quotation marks) of the
s
h a
device with a last pressure reading of 29.7 in the following format:
ฺ b r) deฺ
+--------+-------------+-------------+-------------+
| device | capability1 | capability2 |ocapability3
c m Gui |
a rฺ ent
+--------+-------------+-------------+-------------+
p
| 1003 | temperature | pressure
o n e tu| d |
s is S
+--------+-------------+-------------+-------------+
@
c i
Hints: Use the JSON_UNQUOTE(json_val)
c th function to remove the quotation marks
around the string values r i e
us any nullarray.
ฺ in the “capabilities” Use the MySQL function
e r to"") o
totremove values from the display output.
COALESCE(value,
b
roCOALESCE()se returns the first non-NULL value in the parameter list, or NULL
i
− Note:( e n
R iccif there are
licno non-NULL values.
bertoEnter the following statement at the mysql prompt and receive the results shown:
Ro mysql> SELECT DISTINCT status->'$.device_id' AS device,
-> COALESCE(JSON_UNQUOTE(status->'$.capabilities[0]'),"")
-> AS capability1,
-> COALESCE(JSON_UNQUOTE(status->'$.capabilities[1]'),"")
-> AS capability2,
-> COALESCE(JSON_UNQUOTE(status->'$.capabilities[2]'),"")
-> AS capability3
-> FROM sensor_status
-> WHERE status->'$.last_pressure' = 29.7;
+--------+-------------+-------------+-------------+
| device | capability1 | capability2 | capability3 |
+--------+-------------+-------------+-------------+
| 1003 | temperature | pressure | |
+--------+-------------+-------------+-------------+
8 rows in set (#.## sec)
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 18:
Spatial omDataGu i
a r ฺ c n t
p e
one s Stud
Chapter 18
s
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
Assumptions
None
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
Duration
This practice should take approximately 20 minutes to complete.
Tasks
1. At a Linux terminal prompt, log in to the mysql client as root (password: oracle).
2. Create a new database named malaga.
3. In the malaga database, create a new table called restaurant with the following columns
ble
and attributes:
fe r a
• name: VARCHAR(25), NOT NULL
ans
• g: GEOMETRY, NOT NULL n - t r
4. Populate the restaurant table with the following row data: a no
• Name: “Las Iguanas”, Location: (-4.433, 36.73) h a s
• Name: “Picassos”, Location: (-4.421, 36.729) ฺ b r) deฺ
c o m Gui
• Name: “La Tasca”, Location: (-4.430, 36.720)
p a rฺ ent
• Name: “El Pirata”, Location: (-4.434,
• Name: “Alquima”, Location: (-4.406, s one36.724) tud
36.726)
S
c i @(-4.414,
t h is
• Name: “Amantia”, Location:
t o ฺ ric u(-4.424,
se 36.718)
36.731)
• Name: “El Metro”,
b e r to spatial function to encode the point coordinate data in a
Location:
(risosuitable
Note: Use an appropriate e
nfors storage in a MySQL GEOMETRY column.
MySQL
formatc i
that c e
li query that displays the name and coordinate data of all rows in the
R ic a suitable
5. oExecute
b e rt restaurant table.
R o Note: Use a suitable MySQL function to convert the values stored in the GEOMETRY column
g into readable coordinates.
6. Keep the Linux terminal window open and connected to the mysql command-line client for
the next practice.
erto
• Name: “Picassos”, Location: (-4.421, 36.729)
b
Ro • Name: “La Tasca”, Location: (-4.430, 36.720)
• Name: “El Pirata”, Location: (-4.434, 36.726)
• Name: “Alquima”, Location: (-4.406, 36.724)
• Name: “Amantia”, Location: (-4.414, 36.731)
• Name: “El Metro”, Location: (-4.424, 36.718)
Note: Use an appropriate MySQL spatial function to encode the point coordinate data in a
format that is suitable for storage in a MySQL GEOMETRY column.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> INSERT INTO restaurant (name, g)
-> VALUES
-> ("Las Iguanas", ST_GeomFromText('POINT(-4.433 36.734)')),
-> ("Picassos", ST_GeomFromText('POINT(-4.421 36.729)')),
-> ("La Tasca", ST_GeomFromText('POINT(-4.430 36.720)')),
-> ("El Pirata", ST_GeomFromText('POINT(-4.434 36.726)')),
-> ("Alquima", ST_GeomFromText('POINT(-4.406 36.724)')),
-> ("Almantia", ST_GeomFromText('POINT(-4.414 36.731)')),
-> ("El Metro", ST_GeomFromText('POINT(-4.424 36.718)'));
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT name, ST_AsText(g) FROM restaurant;
+-------------+----------------------+
| name | ST_AsText(g) |
+-------------+----------------------+
| Las Iguanas | POINT(-4.433 36.734) |
| Picassos | POINT(-4.421 36.729) |
| La Tasca | POINT(-4.43 36.72) |
ble
| El Pirata | POINT(-4.434 36.726) |
fe r a
| Alquima
| Almantia
| POINT(-4.406 36.724) |
| POINT(-4.414 36.731) |
ans
| El Metro | POINT(-4.424 36.718) |
n - t r
+-------------+----------------------+
a no
7 rows in set (#.## sec)
h a s
the next practice. ฺ b r) dcommand-line
6. Keep the Linux terminal window open and connected to the mysql
e ฺ client for
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
R ic li
berto
Ro
Duration
This practice should take you approximately 25 minutes to complete.
Tasks
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob 1. The map above shows the location of the restaurants in the restaurant table and a
polygon with the following coordinates:
• -4.422, 36.732
• -4.440, 36.723
• -4.427, 36.718
• -4.411, 36.733
• -4.422, 36.732
Note: The beginning and end coordinates of the polygon are identical, effectively “closing”
the polygon.
Create a variable called @poly to store the polygon coordinates in Well-Known Text (WKT)
format.
2. Create a spatial index on the GEOMETRY column g in the restaurant table.
3. Execute a query that uses the MySQL ST_Contains(g1, g2) function to list all
restaurants that are located within the polygon defined in the @poly variable. The first
5. The following coordinates describe the route taken by a road called the Calle Mármoles (not
identified on the map diagrams), which can be described in MySQL as a LINESTRING.
-4.44000, 36.72148
-4.43579, 36.72150
-4.43262, 36.72153
-4.43007, 36.72151
-4.42680, 36.72172
-4.42530, 36.72176
ble
Execute spatial queries that answer the following questions:
fe r a
a. Does the route taken by the Calle Mármoles fall completely within the polygon that you
ans
defined in step 1?
n - t r
o
step 1? s an
b. Does the route taken by the Calle Mármoles intersect the polygon that you defined in
r ) ha ฺ
c. What is the distance between the start and end points of the Calle Mármoles?
m ฺb uide
Note: For information about the correct spatial functions to use, refer to the MySQL
r ฺco nt G
Reference Manual at: https://dev.mysql.com/doc/mysql/en/spatial-relation-functions-object-
a
shapes.html#function_st-within.
o n ep tude
6. s is S
Exit the mysql client and close the Linux terminal window.
@
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
• -4.440, 36.723
• -4.427, 36.718
• -4.411, 36.733
• -4.422, 36.732
Note: The beginning and end coordinates of the polygon are identical, effectively “closing”
the polygon.
ble
Create a variable called @poly to store the polygon coordinates in Well-Known Text (WKT)
format. fe r a
ans
Enter the following statement at the mysql prompt and receive the results shown:
n - t r
mysql> SET @poly = 'Polygon((-4.422 36.732, -4.440 36.723, -
a no
4.427 36.718, -4.411 36.733, -4.422 36.732))';
h a s
br) ideฺtable.
Query OK, 0 rows affected (#.## sec)
2. Create a spatial index on the GEOMETRY column g in m theฺrestaurant
u
r ฺ c oand receive
t G
Enter the following statement at the mysql prompt
p a e n the results shown:
mysql> ALTER TABLE restaurant
-> ADD SPATIAL INDEX s one s Stud
hi sec)
(g);
Query OK, 0 rows affected
c c i@ (#.##
t
o
Records: 0 Duplicates: ฺr i u e
0s Warnings: 0
r t o
ethat usesethet MySQL ST_Contains(g1, g2) function to list all
3. Execute a query
o b
restaurants
c i r
( that are
c e ns within the polygon defined in the @poly variable. The first
located
ic to theliST_Contains() function is the polygon and the second parameter is the
parameter
R
erto
location of the restaurant stored in the GEOMETRY column g.
b
Ro
Note: Use a suitable MySQL function to encode the Well-Known Text (WKT) that is stored
in the @poly variable in MySQL GEOMETRY format.
Enter the following statement at the mysql prompt and receive the results shown:
mysql> SELECT name, ST_AsText(g) FROM restaurant
-> WHERE ST_Contains(ST_GeometryFromText(@poly), g);
+----------+----------------------+
| name | ST_AsText(g) |
+----------+----------------------+
| Picassos | POINT(-4.421 36.729) |
| La Tasca | POINT(-4.43 36.72) |
| Almantia | POINT(-4.414 36.731) |
+----------+----------------------+
3 rows in set (#.## sec)
4. Refer to the map at the beginning of this practice to ensure that the restaurants retrieved by
your query are located within the polygon of interest.
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
5. The following coordinates describe the routep a rฺ a nroad
taken by e
t called the Calle Mármoles (not
identified on the map diagrams), which
s S tud in MySQL as a LINESTRING.
e be described
oncan
-4.44000, 36.72148
c i @ this
-4.43579, 36.72150
t o ฺ ric use
r
-4.43262, 36.72153
-4.43007,be to
(ro 36.72172
36.72151
ns e
c i
-4.42680,
li c e
R ic
-4.42530, 36.72176
erto
Execute spatial queries that answer the following questions:
b
Ro
a. Does the route taken by the Calle Mármoles fall completely within the polygon that you
defined in step 1?
b. Does the route taken by the Calle Mármoles intersect the polygon that you defined in
step 1?
c. What is the distance between the start and end points of the Calle Mármoles?
Note: For information about the correct spatial functions to use, refer to the MySQL
Reference Manual at: https://dev.mysql.com/doc/mysql/en/spatial-relation-functions-object-
shapes.html#function_st-within.
a. Is the route taken by the Calle Mármoles completely within the polygon that you
defined in step 1?
− Enter the following statements at the mysql prompt and receive the results shown:
mysql> SET @route = 'LINESTRING(
'> -4.44000 36.72148,
'> -4.43579 36.72150,
'> -4.43262 36.72153,
'> -4.43007 36.72151,
'> -4.42680 36.72172,
| Within |
+--------+
| 0 |
+--------+
1 row in set (#.## sec)
− Answer: The ST_Within() function returns zero, so the route is not completely
within the polygon.
b. Does the route taken by the Calle Mármoles intersect the polygon that you defined in
step 1? ble
fe r a
− Enter the following statement at the mysql prompt and receive the results shown:
ans
mysql> SELECT ST_Intersects(ST_GeometryFromText(@route),
n - t r
-> ST_GeometryFromText(@poly)) AS Intersect;
a no
+-----------+
| Intersect | h a s
+-----------+
ฺ b r) deฺ
| 1 |
c o m Gui
+-----------+
p a rฺ ent
one s Stud
1 row in set (#.## sec)
s
− Answer: The ST_Intersects() function returns one, so the route intersects the
polygon.
c c i@ thi
c. o ฺ r i u se
What is the distance between the start and end points of the Calle Mármoles in the
e r t to
b
default units for the Spatial Reference System being used?
e
i (rothe following
− Enter
c c e ns statements at the mysql prompt and receive the results shown:
R c
imysql> li
SET @start = POINT(-4.44000, 36.72148);
6. Exit the mysql client and close the Linux terminal window.
Enter the following statement at the mysql and Linux terminal prompt and receive the
results shown:
mysql> EXIT
Bye
# exit
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
Practices bfor
ฺ r) Lesson
d e ฺ 19:
Conclusionom t Gu i
r ฺ c
a 19den
e p
Chapter
s on s Stu
c c i@ thi
o ฺ r i u se
e r t to
b e
c i (ro cens
Ric li
e r to
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
AppendixbA: r ) ha ฺ
Sample
Databases m ฺ uide
r o
ฺc nt G
a
p u20de
eChapter
s o n S t
c i@ thi s
ฺ r i c se
r t o to u
b e e
c i (ro cens
Ric li
e r to
R ob
The sakila database contains 16 tables and 7 views, along with stored procedures, functions,
and triggers.
To more easily understand their function, the tables in the sakila database can be categorized
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
o
s an
AppendixbB: r ) ha ฺ
MySQL
Workbench m ฺ uide
r o
ฺc nt G
a
p u21de
eChapter
s o n S t
c i@ thi s
ฺ r i c se
r t o to u
b e e
c i (ro cens
Ric li
e r to
R ob
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
c o m Gui
MySQL. Easily convert existing applications to run on MySQL on Windows and other
platforms.
p a rฺ ent
MySQL Workbench is available in community
s S tud editions. The community edition
oneands commercial
is free. Commercial editions provide
c i@ t i
additional features,
hgraphical such as database documentation
generation, schema and model
ฺ r i c s e
validation, and interfaces to other MySQL Enterprise
tools, at low cost. r t o to u
b e e
c i (ro cens
R ic li
e r to
R ob
Tasks
1. Launch MySQL Workbench by clicking Applications > Programming > MySQL Workbench.
2. On the MySQL Workbench home page, click the + (plus) symbol next to “MySQL
Connections” (shown below).
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
4. In the Connection Name textcfield,
i for the connection.
i@entertha name
i c e
ฺris root,uand
s click the “Store in Keychain” button.
5.
e r t
Verify that the usernameo t o
6.
i ( r ob nforsConnection”
The “Store Password e dialog box appears. Enter oracle and press OK.
7. c to enter
If prompted e
lic a password for the default keyring, use oracle again.
8. Ric
toMySQL server sets the current database to sakila. This is equivalent to executing the
Enter sakila in the Default Schema text field. When you establish the connection, the
e r
R ob following statement at the mysql command-line prompt:
mysql> USE sakila;
9. Leave all other values at their default settings. The default values create a TCP/IP
connection to a MySQL server instance running on localhost (127.0.0.1).
10. Click the Test Connection button. If all settings are correct, the following message appears:
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
to Ric
e r
R ob
17. The Review Settings dialog box appears and displays a message telling you that the
connection and configuration settings are correct for your new server instance. Click
Continue.
18. The wizard closes. In the Setup New Connection dialog box, click OK.
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ
20. Double-click the new connection.r i u se
21. A new tab appears e t
rin MySQLtoWorkbench, with the same name as the connection. Note the
b se
o SQL ndevelopment),
r(for
Query pane
c i ( c e Action Output pane (for server messages), and options
R c
iscreen.
for Server Status,l i Client Connections, Data Import/Export, and so on down the left side of
the
berto
22. Click the twin-headed arrow icon next in the SCHEMAS section on the left. This puts the
Ro schema information into its own tab and separates it from the management options. MySQL
Workbench allows you to customize many aspects of the interface in this way.
23. Click Server Status on the Management tab. This page provides a summary of the active
server instance, including graphs displaying real-time statistics:
• Load
• Connections
• Traffic
• Key Efficiency
• Queries per Second
• InnoDB Buffer Usage
• InnoDB Reads per Second
• InnoDB Writes per Second
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
24. On the Management tab, click Startup/Shutdown.
25. A new Administration tab appears,
i c c e t hi that the server instance is running. Click Stop
i@ confirming
Server. If prompted, enter
r t o ฺr the password
u s oracle.
b
26. If the server fails eto stop, echecko
t the Startup Message Log area on the Administration tab,
i r o n s
and see if (the server generates messages similar to the following:
c c l i c e
i2013-09-10 07:51:38 - Checked server status: Server is running.
o R
ert
Could not open error log file: sudo: sorry, you must have a tty to run
b sudo
Ro 2013-09-10 07:51:45 - Stopping server...
2013-09-10 07:51:45 - Executing '/etc/init.d/mysql stop'
2013-09-10 07:51:45 - Stop server: sudo: sorry, you must have a tty to
run sudo
If the server does not stop running and the log shows the sudo: sorry you must have
a tty to run sudo message, perform the following steps:
a. At a Linux terminal prompt, enter the following command:
# visudo
b. Use the arrow keys to move to the beginning of the following line (Defaults
requiretty).
# You have to run “ssh –t hostname sudo <cmd>”.
#
Defaults requiretty
c. Press the i key. This changes the vi editor mode to INSERT.
:wq!
#
− visudo is a utility command that helps you safely edit the /etc/sudoers file in
your Linux installation.
f. Exit the Linux terminal prompt:
# exit
g. On the MySQL Workbench Administration tab, click the Stop Server button.
ble
27. When the server has stopped running, click the Start Server button. If prompted, enter the
fe r a
password oracle.
ans
28. On the Management tab, select the “Status and System Variables” option. n - t r
o
29. an
On the Status Variables tab, enter InnoDB in the search box. Note the InnoDB status
s
30.
variables that appear.
On the Management tab, select Options File. r ) ha ฺ
m ฺb uide
31.
that it is /etc/my.cnf. a r ฺco nt G
View the location of the configuration file at the bottom of the Administration tab, and note
32. n ep tude
Click the InnoDB tab and examine the options available.
o
33. @ s is S
In the “Locate option” search box, enter autocommit and click Find. Select the
c c i th
i
autocommit option to enable it.
r e
34. Click Apply. e r toฺ to us
35. rob ense
In the “Apply Changes to MySQL Configuration File” dialog box, examine the changes and
i (
c
click Apply. lic
to
36. Ric
Using a Linux terminal, view the contents of the /etc/my.cnf file by entering:
e r
R ob # cat /etc/my.cnf
− Note the addition of the autocommit setting.
37. Repeat the previous steps to disable the “autocommit” setting in the option file and verify
the change.
38. Examine the other options available on the Management tab.
39. Close the Administration tab.
The following steps provide a brief overview of some of the MySQL Workbench features that
assist in SQL development. This tutorial builds on the steps in the lesson titled “MySQL Clients”
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2018, Oracle and/or its affiliatesฺ
and covers features of the SQL Development module not covered in that lesson.
Tasks
1. On the MySQL home page, double-click the new connection that you created in the
previous section.
2. A new tab displays with the same name as your connection.
3. Click the Schemas tab. Note that the sakila database is displayed in bold. This is
because the connection that you created previously sets sakila as the default database:
ble
fe r a
ans
n - t r
o
s an
r ) ha ฺ
m ฺb uide
a r ฺco nt G
o n ep tude
@ s is S
c c i th
r i e
e r toฺ to us
i ( rob ense
c lic
4. Ric
Click sakila on the Schemas tab and drill down through the hierarchy of database entities
tofor the sakila database until you find the film table.
b e r
Ro
ble
fe r a
ans
n - t r
a no
h a s
ฺ b r) deฺ
c o m Gui
p a rฺ ent
s one s Stud
c c i@ thi
o ฺ r i u se
e r t to
b e
6.
c (ro button
Click thei Export
c e nsabove the query results. The Export Resultset dialog box appears.
Ric li
e r to
R ob
7. Enter the name FilmOutput, and note the default format “CSV.” Click Save. This creates
a comma-delimited text file called FilmOut.csv in the root user’s home folder.
8. Change the contents of the film query tab to read:
SELECT * FROM sakila.film
where description like '%database%';
9. Execute the current statement by pressing Ctrl + Enter.
properly.
13. On the Snippets tab, click the Add button, which has a star-shaped icon. This adds the
contents of the editor pane to the Snippets tab.
14. Close the main tab (with the same name as your connection) to return to the MySQL
Workbench home tab.
15. Double-click your connection to activate it.
16. On the Snippets tab, right-click the snippet and select “Execute snippet” from the context
menu. ble
fe r a
− The previous SQL statement that you saved as a snippet, executes without you having
ans
to retype it.
n - t r
17. Delete the entire SQL statement on the Query 1 tab.
a no
18. Activate the Context Help tab by clicking it.
h a s
ฺ b r) deฺ
19. Enter select on the Query 1 tab and use the cursor to highlight the word “select.” Note
c o m Gui
how MySQL Workbench provides context-sensitive help for the SELECT statement.
arฺ dent
20. Continue typing to create the following SQL statement:
select count(*) from actor; ep
s n Stu
oyou
21. Note how MySQL Workbench assists
c i @ thby isproviding valid options for statements and
22. Close the Connection t o ric use
database entities as you type.
ฺ
tab.
b e r to Ctrl + Q.
(ro cens
23. Exit MySQL Workbench e
by pressing
c i li
R ic
berto
Ro