Chapter 11 Slides
Chapter 11 Slides
How to use
a MySQL database
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 1
Objectives
Applied
1. Use MySQL Workbench to start and stop the MySQL server.
2. Use MySQL Workbench to run SQL statements.
3. Use MySQL Workbench to run SQL scripts.
4. Code simple SELECT, INSERT, UPDATE, and DELETE
statements and use MySQL Workbench to test them.
Knowledge
1. Distinguish between SQL’s Data Definition Language (DDL) and
Data Manipulation Language (DML).
2. Describe the capabilities of a SELECT statement.
3. Describe the capabilities of INSERT, UPDATE, and DELETE
statements.
4. Describe what a SQL script does.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 2
MySQL is…
Inexpensive. Free for most uses and relatively inexpensive for
other uses.
Fast. One of the fastest relational databases currently available.
Easy to use. Easy to install and use.
Portable. Runs on most modern operating systems including
Windows, OS X, and Linux.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 3
MySQL provides…
Support for SQL. Like any modern database product, MySQL
supports SQL.
Support for multiple clients. Supports access from multiple
clients from a variety of interfaces and programming languages
including Java, PHP, Python, Perl, and C.
Connectivity. Provides access to data via an intranet or the
Internet.
Security. Protects access to your data so only authorized users can
view the data.
Referential integrity. With MySQL 5.5 and later, InnoDB tables
are used by default, which support referential integrity.
Transaction processing. With version 5.5, MySQL uses InnoDB
tables by default, which provide support for transaction
processing.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 4
A command-line tool
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 5
MySQL Workbench
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 6
The Home tab of MySQL Workbench
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 7
The dialog box for opening database connections
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 8
The Startup/Shutdown option
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 9
A SELECT statement and its results
Create New SQL Execute Current SQL tab Results
Tab button Statement button tab
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 10
How to enter and execute a SQL statement
To open a new SQL tab, press Ctrl+T or click the Create New
SQL Tab button in the SQL editor toolbar.
To select the current database, double-click it in the Schemas
section of the Navigator window. This displays the selected
database in bold.
To enter a SQL statement, type it into the SQL tab.
As you enter the text for a statement, the SQL tab applies color to
various elements, such as SQL keywords, to make them easy to
identify.
To execute a SQL statement, press Ctrl+Enter, or click the
Execute Current Statement button in the SQL editor toolbar. If the
statement retrieves data, the data is displayed in a Results tab
below the SQL tab.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 11
A SQL script and its results
Execute SQL Script
button
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 12
How to enter and execute a SQL script
When you code a script that contains more than one statement,
code a semicolon at the end of each statement.
To run an entire SQL script, press the Ctrl+Shift+Enter keys or
click the Execute SQL Script button that’s located just to the left
of the Execute Current Statement button in the SQL editor
toolbar.
When you run a SQL script, the results of each statement that
returns data are displayed in a separate Results tab.
To execute one SQL statement within a script, move the insertion
point into that statement and press Ctrl+Enter or click the Execute
Current Statement button. If the statement retrieves data, the data
is displayed in a Results tab.
To execute two or more statements within a script, select them in
the editor and then press Ctrl+Shift+Enter or click the Execute
SQL Script button.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 13
After a statement has been executed
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 14
How to create a database
CREATE DATABASE murach_test
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 15
How to create, select, and drop a database
Use the CREATE DATABASE statement to create a database and
the DROP DATABASE statement to delete a database. These are
SQL statements.
Use the USE command to select the database that you want to
work with. This is a MySQL command.
You can also select a database by double-clicking on it in the
Schemas section in MySQL Workbench.
The selected database will appear in bold in the Schemas section.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 16
How to create a table
CREATE TABLE User (
UserID INT NOT NULL AUTO_INCREMENT,
Email VARCHAR(50),
FirstName VARCHAR(50),
LastName VARCHAR(50),
PRIMARY KEY(UserID)
)
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 17
Tables, rows, columns, etc.
A relational database consists of one or more tables that consist
of rows (records) and columns (fields).
The primary key in a table is the one that uniquely identifies each
of the rows in the table.
A foreign key is used to relate the rows in one table to the rows in
another table.
When you create a table, you define each of its columns and you
identify its primary key.
To define a column, you must supply the name and the data type,
whether it’s automatically generated for new rows, and so on.
On Unix systems, the table and column names are case-sensitive.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 18
An INSERT statement that inserts multiple rows
INSERT INTO User
(FirstName, LastName, Email)
VALUES
('John', 'Smith', '[email protected]'),
('Andrea', 'Steelman', '[email protected]'),
('Joel', 'Murach', '[email protected]')
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 19
A SELECT statement that gets all columns
Syntax
SELECT *
FROM table-1
[WHERE selection-criteria]
[ORDER BY column-1 [ASC|DESC] [, column-2 [ASC|DESC] ...]]
Result set
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 20
A SELECT statement that gets selected columns
Syntax
SELECT column-1 [,column-2] ...
FROM table-1
[WHERE selection-criteria]
[ORDER BY column-1 [ASC|DESC] [,column-2 [ASC|DESC] ...]]
Result set
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 21
How to select data from a single table
A SELECT statement is a SQL DML statement that returns a
result set (or result table) that consists of the specified rows and
columns.
To specify the columns, use the SELECT clause.
To specify the rows, use the WHERE clause.
To specify the table that the data should be retrieved from, use the
FROM clause.
To specify how the result set should be sorted, use the ORDER
BY clause.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 22
A SELECT statement that joins two tables
Syntax
SELECT column-1 [,column-2] ...
FROM table-1
{INNER | LEFT OUTER | RIGHT OUTER} JOIN table-2
ON table-1. column-1 {=|<|>|<=|>=|<>} table-2.column-2
[WHERE selection-criteria]
[ORDER BY column-1 [ASC|DESC] [,column-2 [ASC|DESC] ...]]
select *
from table1 a, table2 b
where a.id = b.id
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 23
A SELECT statement that joins two tables (cont.)
A statement that joins the User and Download tables
SELECT Email, DownloadFilename, DownloadDate
FROM User
INNER JOIN Download
ON User.UserID = Download.UserID
WHERE DownloadDate > '2014-01-01'
ORDER BY Email ASC
Result set
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 24
How to select data from multiple tables
To return a result set that contains data from two tables, join the
tables. To do that, use a JOIN clause. Most of the time, you’ll
want to code an inner join so that rows are only included when the
key of a row in the first table matches the key of a row in the
second table.
In a left outer join, the data for all of the rows in the first table (the
one on the left) are included in the table, but only the data for
matching rows in the second table are included. In a right outer
join, the reverse is true.
An inner join is the default type of join. As a result, it’s common
to omit the INNER keyword from a SELECT statement for an
inner join.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 25
The INSERT statement
Syntax
INSERT INTO table-name [(column-list)]
VALUES (value-list)
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 26
The UPDATE statement
Syntax
UPDATE table-name
SET expression-1 [, expression-2] ...
WHERE selection-criteria
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 27
The DELETE statement
Syntax
DELETE FROM table-name
WHERE selection-criteria
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 28
How to insert, update, and delete data
INSERT, UPDATE, and DELETE statements modify the data
that’s stored in a database, but they don’t return a result set.
Instead, they return the number of rows that were affected by the
query.
These statements are sometimes referred to as action queries.
Murach's Java Servlets/JSP (3rd Ed.), C11 © 2014, Mike Murach & Associates, Inc. Slide 29