Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
murachs
MySQL
2nd Edition
(Chapter 2)
Thanks for downloading this chapter from Murachs MySQL (2nd Edition). We hope
it will show you how easy it is to learn from any Murach book, with its paired-pages
presentation, its how-to headings, its practical coding examples, and its clear, concise
style.
To view the full table of contents for this book, you can go to our website. From there,
you can read more about this book, you can find out about any additional downloads that
are available, and you can review our other books on related topics.
Thanks for your interest in our books!
A great first book into SQL: From all the SQL books I looked over, this has by
far the best division of chapters and the best order for learning.
Posted at an online bookseller
As a developer with almost ten years MySQL experience, I still picked up a lot
of new detail on things I thought I knew. Every development shop that works
with MySQL should have a copy of this book.
David Bolton, C/C++/C# Guide, About.com
Ive found that many technical books go into laborious details that make them
difficult to read, let alone use, but Murachs MySQL has already proven to be
helpful in solving several challenges Ive encountered on my current MySQL
project.
Posted at an online bookseller
I was amazed at how much information was packed into this book. The style
made it really easy to read and understand the information.... I learned a lot
of new MySQL ideas reading this book, and I will be using it frequently as a
reference.
Paul Turpin, Southeastern Inter-Relational Database Users Group
2
How to use
MySQL Workbench
and other development tools
In the last chapter, you learned about some of the SQL statements that you can
use to work with the data in a relational database. Before you learn the details
of coding these statements, however, you need to learn how to use MySQL
Workbench to enter and execute SQL statements. In addition, you should learn
how to use the MySQL Reference manual, and you should at least be familiar
with the MySQL Command Line Client.
Perspective............................................................................70
42
Section 1
An introduction to MySQL
Chapter 2
Description
The Home tab of MySQL Workbench is divided into three main sections: MySQL
Connections, Models, and Shortcuts.
You can use the MySQL Connections section to start and stop the database server
and to code and run SQL statements.
You can use the Models section to design databases.
You can use the Shortcuts section to open some other MySQL tools including some
MySQL blogs and forums.
You can return to the Home tab by clicking the tab with the house icon. This tab is
always displayed in the top left corner of the Workbench window.
Figure 2-1
43
44
Section 1
An introduction to MySQL
Chapter 2
Description
To connect as the root user to an instance of MySQL thats running on the local
host computer, click the stored connection named Local instance MySQL56, and
enter the password for the root user if prompted.
To save the password for a connection so you dont have to enter it every time,
check the Save password in vault option when youre prompted for your
password.
To clear the password from the vault so you are prompted for your password,
right-click the connection, select the Edit Connection item, click the Clear button,
and click the Close button.
To edit the connection parameters for a connection, right-click the connection,
select the Edit Connection item, enter the connection parameters, and click the
Close button. This lets you specify the username, the host address, the port number,
and other connection parameters.
To add a new connection to the Home tab, click the + icon to the right of MySQL
Connections, enter the connection parameters, and click the OK button. Then, the
connection appears in the list of connections.
Figure 2-2
45
46
Section 1
An introduction to MySQL
Chapter 2
Description
After you install the MySQL, the database server usually starts automatically each
time you start your computer.
The database server can also be referred to as the database service or the database
engine.
If you arent able to use Workbench to start and stop the database server, you may
need to edit your connection so it points to the correct instance of MySQL. To do
that, click the Home tab, right-click the connection, select the Edit Connection
item, click the System Profile tab, and edit the service name. For MySQL 5.6 on
Windows, the service name is typically MySQL56.
Figure 2-3
47
48
Section 1
An introduction to MySQL
Chapter 2
Description
Each database (or schema) provides access to the database objects that are
available. These database objects include tables, views, stored procedures, and
functions.
On some systems, the Navigator window provides Management and Schemas tabs
that you can use to display the Management and Schemas categories. On other
systems, the Navigator window displays the Management category above the
Schemas category.
To display the databases for the current connection, you can use the Navigator
window to view the Schemas category.
To navigate through the database objects for a database, click the arrows to the left
of each of the nodes in the Navigator window to expand or collapse the node.
To work with a node or an object, right-click the node or object and select a
command from the resulting menu.
Figure 2-4
49
50
Section 1
An introduction to MySQL
Chapter 2
The data for the Invoices table displayed in the Result grid
Result grid
Description
To view the data for a table, right-click the table in the Navigator window and
select the Select Rows - Limit 1000 command to display it in a Result grid.
To edit the data for a table, view the data. Then, you can use the buttons at the top
of the Result grid to insert, update, and delete rows.
To apply the changes to the table, click the Apply button at the bottom of the tab.
To cancel the changes, click the Revert button.
Figure 2-5
51
52
Section 1
An introduction to MySQL
Chapter 2
Description
To view the column definitions for a table, right-click the table name in the
Navigator window and select the Alter Table command. Then, select the Columns
tab at the bottom of the window thats displayed to view the column definitions for
the table.
To edit the column definitions for a table, view the column definitions. Then, you
can use the resulting window to add new columns and modify and delete existing
columns.
For more information about creating and modifying tables, see chapter 11.
Figure 2-6
53
54
Section 1
An introduction to MySQL
Similarly, if you havent selected the correct database, youll get an error
message that says the table doesnt exist. For example, if the EX database is
selected when you attempt to retrieve data from the Vendors table, youll get an
error message like this:
Error Code: 1146. Table 'ex.vendors' doesn't exist
Chapter 2
Execute Current
Statement button
SQL
editor
Result
grid
Description
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 tab of the Navigator
window. This displays the selected database in bold.
To enter a SQL statement, type it into the SQL editor.
As you enter the text for a statement, the SQL editor 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 Result grid below the SQL tab.
Figure 2-7
55
56
Section 1
An introduction to MySQL
Chapter 2
Description
The SQL Additions tab contains snippets. Snippets contain the syntax for many
common SQL statements. You can use the snippets to guide you as you create a
SQL statement. You can also create your own snippets and save them for later use.
The SQL Additions tab is displayed to the right of the SQL Editor tab by default. If
this tab isnt displayed, you can display it by clicking the rightmost button ( ) at
the right side of the SQL Editor toolbar.
The snippets are organized into categories. To display any category of snippets,
select the category from the drop-down list at the top of the SQL Additions tab.
To enter a snippet into a SQL editor, select the snippet and then click the Insert
Snippet button at the top of the SQL Additions tab. Then, edit the snippet code so
its appropriate for your SQL statement.
To replace code in the SQL editor with a snippet, select the code, select the snippet
you want to replace it with, and then click the Replace Current Text button.
To create your own snippet, enter the code for the snippet into a SQL editor. Then,
select the category where you want to save the snippet, click the Add New Snippet
button, and enter a name for the snippet.
To delete a snippet, right-click it in the Snippets tab and select the Delete Snippet
item.
Figure 2-8
57
58
Section 1
An introduction to MySQL
Chapter 2
Description
If an error occurs during the execution of a SQL statement, MySQL Workbench
displays a message in the Output tab that includes an error code and a brief description of the error.
Most errors are caused by incorrect syntax and can be corrected without any
additional assistance. Otherwise, you can usually get more information about
an error by searching for the error code or description in the MySQL Reference
Manual or on the Internet.
Figure 2-9
59
60
Section 1
An introduction to MySQL
Chapter 2
Description
A SQL script is a file that contains one or more SQL statements.
To open a file that contains a SQL script, click the Open SQL Script File button in
the SQL Editor toolbar or press the Ctrl+Shift+O keys. Then, use the Open SQL
Script dialog box to locate and open the SQL script.
When you open a SQL script, MySQL Workbench displays it in its own SQL
Editor tab. To switch between open scripts, select the appropriate tab.
To cut, copy, and paste code from one SQL script to another, use the standard
techniques.
To save a SQL statement to a script file, click the Save button in the SQL Editor
toolbar or press Ctrl+S. Then, use the Save SQL Script dialog box to specify a
location and name for the file.
To save a script youve modified to a new file, press the Ctrl+Shift+S keys or select
the FileSave Script As command.
Figure 2-10
61
62
Section 1
An introduction to MySQL
Chapter 2
Description
When you code a script that contains more than one statement, you must 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 ( ) thats 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 Result grid. To switch between these Result grids, you can
click on the tabs that are displayed below the current Result grid.
To execute one SQL statement within a script, move the insertion point into that
statement and press the Ctrl+Enter keys or click the Execute Current Statement
button ( ). If the statement retrieves data, the data is displayed in a Result grid.
To execute two or more statements within a script, select them in the editor and
then press the Ctrl+Shift+Enter keys or click the Execute SQL Script button.
Figure 2-11
63
64
Section 1
An introduction to MySQL
Chapter 2
Description
To view the MySQL Reference Manual, go to the MySQL website and select the
correct version of the manual.
To view a chapter, click the link for the chapter in the table of contents on the right
side of the page.
To return to the Home page for the manual, click the link for the manual thats
displayed across the top of the current page.
To search for a particular word or phrase, type the word or phrase in the Search
manual text box on the left side of the page and click the Go button. Then, you can
scroll through the results and click links to get more information.
You can also download the MySQL Reference Manual in several different formats.
However, it typically makes sense to use it online.
Figure 2-12
65
66
Section 1
An introduction to MySQL
From this prompt, you can enter any statement that works with MySQL. When
youre done, you can exit the MySQL Command Line Client by entering exit
or quit followed by a semicolon.
Chapter 2
How to start the MySQL Command Line Client from the command line
For Windows
cd \Program Files\MySQL\MySQL Server 5.6\bin
mysql u root -p
Examples
mysql u ap_tester -p
mysql h localhost u root p
mysql h murach.com u ap_tester p
Description
MySQL provides a command-line client program called the MySQL Command
Line Client that lets you enter SQL statements that work with MySQL databases.
This program is also known as the MySQL command line.
For Windows, use a Command Prompt window to start the MySQL Command Line
Client.
For Mac OSX, use a Terminal window to start the MySQL Command Line Client.
To stop the MySQL Command Line Client, enter exit or quit at the command
line, followed by a semicolon.
Figure 2-13
67
68
Section 1
An introduction to MySQL
This shows that the MySQL Command Line Client is waiting for you to finish
your statement. To finish a statement and execute it, you just type a semicolon
and press the Enter key.
Figure 2-14 shows how to execute three SQL statements. Here, I entered all
three of these statements in lowercase letters. Thats because SQL isnt casesensitive, and lowercase letters are easier to type.
To list the names of the databases stored on a server, you use the SHOW
DATABASES statement as illustrated by the first example. Here, the ap, ex,
and om databases are the databases that are created when you install our
downloadable databases as described in appendixes A and B. The
information_schema, performance_schema, and mysql databases are
internal databases that are used by the MySQL server. And the test database is
a test database that comes with MySQL.
To select the database that you want to work with, you can enter a USE
statement as illustrated by the second example. Here, the AP database is
selected, and the message after this statement says Database changed to
indicate that the statement was successful. After you select a database, the
commands and statements that you enter will work with that database.
To retrieve data from the database, you use a SELECT statement as illustrated in the third example. Here, the vendor_name column from the Vendors
table is displayed. Note, however that the result set is limited to only the first
five rows. When you successfully execute a SELECT statement, the MySQL
Command Line Client displays a message giving the number of rows that are
included in the result set and the amount of time it took to run the query.
Chapter 2
Description
You can use the MySQL Command Line Client to work with any of the databases
running on the database server. To do that, you can use any SQL statement that
works with a MySQL database.
To execute a SQL statement, type the statement on the command line, followed by
a semicolon. Then, press the Enter key.
To show a list of all available databases, you can use the SHOW DATABASES
statement.
To select the database that you want to work with, you can use the USE statement.
SQL statements arent case-sensitive. As a result, when using the MySQL
Command Line Client, most programmers enter their statements in lowercase
letters because theyre easier to type.
Figure 2-14
How to use the MySQL Command Line Client to work with a database
69
70
Section 1
An introduction to MySQL
Perspective
In this chapter, you learned how to use MySQL Workbench to start and
stop a MySQL server and to enter and execute SQL statements. With that
as background, youre ready to go on to the next chapter, where youll start
learning the details of coding your own SQL statements.
Terms
MySQL Workbench
database server
database service
database engine
database object
schema
snippet
SQL script
MySQL Reference manual
MySQL Command Line Client
Exercises
In these exercises, youll use MySQL Workbench to review the tables in the AP
database. In addition, youll use MySQL Workbench to enter SQL statements
and run them against these tables.
Chapter 2
5. Navigate through the database objects and view the column definitions for at
least the Vendors and Invoices tables.
8. Delete the e at the end of vendor_name and run the statement again. Note the
error number and the description of the error.
9. Open another SQL Editor tab. Then, enter and run this statement:
SELECT COUNT(*) AS number_of_invoices,
SUM(invoice_total) AS grand_invoice_total
FROM invoices
71
Handle basic DBA tasks like monitoring, configuring, securing, and backing up a
database
Pick up new skills whenever you want to or need to by focusing on material thats
new to you
Look up coding details or refresh your memory on forgotten details when youre in
the middle of developing a MySQL application
Loan to your colleagues who are always asking you questions about MySQL
programming
To get your copy, you can order online at www.murach.com or call us at
1-800-221-5528 (toll-free in the U.S. and Canada). And remember, when you order
directly from us, this book comes with my personal guarantee:
100% Guarantee
You must be satisfied. Each book you buy directly
from us must outperform any competing book or
course youve ever tried, or send it back within 60
days for a full refundno questions asked.
Thanks for your interest in Murach books!