<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1888076610204930038</id><updated>2012-02-16T00:00:27.596-08:00</updated><category term='developer.com'/><category term='oracle.automation.inc'/><category term='dev.mysql'/><title type='text'>mysql</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>47</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-4418385420645866970</id><published>2010-01-13T22:02:00.000-08:00</published><updated>2008-01-13T23:30:34.305-08:00</updated><title type='text'>commands</title><content type='html'>$ mysqldump -u root --all-databases &gt; alldatabases.sql&lt;br /&gt;$ mysqldump -u username -ppassword --databases databasename &gt;/tmp/databasename.sql (Dump one database for backup.)&lt;br /&gt;$ mysqldump -c -u username -ppassword databasename tablename &gt; /tmp/databasename.tablename.sql (Dump a table from a database)&lt;br /&gt;$ mysql -u root &lt; alldatabases.sql&lt;br /&gt;$ mysql -u root [databasename] &lt; youfile.sql&lt;br /&gt;&lt;br /&gt;$ [mysql dir]/bin/mysql -h hostname -u root -p&lt;br /&gt;&lt;br /&gt;$ show databases;&lt;br /&gt;$ create database [databasename];&lt;br /&gt;$ drop database [database name];&lt;br /&gt;$ use [db name];&lt;br /&gt;&lt;br /&gt;$ show tables;&lt;br /&gt;$ create table [table name] (column1 integer, column2 char(10), column3 VARCHAR(20), datestamp DATE, timestamp time);&lt;br /&gt;$ create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');&lt;br /&gt;$ describe [table name];&lt;br /&gt;$ desc [table name];&lt;br /&gt;$ show columns from [table name];&lt;br /&gt;&lt;br /&gt;$ INSERT INTO [table name] (Host,User,Password) VALUES ('%', 'user', PASSWORD('password'));&lt;br /&gt;&lt;br /&gt;$ SELECT * FROM [table name];&lt;br /&gt;$  SELECT * FROM [table name] WHERE [field name] = "whatever";&lt;br /&gt;$ SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';&lt;br /&gt;$ SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;&lt;br /&gt;$ SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';&lt;br /&gt;$ SELECT DISTINCT [column name] FROM [table name];&lt;br /&gt;$ SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC | ASC;&lt;br /&gt;$ SELECT COUNT(*) FROM [table name];&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;$ SELECT * FROM [table name] WHERE rec RLIKE "^a$";&lt;br /&gt;$ SELECT * FROM [table name] WHERE rec RLIKE "^a$"; (Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.)&lt;br /&gt;&lt;br /&gt;$ alter table [table name] add column [new column name] varchar (20), add column [new column name 2] time;&lt;br /&gt;$ alter table [table name] drop column [column name];&lt;br /&gt;$ alter table [table name] change [old column name] [new column name] varchar (50);&lt;br /&gt;$ alter table [table name] add unique ([column name]);&lt;br /&gt;$ alter table [table name] modify [column name] VARCHAR(3);&lt;br /&gt;$ alter table [table name] drop index [colmn name];&lt;br /&gt;&lt;br /&gt;$ LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3); (Load a CSV file into a table.)&lt;br /&gt;&lt;br /&gt;$ DELETE from [table name] where [field name] = 'whatever';&lt;br /&gt;&lt;br /&gt;$ UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';&lt;br /&gt;&lt;br /&gt;$ drop table [table name];&lt;br /&gt;&lt;br /&gt;$ SELECT SUM(*) FROM [table name];&lt;br /&gt;&lt;br /&gt;$ select lookup.illustrationid, lookup.personid,person.birthday from lookup&lt;br /&gt;left join person on  lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;&lt;br /&gt;&lt;br /&gt;$ FLUSH PRIVILEGES;  (Update database permissions/privilages.)&lt;br /&gt;&lt;br /&gt;--- switch to mysql db&lt;br /&gt;&lt;br /&gt;$ INSERT INTO [table name] (Host,User,Password) VALUES('%','user',PASSWORD('password'));&lt;br /&gt;$ mysqladmin -u root -h hostname.blah.org -p password 'new-password' (from unix shell)&lt;br /&gt;$ SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere'); (from mysql prompt)&lt;br /&gt;$ grant usage on *.* to bob@localhost identified by 'passwd';&lt;br /&gt;$ INSERT INTO [table name] (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES  ('%','databasename','username','Y','Y','Y','Y','Y','N');&lt;br /&gt;&lt;br /&gt;OR&lt;br /&gt;&lt;br /&gt;$  grant all privileges on databasename.* to username@localhost;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-4418385420645866970?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/4418385420645866970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=4418385420645866970' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4418385420645866970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4418385420645866970'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2010/01/commands.html' title='commands'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-4142836874351122121</id><published>2008-01-13T22:05:00.000-08:00</published><updated>2008-01-13T22:12:40.192-08:00</updated><title type='text'>upgrade mysql 4.1.15 to mysql 4.1.22</title><content type='html'>Download and install MySql 4.1.22&lt;ul&gt;&lt;li&gt;File name: mysql-noinstall-4.1.22-win32.zip&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;     b. Just unzip it under 'C:\dev\tools'&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;  Expoprt all existsitng databases (mysqldump -u root --all-databases &gt; alldatabases.sql)        Shut down mysql server&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;       Copy my.cnf file from &lt;oldmysqlhomedir&gt; to &lt;newmysqlhomedir&gt;        Edit the copied my.cnf file and replace all directory references to point to the new &lt;mysqlhomedir&gt;        Modify !MySql server start-up and shut-down shortcuts to point to the new &lt;mysqlhomedir&gt;&lt;/mysqlhomedir&gt;&lt;/mysqlhomedir&gt;&lt;/newmysqlhomedir&gt;&lt;/oldmysqlhomedir&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;oldmysqlhomedir&gt;&lt;newmysqlhomedir&gt;&lt;mysqlhomedir&gt;&lt;mysqlhomedir&gt;       Test Startup/Shutdown scripts (Allow it in the firewall!!!)&lt;br /&gt;&lt;/mysqlhomedir&gt;&lt;/mysqlhomedir&gt;&lt;/newmysqlhomedir&gt;&lt;/oldmysqlhomedir&gt;&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;oldmysqlhomedir&gt;&lt;newmysqlhomedir&gt;&lt;mysqlhomedir&gt;&lt;mysqlhomedir&gt;Import all databases (mysql -u root &lt; &lt;/mysqlhomedir&gt;&lt;/mysqlhomedir&gt;&lt;/newmysqlhomedir&gt;&lt;/oldmysqlhomedir&gt;alldatabases.sql)&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Restart Mysql server&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Update computer's Environment path (if mysql command is to be global) (Allow it in firewall!!!) &lt;/li&gt;&lt;/ul&gt;&lt;oldmysqlhomedir&gt;&lt;newmysqlhomedir&gt;&lt;mysqlhomedir&gt;&lt;mysqlhomedir&gt;&lt;br /&gt;&lt;/mysqlhomedir&gt;&lt;/mysqlhomedir&gt;&lt;/newmysqlhomedir&gt;&lt;/oldmysqlhomedir&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-4142836874351122121?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/4142836874351122121/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=4142836874351122121' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4142836874351122121'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4142836874351122121'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2008/01/upgrade-mysql-4115-to-mysql-4122.html' title='upgrade mysql 4.1.15 to mysql 4.1.22'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-958029471819644560</id><published>2007-04-15T22:34:00.000-07:00</published><updated>2007-04-15T23:35:37.490-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='developer.com'/><title type='text'>Using JDBC with MySQL 3</title><content type='html'>&lt;h2&gt;&lt;span&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#000000;"&gt;JDBC Programs&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;&lt;br /&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;With all of the above as preparation, it is now time to learn how to write JDBC  programs to administer and manipulate the data on the MySQL database server.&lt;/span&gt;&lt;p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Three separate programs&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will explain three programs.  The first program, named &lt;b&gt;Jdbc11&lt;/b&gt;  shows how to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Log onto the server as the administrator named &lt;b&gt;root&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Create a new database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Register a new user named &lt;b&gt;auser&lt;/b&gt; on the database named &lt;b&gt;JunkDB  &lt;/b&gt;with six different privileges and a password of &lt;b&gt;drowssap&lt;/b&gt;.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The second program named &lt;b&gt;Jdbc12&lt;/b&gt; shows how to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Log onto the server as the administrator named &lt;b&gt;root&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Revoke the privileges of and remove the user named &lt;b&gt;auser&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Delete the database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The third program named &lt;b&gt;Jdbc10&lt;/b&gt; shows how to log onto the server as  the user named &lt;b&gt;auser&lt;/b&gt; and to manipulate the database named &lt;b&gt;JunkDB&lt;/b&gt;  in a variety of ways.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will break each of these programs down into fragments and discuss the  fragments.  Complete listings of all the programs are shown in Listings 40,  41, and 42  near the end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Jdbc11 - Create a database and make a new user&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The purpose of the program named &lt;b&gt;Jdbc11&lt;/b&gt; is to log onto the master  database named &lt;b&gt;mysql&lt;/b&gt; as the default administrator named &lt;b&gt;root&lt;/b&gt; whose  password is blank in order to perform the following updates on the MySQL  database server:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Create a new database named JunkDB.&lt;/li&gt;  &lt;li&gt;Create a new user named &lt;b&gt;auser&lt;/b&gt; with a password of &lt;b&gt;drowssap&lt;/b&gt;   with six different privileges on the database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The output, or lack thereof&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;These two operations produce no visible output when successful. However, they  produce error messages in the output when unsuccessful.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note, however, that print statements in the program produce several   lines of output that are independent of the operations being performed on   the database server.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Server must be running&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The MySQL server must be running on &lt;b&gt;localhost&lt;/b&gt; before this program is  started.  Instructions for starting and stopping the database server were  provided earlier &lt;i&gt;(see Listing 1, Figure 1, Listing 2, and Figure 2).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The program was tested using Java SDK 1.4.2 under WinXP, MySQL version 4.0.21-win,  and JDBC connector version mysql-connector-java-3.0.15-ga.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;&lt;a name="Critical_steps_in_using_JDBC"&gt;Critical steps in using JDBC&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There are five critical steps in using JDBC to manipulate a database:&lt;/span&gt;&lt;/p&gt; &lt;ol&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Load and register the JDBC driver classes &lt;i&gt;(programming interface)&lt;/i&gt; for the database server that   you intend to use. &lt;/li&gt;  &lt;li&gt;Get a &lt;b&gt;Connection&lt;/b&gt; object that represents a connection to the database   server &lt;i&gt;(analogous to logging onto the server).&lt;/i&gt; &lt;/li&gt;  &lt;li&gt;Get one or more &lt;b&gt;Statement&lt;/b&gt; objects for use in manipulating the database.   &lt;/li&gt;  &lt;li&gt;Use the &lt;b&gt;Statement&lt;/b&gt; objects to manipulate the database. &lt;/li&gt;  &lt;li&gt;Close the connection to the database. &lt;/li&gt; &lt;/span&gt;&lt;/ol&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will highlight these five steps in the discussion of the sample program  that follows.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Beginning of class definition for Jdbc11&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The first program fragment for the program named &lt;b&gt;Jdbc11&lt;/b&gt; is shown in Listing 3.  The entire program is  shown in Listing 40 near the end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;public class Jdbc11 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 3&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 3 is straightforward, showing the beginning of the class,  the beginning of the &lt;b&gt;main&lt;/b&gt; method, and a print statement.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 3 also declares a local variable of type &lt;b&gt;Statement&lt;/b&gt;.  I  will have more to say about the &lt;b&gt;Statement&lt;/b&gt; interface later.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Register the JDBC driver for MySQL&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 4 shows the statement that implements the first &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt;critical step&lt;/a&gt; listed earlier &lt;i&gt; (load and register the JDBC driver classes).&lt;/i&gt;   This statement registers the MySQL driver classes with the Java program, making it possible for this program to  manipulate data on the MySQL server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 4&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Reference to the driver class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The following statement appears in the MySQL Connector documentation, Section  2.2.1. entitled &lt;i&gt;Setting the CLASSPATH (For Standalone Use).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"If you are going to use the driver with the JDBC DriverManager, you   would use "com.mysql.jdbc.Driver" as the class that implements   java.sql.Driver."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This information is also provided in the connector documentation in Section  2.2.2. entitled &lt;i&gt;Driver Class Name and JDBC URL Format.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The Driver interface&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Note the reference to the &lt;b&gt;Driver&lt;/b&gt; interface in the above quotation.   Here is some of what Sun has to say about the &lt;b&gt;Driver&lt;/b&gt; interface:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"The interface that every driver class must implement. &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;The Java SQL framework allows for multiple database drivers. &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Each driver should supply a class that implements the &lt;b&gt;Driver&lt;/b&gt;   interface. &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;The &lt;b&gt;DriverManager&lt;/b&gt; will try to load as many drivers as it can   find and then for any given connection request, it will ask each driver in   turn to try to connect to the target URL. ...&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;When a &lt;b&gt;Driver&lt;/b&gt; class is loaded, it should create an instance of   itself and register it with the &lt;b&gt;DriverManager&lt;/b&gt;. This means that a user   can load and register a driver by calling &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Class.forName("foo.bah.Driver")"&lt;/i&gt;  &lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The name of the driver class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In order to use a JDBC program with a specific database server, you must  obtain the name of this critical driver class from the database vendor &lt;i&gt;(or from some  third party that supports the database server).  &lt;/i&gt;You must then cause your program to load the class.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This is  the class that connects the other classes in the connector package to the Java  program.  Without it, the Java program would be unable to communicate  successfully with the database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Loading the driver class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The statement in Listing 4 causes this class to be loaded as described in the  Sun documentation quoted above.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(If you are unfamiliar with the use of the &lt;b&gt;forName&lt;/b&gt; method of   the class named &lt;b&gt;Class&lt;/b&gt;, see  &lt;a href="http://www.developer.com/java/other/article.php/1025601"&gt;The   Essence of OOP using Java: Static Members&lt;/a&gt; for a brief introduction to   the class named&lt;b&gt; Class&lt;/b&gt;.  Then open your  &lt;a href="http://www.google.com/advanced_search?hl=en"&gt;Google&lt;/a&gt; search   engine, set the number of results to 100, and search for all of the keywords  &lt;b&gt;java forname richard baldwin&lt;/b&gt;.  This should point you to several   previous lessons that I have published that discuss this topic.  If you   don't find what you need there, click on the link on the bottom of the   last Google page that reads &lt;b&gt;repeat the search with the omitted results   included&lt;/b&gt; to see even more lessons.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Specification of Driver class as a String&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There are several alternative ways to register the &lt;b&gt;Driver&lt;/b&gt; class, only  one of which is shown in Listing 4.  The statement in Listing 4 makes it possible to specify the &lt;b&gt;Driver&lt;/b&gt; class as a &lt;b&gt;String&lt;/b&gt;.  The  primary advantage of this approach is that this string can be obtained by the  program in a variety of ways at runtime.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Although the string was hard  coded into this simple program, that is not a requirement.  For a more  general program intended to be used with two or more database servers, this  string would most likely be provided as some form of user input.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The URL of the database server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;MySQL and other similar database engines  behave as servers on a network. They are identified by a URL much as other types  of servers &lt;i&gt;(such as HTTP servers and FTP servers)&lt;/i&gt; are identified. The  next fragment defines the URL for the MySQL database server that I used in this  sample program.&lt;/strong&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 5 defines the URL of the master database named &lt;b&gt;mysql&lt;/b&gt;  on the  MySQL database server residing on &lt;b&gt;localhost&lt;/b&gt; and servicing the default  port number 3306.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(I could have omitted the default port number from the URL, but I   decided to include it to remind me to mention it.   &lt;strong style="font-weight: 400;"&gt;Note that the "//" characters shown to the   right of "mysql:" form part of the URL. They are not comment indicators.&lt;/strong&gt;)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/mysql";&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 5&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The URL format is provided in the connector documentation in Section  2.2.2. entitled &lt;i&gt;Driver Class Name and JDBC URL Format.&lt;/i&gt;  The URL  format contains several optional elements.  This URL will be referenced in the statement  in Listing 6 for the purpose of getting a connection to the database.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Get a connection to the database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 6 implements the second &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt;critical step&lt;/a&gt; listed earlier &lt;i&gt;(get  a connection object).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 6 gets a connection to the database at the specified URL &lt;i&gt;(mysql on  localhost port 3306)&lt;/i&gt; for a user named &lt;b&gt;root&lt;/b&gt; with a blank password.   As you are already aware, this user is the default administrator having full privileges to do anything,  including creating new databases and registering new users on those databases. &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                                url,"root", "");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 6&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In effect, Listing 6 logs the JDBC program onto the MySQL database server in  a manner that is analogous to the first line in Figures 3, 4, and 5 as well as  the statements in the batch files shown in Listings 32, 34, 36, and 38.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The getConnection method&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;The &lt;/strong&gt;&lt;strong&gt;getConnection&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  method is a static method of the &lt;/strong&gt;&lt;strong&gt;DriverManager&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  class. When &lt;/strong&gt;&lt;strong&gt;getConnection&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  is invoked, the &lt;/strong&gt;&lt;strong&gt;DriverManager&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  will attempt to locate a suitable driver from among those loaded at  initialization and those loaded explicitly using the same classloader as the  current applet or application.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There are several overloaded versions of the &lt;b&gt;getConnection&lt;/b&gt; method.   The version used in Listing 6 attempts to establish a connection to the given  database URL for a specific user with a specific password.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;If the attempt to get a connection to the database server is successful, the  method returns an object of type &lt;b&gt;Connection&lt;/b&gt;. In this program, a reference to the  &lt;b&gt;Connection&lt;/b&gt; object is stored in the reference variable named &lt;b&gt;con&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;If the attempt is not successful, an exception of type &lt;b&gt;SQLException&lt;/b&gt;  will be  thrown.  Information pertinent to the nature of the problem will be  encapsulated in the &lt;b&gt;SQLException&lt;/b&gt; object.&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As you will see later, SQL statements are executed and results are returned  within the context of a connection.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Display some information&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 7 is not critical to the program.  This code simply displays information about the URL and the  connection.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 7&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Get a Statement object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 8 implements the third &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt;critical step&lt;/a&gt; listed earlier &lt;i&gt;(get  one or more &lt;b&gt;Statement&lt;/b&gt; objects).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This code invokes the&lt;b&gt; createStatement&lt;/b&gt; method of the &lt;b&gt; Connection&lt;/b&gt; interface to get an object of type &lt;b&gt;Statement&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 8&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;Recall that &lt;/strong&gt;&lt;strong&gt;con&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  is a reference to an object of type &lt;/strong&gt;&lt;strong&gt;Connection&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;.   A &lt;/strong&gt;&lt;strong&gt;Connection&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt; object  defines a connection &lt;i&gt;(session)&lt;/i&gt; with a specific database.  SQL  statements are executed and results are returned within the context of a  connection.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;According to Sun, a Statement object is:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"... used for executing a static SQL statement and returning the   results it produces."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;strong&gt;The results that are returned&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;The results, if any, are returned in the  form of a &lt;/strong&gt;&lt;strong&gt;ResultSet&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  object.  I will have more to say about the &lt;/strong&gt;&lt;strong&gt;ResultSet&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  interface later in conjunction with the discussion of the program named &lt;/strong&gt; &lt;strong&gt;Jdbc10&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-style: italic; font-weight: 400;"&gt;(The SQL commands   used in this program don't return any results.)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Methods of the Statement interface&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;The &lt;/strong&gt;&lt;strong&gt;Statement&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  interface declares many methods that can be used to access the database server  and to manipulate the data in the  database. One of those methods is &lt;/strong&gt;&lt;strong&gt;executeUpdate&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;,  which will be used in this program.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;The &lt;/strong&gt;&lt;strong&gt;executeUpdate &lt;/strong&gt; &lt;strong style="font-weight: 400;"&gt;method has a single &lt;/strong&gt;&lt;strong&gt;String&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  parameter.  This parameter must be a valid SQL command. The method is used to execute SQL  INSERT, UPDATE or DELETE statements. In addition, other SQL statements that  return nothing can be executed using this method.&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Create the new database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 9 implements the fourth &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt;critical step&lt;/a&gt; in the list provided  earlier, &lt;i&gt;(use of a &lt;b&gt;Statement &lt;/b&gt;object to manipulate the database)&lt;/i&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 9 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method on the &lt;b&gt;Statement&lt;/b&gt;  object to create the new database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;                      "CREATE DATABASE JunkDB");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 9&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The executeUpdate method&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Sun has this to say about the &lt;b&gt;executeUpdate&lt;/b&gt; method:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"public int &lt;b&gt;executeUpdate&lt;/b&gt;(String sql) throws SQLException&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Executes the given SQL statement, ... &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Parameters: sql - an SQL INSERT, UPDATE or DELETE statement or an SQL   statement that returns nothing &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Returns: either the row count for INSERT, UPDATE or DELETE statements,   or 0 for SQL statements that return nothing."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The method parameter is an SQL command&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Note the similarity of the method parameter in Listing 9 to the interactive  input shown in Figure 4 and the contents of the text file shown in Listing 33.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In all three cases, an SQL command is invoked on the database server to cause a  new database named &lt;b&gt;JunkDB&lt;/b&gt; to be created.  This SQL command returns  nothing, so it is suitable for use with the &lt;b&gt;executeUpdate&lt;/b&gt; method  described above.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In all three cases, the SQL command is invoked by the  default administrator named &lt;b&gt;root&lt;/b&gt; who has the ability to create new  databases.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Make a new user&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 10 also implements the fourth &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt;critical step&lt;/a&gt; listed earlier, &lt;i&gt; (use of a &lt;b&gt;Statement &lt;/b&gt;object to manipulate the database)&lt;/i&gt;. &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 10 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method once again to  make a new user named &lt;b&gt;auser&lt;/b&gt; who is capable of accessing the database named &lt;b&gt; JunkDB&lt;/b&gt; from &lt;b&gt;localhost&lt;/b&gt; using the password &lt;b&gt;drowssap&lt;/b&gt; with six  different privileges.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;         "GRANT SELECT,INSERT,UPDATE,DELETE," +&lt;br /&gt;         "CREATE,DROP " +&lt;br /&gt;         "ON JunkDB.* TO 'auser'@'localhost' " +&lt;br /&gt;         "IDENTIFIED BY 'drowssap';");&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 10&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Another SQL command&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Once again, note the similarity of the &lt;b&gt;executeUpdate&lt;/b&gt;  method parameter in Listing 10 to the interactive input shown in Figure 5 and to the  contents of the text file shown in Listing 35.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 5, Listing 10, and Listing 35 illustrate three different ways  for the default administrator named &lt;b&gt;root&lt;/b&gt; to invoke the same SQL command  on the database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Close the connection and terminate the program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 11 implements the fifth &lt;a href="http://www.developer.com/java/data/article.php/3417381#Critical_steps_in_using_JDBC"&gt; critical step&lt;/a&gt; listed earlier, closing the connection and terminating the program.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc11&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 11&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The results of running the program named Jdbc11&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Once this program has been run successfully, the MySQL database server contains a database  named &lt;b&gt;JunkDB&lt;/b&gt;, as well as a user named &lt;b&gt;auser&lt;/b&gt;, having various  privileges relative to that database with a password of &lt;b&gt;drowssap&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;At this point, it is possible to execute JDBC programs by which the user  named &lt;b&gt;auser&lt;/b&gt; manipulates the contents of the database named &lt;b&gt;JunkDB&lt;/b&gt;.   That will be the purpose of the program named &lt;b&gt;Jdbc10&lt;/b&gt;, which I will  explain later.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;First, however, I am going to show you how to write a JDBC  program to remove the user named &lt;b&gt;auser&lt;/b&gt; and to delete the database named &lt;b&gt;JunkDB&lt;/b&gt; from the MySQL database server.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;JDBC12 - Remove a user and delete a database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The purpose of the program named &lt;b&gt;Jdbc12&lt;/b&gt; is to log onto the master  database named &lt;b&gt;mysql&lt;/b&gt; as the default administrator named &lt;b&gt;root&lt;/b&gt; whose  password is blank in order to perform the following updates on the MySQL  database server:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Revoke the privileges of and remove a user named &lt;b&gt;auser&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Delete a database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;These two operations produce no visible output when successful. However, they  produce error messages in the output when unsuccessful.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Print statements in the program do produce output that is unrelated   to the operations listed above.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;A reversal&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program is the reverse of the program named &lt;b&gt;Jdbc11&lt;/b&gt;, discussed  earlier, which creates the database named &lt;b&gt;JunkDB&lt;/b&gt; and registers the user  named &lt;b&gt;auser&lt;/b&gt; on that database.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Server must be running&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The MySQL server must be running on &lt;b&gt;localhost&lt;/b&gt; before this program is  started.  Instructions for starting and stopping the database server were  provided earlier &lt;i&gt;(see Listing 1, Figure 1, Listing 2, and Figure 2).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Testing&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program was tested using Java SDK 1.4.2 under WinXP, MySQL version  4.0.21-win, and JDBC connector version mysql-connector-java-3.0.15-ga.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Beginning of the Jdbc12 class&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 12 shows the beginning of the class definition and the beginning of  the &lt;b&gt;main&lt;/b&gt; method for the program named &lt;b&gt;Jdbc12&lt;/b&gt;.  A complete  listing of the program is provided in Listing 41&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 12 is the same as the code discussed earlier for the  program named Jdbc11.  Therefore, I won't repeat that discussion.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;public class Jdbc12 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;&lt;br /&gt;     //Register the JDBC driver for MySQL.&lt;br /&gt;     Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;     //Define URL of database server for&lt;br /&gt;     // database named mysql on the localhost&lt;br /&gt;     // with the default port number 3306.&lt;br /&gt;     String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/mysql";&lt;br /&gt;&lt;br /&gt;     //Get a connection to the database for a&lt;br /&gt;     // user named root with a blank password.&lt;br /&gt;     // This user is the default administrator&lt;br /&gt;     // having full privileges to do anything.&lt;br /&gt;     Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                                url,"root", "");&lt;br /&gt;&lt;br /&gt;     //Display URL and connection information&lt;br /&gt;     System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;     //Get a Statement object&lt;br /&gt;     stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 12&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Revoke privileges and remove user named auser&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 13 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method four times in succession  to cause the database server to revoke privileges for and to remove the user  named &lt;b&gt;auser&lt;/b&gt;.  The SQL commands required to accomplish this were  discussed &lt;a href="http://www.developer.com/java/data/article.php/3417381#Removing_the_user_named_auser"&gt;earlier&lt;/a&gt; and illustrated  in Listings 36 and 37.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;         "REVOKE ALL PRIVILEGES ON *.* " +&lt;br /&gt;         "FROM 'auser'@'localhost'");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "REVOKE GRANT OPTION ON *.* " +&lt;br /&gt;         "FROM 'auser'@'localhost'");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "DELETE FROM mysql.user WHERE " +&lt;br /&gt;         "User='auser' and Host='localhost'");&lt;br /&gt;     stmt.executeUpdate("FLUSH PRIVILEGES");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 13&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Delete the database named JunkDB&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 14 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method to delete the database  named &lt;b&gt;JunkDB&lt;/b&gt;, using an SQL command discussed &lt;a href="http://www.developer.com/java/data/article.php/3417381#Deleting_the_database_named_JunkDB"&gt;earlier&lt;/a&gt; and illustrated in  Listings 38 and 39.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;                       "DROP DATABASE JunkDB");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 14&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Closing the connection and terminating the program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 15 closes the connection and terminates the program.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc12&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 15&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Jdbc10 - Manipulating the data in the database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The behavior of each of the two previous programs has been in the nature of  administering or managing the database server.  In particular, those programs added and  removed databases and users from the database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The user perspective&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The purpose of the program named  &lt;b&gt;Jdbc10&lt;/b&gt; is to illustrate the  ability to use JDBC to access a MySQL database server on &lt;b&gt;localhost&lt;/b&gt; and to  manipulate the data stored in that database.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Server must be running&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The MySQL server must be running on localhost before the program named &lt;b&gt; Jdbc10&lt;/b&gt; is started.  Instructions for starting and stopping the MySQL  database server were provided earlier &lt;i&gt;(see Listing 1, Figure 1, Listing 2,  and Figure 2).&lt;/i&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Server must be prepared&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In addition, a database named &lt;b&gt;JunkDB&lt;/b&gt; must have been created on the  server and a user named &lt;b&gt;auser&lt;/b&gt; must have been registered on that database  with a password of &lt;b&gt;drowssap&lt;/b&gt; before this program is started.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The user named &lt;b&gt;auser&lt;/b&gt; must have privileges that allow for the creation of tables in the  database and the insertion of data into the tables.  In addition the user  must be allowed to perform SELECT queries on the tables in the database.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Creating the database and adding the user&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The database may have been created and the user may have been added in at  least three different ways:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Performing manual data entry with the monitor program as illustrated in   Figures 4 and 5.&lt;/li&gt;  &lt;li&gt;Using the monitor program coupled with data input derived from a text   file as illustrated in Listings 32, 33, 34, and 35.&lt;/li&gt;  &lt;li&gt;Running the program named &lt;b&gt;Jdbc11&lt;/b&gt; discussed earlier.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Behavior of Jdbc10 program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Logs in as &lt;b&gt;auser&lt;/b&gt; with a password of &lt;b&gt;drowssap&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Accesses the database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Creates a table named &lt;b&gt;myTable&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Puts five rows of data into the table named &lt;b&gt;myTable&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Accesses the data in the table named &lt;b&gt;myTable&lt;/b&gt;.&lt;/li&gt;  &lt;li&gt;Displays the data.&lt;/li&gt;  &lt;li&gt;Deletes the table named &lt;b&gt;myTable&lt;/b&gt;.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Two different approaches are used to display the contents of the table.   The first approach displays all of the data in the table.  The second  approach displays only the data in a specific row in the table.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;A precaution&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As a precaution, before attempting to create the new table, the program  attempts to delete a table having the same name.  If a table having the  same name already exists as residue from a previous run, it is deleted.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;If it doesn't already exist when the attempt is made to delete it, an  exception is thrown. This exception is caught, displayed, and ignored.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The program output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program produces the output shown in Figure 6 under normal conditions  where the table named &lt;b&gt;myTable&lt;/b&gt; does not exist when the program is started &lt;i&gt;(the specifics regarding the Connection object may vary from one run to the  next)&lt;/i&gt;.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;URL: jdbc:mysql://localhost:3306/JunkDB&lt;br /&gt;Connection: com.mysql.jdbc.Connection@1430b5c&lt;br /&gt;java.sql.SQLException: Base table or view not&lt;br /&gt;found message from server: "Unknown table&lt;br /&gt;'mytable'"No existing table to delete&lt;br /&gt;Display all results:&lt;br /&gt;test_id= 1 str = One&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;test_id= 3 str = Three&lt;br /&gt;test_id= 4 str = Four&lt;br /&gt;test_id= 5 str = Five&lt;br /&gt;Display row number 2:&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 6&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Testing&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program was tested using Java SDK 1.4.2 under WinXP, MySQL version  4.0.21-win, and JDBC connector version mysql-connector-java-3.0.15-ga.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Beginning of the class definition&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The class definition begins in Listing 16.  A complete listing of the  program is shown in Listing 42 near the end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;public class Jdbc10 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;     ResultSet rs;&lt;br /&gt;&lt;br /&gt;     //Register the JDBC driver for MySQL.&lt;br /&gt;     Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 16&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Except for the declaration of a variable of type &lt;b&gt;ResultSet&lt;/b&gt;, the code  in Listing 16 is the same as the code in the two previous programs.  I will  have more to say about the &lt;b&gt;ResultSet&lt;/b&gt; type later.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Define the database URL&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 17 defines the URL of the database server and the database that will  be accessed later by this program.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/JunkDB";&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 17&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Note that the previous two programs accessed the master database named &lt;b&gt; mysql&lt;/b&gt;, whereas this program accesses the user database named &lt;b&gt;JunkDB&lt;/b&gt;.   Otherwise, the code in Listing 17 is the same as in the two previous programs.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Get a connection to the database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 18 gets a connection to the database.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                       url,"auser", "drowssap");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 18&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Recall that the code in Listing 18 is analogous to a specific user logging  onto a specific database using a specific password.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The previous two programs got a connection to the master database named &lt;b&gt; mysql&lt;/b&gt; for the default administrator named &lt;b&gt;root&lt;/b&gt; whose password was  blank.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This program gets a connection to the database named &lt;b&gt;JunkDB&lt;/b&gt; for the  user named &lt;b&gt;auser&lt;/b&gt; whose password is &lt;b&gt;drowssap&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Display some information and get a statement object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As in the previous two programs, Listing 19 displays some information about  the URL and the connection, and then gets a &lt;b&gt;Statement&lt;/b&gt; object.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      //Display URL and connection information&lt;br /&gt;     System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;     //Get a Statement object&lt;br /&gt;     stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 19&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Delete the table named myTable if it exists&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;A table named &lt;b&gt;myTable&lt;/b&gt; may already exist in the database named &lt;b&gt; JunkDB&lt;/b&gt; for some reason such as the premature ending of a previous run of  this program.  If the table already exists, it will not be possible to  create a new empty table having that name.  A requirement of this program  is to create an empty table named &lt;b&gt;myTable&lt;/b&gt; in a particular format.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Therefore, as a precaution, before attempting to create the new table, the  code in Listing 20 attempts to delete a table named &lt;b&gt;myTable&lt;/b&gt;.  If a table  having that name already exists, it is deleted.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;If it doesn't already exist when the attempt is made to delete it, an  exception is thrown. This exception is simply caught, displayed, and ignored. &lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      try{&lt;br /&gt;       stmt.executeUpdate("DROP TABLE myTable");&lt;br /&gt;     }catch(Exception e){&lt;br /&gt;       System.out.print(e);&lt;br /&gt;       System.out.println(&lt;br /&gt;                 "No existing table to delete");&lt;br /&gt;     }//end catch&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 20&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 20 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method, with which  you are already familiar.  Only the syntax of the SQL command provided as a  parameter to the method is new to this program.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Create the new table named myTable&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 22 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method to create a  new table named &lt;b&gt;myTable&lt;/b&gt;.  I will refer you to an SQL book for a full  understanding of the SQL command that is passed as a parameter to the method.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;           "CREATE TABLE myTable(test_id int," +&lt;br /&gt;                 "test_val char(15) not null)");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 22&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Briefly, the new table will have two columns.  The first column will be  named &lt;b&gt;test_id&lt;/b&gt; and will be designed to contain integer data.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The second column will be named &lt;b&gt;test_val&lt;/b&gt; and will be designed to  contain character data up&lt;i&gt; &lt;/i&gt;to 15 characters in length&lt;i&gt; &lt;/i&gt;(&lt;i&gt;in Java,  we might refer to that as &lt;b&gt;String&lt;/b&gt; data, except that there is no limit to  the length of a &lt;b&gt;String&lt;/b&gt; in Java).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Insert some values into the table&lt;/b&gt;&lt;/span&gt;  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 23 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method to insert one value into  each column of the first row in the table.&lt;/span&gt;&lt;/p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                   "test_val) VALUES(1,'One')");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 23&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The integer value &lt;i&gt; &lt;b&gt;1&lt;/b&gt; &lt;/i&gt;is inserted into the column named &lt;b&gt;test_id&lt;/b&gt;.   The three characters, &lt;i&gt; &lt;b&gt;One&lt;/b&gt;,&lt;/i&gt; are inserted into the column named &lt;b&gt;test_val&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Insert values into four more rows&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 24 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method four more times in  succession to insert values into the columns in rows 2 through 5.  You  should be able to examine the SQL commands and determine the values inserted  into the table.&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                   "test_val) VALUES(2,'Two')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                 "test_val) VALUES(3,'Three')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                  "test_val) VALUES(4,'Four')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                  "test_val) VALUES(5,'Five')");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 24&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The table is populated&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;At this point, the first five rows in the table named &lt;b&gt;myTable&lt;/b&gt; have  been populated with data.  The remaining code in the program will:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Access and display all of the data in the table.&lt;/li&gt;  &lt;li&gt;Display the data in a specific row in the table.&lt;/li&gt;  &lt;li&gt;Delete the table from the database.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Get another Statement object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 25 gets a different &lt;b&gt;Statement&lt;/b&gt; object, initialized  as shown by the parameters that are passed to the &lt;b&gt;createStatement&lt;/b&gt; method.&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt = con.createStatement(&lt;br /&gt;              ResultSet.TYPE_SCROLL_INSENSITIVE,&lt;br /&gt;                    ResultSet.CONCUR_READ_ONLY);&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 25&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I won't try to explain the meaning of the initialization parameters.   Rather, I will let you look them up in the Sun documentation for the &lt;b&gt; ResultSet&lt;/b&gt; interface, and then perhaps do further research in an SQL book.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;The &lt;/strong&gt;&lt;strong&gt;ResultSet&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  interface provides about ten symbolic constants that can be used as parameters  to this method. The values of the parameters exercise control over the behavior  of the &lt;/strong&gt;&lt;strong&gt; ResultSet&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt; object returned by a  subsequent query based on the &lt;/strong&gt;&lt;strong&gt;Statement&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  object.&lt;/strong&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Query the database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;All of the database operations to this point have been based on the use of  the &lt;b&gt;executeUpdate&lt;/b&gt; method of the &lt;b&gt;Statement&lt;/b&gt; interface.  Recall  that I told you earlier that the &lt;b&gt;executeUpdate&lt;/b&gt; method can be used to  execute SQL commands that don't return anything.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;We have now reached the point where we want to execute an SQL command that  does return something.  For this, we will invoke the &lt;b&gt;executeQuery&lt;/b&gt;  method on the &lt;b&gt;Statement&lt;/b&gt; object.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;What does Sun have to say?&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Here is part of what Sun has to say about this method:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"public ResultSet &lt;b&gt;executeQuery&lt;/b&gt;(String sql) throws SQLException&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Executes the given SQL statement, which returns a single &lt;b&gt;ResultSet&lt;/b&gt;   object. &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Parameters: sql - an SQL statement to be sent to the database,   typically a static SQL SELECT statement &lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;Returns: a &lt;b&gt;ResultSet&lt;/b&gt; object that contains the data produced by   the given query; never null"&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In other words, this method will execute the SQL command on the database and  encapsulate the returned values in an object of type &lt;b&gt;ResultSet&lt;/b&gt;.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;strong&gt;A ResultSet object&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;strong style="font-weight: 400;"&gt;A &lt;/strong&gt;&lt;strong&gt;ResultSet&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  object provides access to an encapsulated table of data.  The object  maintains a cursor pointing to its current row of data.  Initially the  cursor is positioned before the first row.  The &lt;/strong&gt;&lt;strong&gt;next&lt;/strong&gt;&lt;strong style="font-weight: 400;"&gt;  method moves the cursor to the next row &lt;i&gt;(similar to an iterator or an  enumerator in Java).&lt;/i&gt;&lt;/strong&gt;&lt;i&gt; &lt;/i&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Once the results are encapsulated in a &lt;b&gt;ResultSet&lt;/b&gt; object, the &lt;b&gt; ResultSet&lt;/b&gt; interface provides several methods that can be used to extract the  information from the object.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Methods of a ResultSet object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The &lt;b&gt;get&lt;/b&gt; methods &lt;i&gt;(such as &lt;b&gt;getString&lt;/b&gt;)&lt;/i&gt; retrieve column values for the  current row. You can retrieve values using either the index number of the column  or the name of the column. I have read that using the column index is more efficient  but I can't give you a reference on that.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Columns are numbered beginning with 1, not with 0.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;For the &lt;b&gt;get&lt;/b&gt; methods, the JDBC driver attempts to convert the underlying  data to the specified Java type and returns a suitable Java value.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The life of a ResultSet object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;A &lt;b&gt;ResultSet&lt;/b&gt; object is automatically closed by the &lt;b&gt;Statement&lt;/b&gt; object that generated it  when that &lt;b&gt;Statement&lt;/b&gt; object is closed, re-executed, or used to retrieve the next  result from a sequence of multiple results. &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Perform the query&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 26 invokes the &lt;b&gt;executeQuery&lt;/b&gt; method to perform the query on the database selecting all columns in all  rows, and sorting the results in order by the values in the column named &lt;b&gt; test_id&lt;/b&gt;.  The results are encapsulated in the &lt;b&gt;ResultSet&lt;/b&gt; object  referred to by the reference variable named &lt;b&gt;rs&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      rs = stmt.executeQuery("SELECT * " +&lt;br /&gt;               "from myTable ORDER BY test_id");&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 26&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Display all of the results in the ResultSet object&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 27 uses a &lt;b&gt;while&lt;/b&gt; loop to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Iterate on the &lt;b&gt;ResultSet&lt;/b&gt; object one row at a time.&lt;/li&gt;  &lt;li&gt;Invoke the &lt;b&gt;getInt&lt;/b&gt; method to get and save the value in the column   named &lt;b&gt;test_id&lt;/b&gt; for each row.&lt;/li&gt;  &lt;li&gt;Invoke the &lt;b&gt;getString&lt;/b&gt; method to get and save the value in the   column named &lt;b&gt;test_val&lt;/b&gt; for each row.&lt;/li&gt;  &lt;li&gt;Display the two values on a new output line on the screen.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      System.out.println("Display all results:");&lt;br /&gt;     while(rs.next()){&lt;br /&gt;       int theInt= rs.getInt("test_id");&lt;br /&gt;       String str = rs.getString("test_val");&lt;br /&gt;       System.out.println("\ttest_id= " + theInt&lt;br /&gt;                            + "\tstr = " + str);&lt;br /&gt;     }//end while loop&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 27&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The output produced by the code in Listing 27 is shown in Figure 7.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;Display all results:&lt;br /&gt;test_id= 1 str = One&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;test_id= 3 str = Three&lt;br /&gt;test_id= 4 str = Four&lt;br /&gt;test_id= 5 str = Five&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 7&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Display the data in row number 2&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The code in Listing 28 invokes the &lt;b&gt;absolute&lt;/b&gt; method on the same &lt;b&gt; ResultSet&lt;/b&gt; object to get, save, and display the data in the two columns of  row number 2. &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      System.out.println(&lt;br /&gt;                       "Display row number 2:");&lt;br /&gt;     if( rs.absolute(2) ){&lt;br /&gt;       int theInt= rs.getInt("test_id");&lt;br /&gt;       String str = rs.getString("test_val");&lt;br /&gt;       System.out.println("\ttest_id= " + theInt&lt;br /&gt;                            + "\tstr = " + str);&lt;br /&gt;     }//end if&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 28&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note that row numbers begin with 1 and do not begin with 0 as would   be the case in most Java contexts.  Although not demonstrated here, the   same is true for column numbers.  Apparently this is the norm in   database work.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 8 shows the output produced by the code in Listing 28.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;Display row number 2:&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 8&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Delete the table, close the connection, and  terminate&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Listing 29 invokes the &lt;b&gt;executeUpdate&lt;/b&gt; method to delete the table named &lt;b&gt;myTable&lt;/b&gt; from the database named &lt;b&gt;JunkDB&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;      stmt.executeUpdate("DROP TABLE myTable");&lt;br /&gt;     con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc10&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 29&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Then Listing 29 closes the connection &lt;i&gt;(logs off the database)&lt;/i&gt; and  terminates the program.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;center&gt; &lt;h2&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;a name="Run the program"&gt;&lt;/a&gt;Run the Programs&lt;/span&gt;&lt;/h2&gt; &lt;/center&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I encourage you to download and install the MySQL database server and the  MySQL connector as described in this lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Then copy the code from the listings near the end of this lesson.   Execute the batch files.  Compile and execute the programs.  Experiment with  the files and the programs, making changes, and observing the results of your changes. &lt;/span&gt;&lt;/p&gt; &lt;h2 align="center"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;a name="Summary"&gt;Summary&lt;/a&gt;&lt;/span&gt;&lt;/h2&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I showed you how to download, install, and prepare a MySQL database as a &lt;i&gt; localhost&lt;/i&gt; server on a Windows platform for use with JDBC.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Then I showed you how to write three simple JDBC programs to administer the  database server and to manipulate data stored in a MySQL database. &lt;/span&gt;&lt;/p&gt;&lt;center&gt; &lt;h2&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;a name="Complete Program Listings"&gt;&lt;/a&gt;Complete Program Listings&lt;/span&gt;&lt;/h2&gt; &lt;/center&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;A complete listing of each of the programs, batch files, and text files  discussed in this lesson is shown below.&lt;br /&gt;  &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;pause&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 30 Contents of MySqlStart.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;br /&gt;pause&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 31 Contents of MySqlStop.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;&lt;span style="font-family:Arial;"&gt;c:\mysql\bin\mysql --user=root mysql &lt; MySqlCreateDatabase01.txt&lt;br /&gt;pause&lt;/span&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 32 MySqlCreateDatabase01.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;CREATE DATABASE JunkDB;&lt;br /&gt;\q&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 33 MySqlCreateDatabase01.txt&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;&lt;span style="font-family:Arial;"&gt;c:\mysql\bin\mysql --user=root mysql &lt; MySqlMakeUser01.txt&lt;br /&gt;pause&lt;/span&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 34 MySqlMakeUser01.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP&lt;br /&gt;ON JunkDB.*&lt;br /&gt;TO 'auser'@'localhost'&lt;br /&gt;IDENTIFIED BY 'drowssap';&lt;br /&gt;\q&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 35 MySqlMakeUser01.txt&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;&lt;span style="font-family:Arial;"&gt;c:\mysql\bin\mysql --user=root mysql &lt; MySqlRemoveUser01.txt&lt;br /&gt;pause&lt;/span&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 36 MySqlRemoveUser01.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;&lt;span style="font-family:Arial Narrow;"&gt;REVOKE ALL PRIVILEGES ON *.* FROM 'auser'@'localhost';&lt;br /&gt;REVOKE GRANT OPTION ON *.* FROM 'auser'@'localhost';&lt;br /&gt;DELETE FROM mysql.user WHERE User='auser' and Host='localhost';&lt;br /&gt;FLUSH PRIVILEGES;&lt;br /&gt;\q&lt;/span&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 37 MySqlRemoveUser01.txt&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;&lt;span style="font-family:Arial;"&gt;c:\mysql\bin\mysql --user=root mysql &lt; MySqlDropDatabase01.txt&lt;br /&gt;pause&lt;/span&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 38 MySqlDropDatabase01.bat&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;DROP DATABASE JunkDB;&lt;br /&gt;\q&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 39 MySqlDropDatabase01.txt&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;/*File Jdbc11.java&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;Rev 09/18/04&lt;br /&gt;&lt;br /&gt;The purpose of this program is to log onto&lt;br /&gt;the master database named mysql as the default&lt;br /&gt;administrator named root whose password is blank&lt;br /&gt;in order to perform the following updates:&lt;br /&gt;&lt;br /&gt;1. Create a new database named JunkDB.&lt;br /&gt;2. Create a new user named auser with a password&lt;br /&gt;  of drowssap with broad access to the&lt;br /&gt;  database named JunkDB.&lt;br /&gt;&lt;br /&gt;These two operations produce no visible output&lt;br /&gt;when successful.  However, they produce error&lt;br /&gt;messages in the output when unsuccessful.&lt;br /&gt;&lt;br /&gt;This program is the reverse of the program named&lt;br /&gt;Jdbc12, which deletes the database JunkDB and&lt;br /&gt;removes the user named auser.&lt;br /&gt;&lt;br /&gt;The MySQL server must be running on localhost&lt;br /&gt;before this program is started.&lt;br /&gt;&lt;br /&gt;It is necessary to manually start the MySQL&lt;br /&gt;database server running on localhost. See the&lt;br /&gt;documentation sections 2.2.1.5,Starting the&lt;br /&gt;Server for the First Time and 2.2.1.4 Selecting a&lt;br /&gt;Windows Server. This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;&lt;br /&gt;Similarly, it is necessary to manually stop the&lt;br /&gt;MySQL database server.  See the documentation&lt;br /&gt;Section 2.2.1.6 Starting MySQL from the Windows&lt;br /&gt;Command Line.  This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;br /&gt;&lt;br /&gt;To install the JDBC interface classes, I  copied&lt;br /&gt;the jar file named&lt;br /&gt;mysql-connector-java-3.0.15-ga-bin.jar into the&lt;br /&gt;jre\lib\ext folder of my Java installation.  I&lt;br /&gt;did this to avoid having to make changes to the&lt;br /&gt;classpath.&lt;br /&gt;&lt;br /&gt;I am currently running SDK v1.4.2.  When I&lt;br /&gt;upgrade to a newer version of the SDK, it will be&lt;br /&gt;necessary for me to copy the JDBC jar file into&lt;br /&gt;the jre\lib\ext folder for the new version of the&lt;br /&gt;SDK.&lt;br /&gt;&lt;br /&gt;This program produces the following output as&lt;br /&gt;a result of a successful run:&lt;br /&gt;&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;URL: jdbc:mysql://localhost:3306/mysql&lt;br /&gt;Connection: com.mysql.jdbc.Connection@1430b5c&lt;br /&gt;&lt;br /&gt;Tested using SDK 1.4.2 under WinXP, MySQL&lt;br /&gt;version 4.0.21-win, and JDBC connector&lt;br /&gt;version mysql-connector-java-3.0.15-ga.&lt;br /&gt;************************************************/&lt;br /&gt;import java.sql.*;&lt;br /&gt;&lt;br /&gt;public class Jdbc11 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;&lt;br /&gt;     //Register the JDBC driver for MySQL.&lt;br /&gt;     Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;     //Define URL of database server for&lt;br /&gt;     // database named mysql on the localhost&lt;br /&gt;     // with the default port number 3306.&lt;br /&gt;     String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/mysql";&lt;br /&gt;&lt;br /&gt;     //Get a connection to the database for a&lt;br /&gt;     // user named root with a blank password.&lt;br /&gt;     // This user is the default administrator&lt;br /&gt;     // having full privileges to do anything.&lt;br /&gt;     Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                                url,"root", "");&lt;br /&gt;&lt;br /&gt;     //Display URL and connection information&lt;br /&gt;     System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;     //Get a Statement object&lt;br /&gt;     stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;     //Create the new database&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;                      "CREATE DATABASE JunkDB");&lt;br /&gt;     //Register a new user named auser on the&lt;br /&gt;     // database named JunkDB with a password&lt;br /&gt;     // drowssap enabling several different&lt;br /&gt;     // privileges.&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "GRANT SELECT,INSERT,UPDATE,DELETE," +&lt;br /&gt;         "CREATE,DROP " +&lt;br /&gt;         "ON JunkDB.* TO 'auser'@'localhost' " +&lt;br /&gt;         "IDENTIFIED BY 'drowssap';");&lt;br /&gt;     con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc11&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 40 Jdbc11.java&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;/*File Jdbc12.java&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;Rev 09/18/04&lt;br /&gt;&lt;br /&gt;The purpose of this program is to log onto&lt;br /&gt;the master database named mysql as the default&lt;br /&gt;administrator named root whose password is blank&lt;br /&gt;in order to perform the following updates:&lt;br /&gt;&lt;br /&gt;1. Remove a user named auser.&lt;br /&gt;2. Delete a database named JunkDB.&lt;br /&gt;&lt;br /&gt;These two operations produce no visible output&lt;br /&gt;when successful.  However, they produce error&lt;br /&gt;messages in the output when unsuccessful.&lt;br /&gt;&lt;br /&gt;This program is the reverse of the program named&lt;br /&gt;Jdbc11, which creates the database named JunkDB&lt;br /&gt;and registers the user named auser on that&lt;br /&gt;database.&lt;br /&gt;&lt;br /&gt;The MySQL server must be running on localhost&lt;br /&gt;before this program is started.&lt;br /&gt;&lt;br /&gt;It is necessary to manually start the MySQL&lt;br /&gt;database server running on localhost. See the&lt;br /&gt;documentation sections 2.2.1.5,Starting the&lt;br /&gt;Server for the First Time and 2.2.1.4 Selecting a&lt;br /&gt;Windows Server. This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;&lt;br /&gt;Similarly, it is necessary to manually stop the&lt;br /&gt;MySQL database server.  See the documentation&lt;br /&gt;Section 2.2.1.6 Starting MySQL from the Windows&lt;br /&gt;Command Line.  This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;br /&gt;&lt;br /&gt;To install the JDBC interface classes, I  copied&lt;br /&gt;the jar file named&lt;br /&gt;mysql-connector-java-3.0.15-ga-bin.jar into the&lt;br /&gt;jre\lib\ext folder of my Java installation.  I&lt;br /&gt;did this to avoid having to make changes to the&lt;br /&gt;classpath.&lt;br /&gt;&lt;br /&gt;I am currently running SDK v1.4.2.  When I&lt;br /&gt;upgrade to a newer version of the SDK, it will be&lt;br /&gt;necessary for me to copy the JDBC jar file into&lt;br /&gt;the jre\lib\ext folder for the new version of the&lt;br /&gt;SDK.&lt;br /&gt;&lt;br /&gt;This program produces the following output as&lt;br /&gt;a result of a successful run:&lt;br /&gt;&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;URL: jdbc:mysql://localhost:3306/mysql&lt;br /&gt;Connection: com.mysql.jdbc.Connection@1430b5c&lt;br /&gt;&lt;br /&gt;Tested using SDK 1.4.2 under WinXP, MySQL&lt;br /&gt;version 4.0.21-win, and JDBC connector&lt;br /&gt;version mysql-connector-java-3.0.15-ga.&lt;br /&gt;************************************************/&lt;br /&gt;import java.sql.*;&lt;br /&gt;&lt;br /&gt;public class Jdbc12 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;&lt;br /&gt;     //Register the JDBC driver for MySQL.&lt;br /&gt;     Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;     //Define URL of database server for&lt;br /&gt;     // database named mysql on the localhost&lt;br /&gt;     // with the default port number 3306.&lt;br /&gt;     String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/mysql";&lt;br /&gt;&lt;br /&gt;     //Get a connection to the database for a&lt;br /&gt;     // user named root with a blank password.&lt;br /&gt;     // This user is the default administrator&lt;br /&gt;     // having full privileges to do anything.&lt;br /&gt;     Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                                url,"root", "");&lt;br /&gt;&lt;br /&gt;     //Display URL and connection information&lt;br /&gt;     System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;     //Get a Statement object&lt;br /&gt;     stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;     //Remove the user named auser&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "REVOKE ALL PRIVILEGES ON *.* " +&lt;br /&gt;         "FROM 'auser'@'localhost'");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "REVOKE GRANT OPTION ON *.* " +&lt;br /&gt;         "FROM 'auser'@'localhost'");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;         "DELETE FROM mysql.user WHERE " +&lt;br /&gt;         "User='auser' and Host='localhost'");&lt;br /&gt;     stmt.executeUpdate("FLUSH PRIVILEGES");&lt;br /&gt;&lt;br /&gt;     //Delete the database&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;                      "DROP DATABASE JunkDB");&lt;br /&gt;&lt;br /&gt;     con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc12&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 41 Jdbc12.java&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ffff00" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;/*File Jdbc10.java&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;Rev 09/16/04&lt;br /&gt;&lt;br /&gt;The purpose of this program is to test the&lt;br /&gt;ability to use JDBC to access a MySQL database&lt;br /&gt;server on localhost.&lt;br /&gt;&lt;br /&gt;The MySQL server must be running on localhost&lt;br /&gt;before this program is started.  In addition, a&lt;br /&gt;database named JunkDB must have been created and&lt;br /&gt;a user named auser must have been registered on&lt;br /&gt;that database with a password of drowssap before&lt;br /&gt;this program is started.&lt;br /&gt;&lt;br /&gt;It is necessary to manually start the MySQL&lt;br /&gt;database server running on localhost. See the&lt;br /&gt;documentation sections 2.2.1.5,Starting the&lt;br /&gt;Server for the First Time and 2.2.1.4 Selecting a&lt;br /&gt;Windows Server. This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;&lt;br /&gt;Similarly, it is necessary to manually stop the&lt;br /&gt;MySQL database server.  See the documentation&lt;br /&gt;Section 2.2.1.6 Starting MySQL from the Windows&lt;br /&gt;Command Line.  This is accomplished by executing&lt;br /&gt;the following command at the command prompt:&lt;br /&gt;&lt;br /&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;br /&gt;&lt;br /&gt;You can prepare MySQL for use in three&lt;br /&gt;alternative ways:&lt;br /&gt;1. Using the control program named mysql located&lt;br /&gt;in folder C:\mysql\bin in interactive mode.&lt;br /&gt;2. Using a batch file to start the mysql program&lt;br /&gt;along with a text file to provide the input.&lt;br /&gt;3. By running the Java programs named Jdbc11 and&lt;br /&gt;Jdbc12.&lt;br /&gt;&lt;br /&gt;See the MySQL documentation Section 3.5, entitled&lt;br /&gt;Using mysql in Batch Mode, for an explanation of&lt;br /&gt;how to access the MySQL database server using&lt;br /&gt;batch files and text files.&lt;br /&gt;&lt;br /&gt;There is one bat file and one txt file used in&lt;br /&gt;each case. The bat file logs into the mysql&lt;br /&gt;monitor program as root having administrator&lt;br /&gt;privileges and points to the txt file, which&lt;br /&gt;provides the commands that are executed by the&lt;br /&gt;monitor program.&lt;br /&gt;&lt;br /&gt;The following two files create a new database&lt;br /&gt;named JunkDB. See the documentation Section 3.3,&lt;br /&gt;entitled Creating and Using a Database.&lt;br /&gt;&lt;br /&gt;MySqCreateDatabase01.bat&lt;br /&gt;MySqlCreateDatabase01.txt&lt;br /&gt;&lt;br /&gt;The following two files make a new user named&lt;br /&gt;auser registered on the database named JunkDB&lt;br /&gt;with a password of drowssap. This user can&lt;br /&gt;access the JunkDB database, but only from&lt;br /&gt;localhost. See the documentation Section 5.6.2,&lt;br /&gt;entitled Adding New User Accounts to MySQL.&lt;br /&gt;&lt;br /&gt;MySqlMakeUser01.bat&lt;br /&gt;MySqlMakeUser01.txt&lt;br /&gt;&lt;br /&gt;Another way to create the database and register&lt;br /&gt;a user named auser on that database is to run&lt;br /&gt;the Java program named Jdbc11.&lt;br /&gt;&lt;br /&gt;The following two files delete the database named&lt;br /&gt;JunkDB. See documentation Section 14.2.8, DROP&lt;br /&gt;DATABASE Syntax.&lt;br /&gt;&lt;br /&gt;MySqDropDatabase01.bat&lt;br /&gt;MySqlDropDatabase01.txt&lt;br /&gt;&lt;br /&gt;The following two files remove the user named&lt;br /&gt;auser. See the documentation Section 5.6.3,&lt;br /&gt;Removing User Accounts from MySQL.&lt;br /&gt;&lt;br /&gt;MySqlRemoveUser01.bat&lt;br /&gt;MySqlRemoveUser01.txt&lt;br /&gt;&lt;br /&gt;Another way to delete a database named JunkDB&lt;br /&gt;and remove the user named auser is to run the&lt;br /&gt;program named Jdbc12.&lt;br /&gt;&lt;br /&gt;This program:&lt;br /&gt;Accesses the database named JunkDB,&lt;br /&gt;Creates a table named myTable,&lt;br /&gt;Puts five rows of data into the table,&lt;br /&gt;Displays the data,&lt;br /&gt;Deletes the table.&lt;br /&gt;&lt;br /&gt;Two different approaches are used to display the&lt;br /&gt;contents of the table. The first approach&lt;br /&gt;displays all of the data in the table. The&lt;br /&gt;second approach displays only the data in a&lt;br /&gt;specific row in the table.&lt;br /&gt;&lt;br /&gt;As a precaution, before attempting to create the&lt;br /&gt;new table, the program attempts to delete a table&lt;br /&gt;having the same name. If a table having the same&lt;br /&gt;name already exists as residue from a previous&lt;br /&gt;run, it is deleted. If it doesn't already exist&lt;br /&gt;when the attempt is made to delete it, an&lt;br /&gt;exception is thrown. This exception is simply&lt;br /&gt;caught and ignored.&lt;br /&gt;&lt;br /&gt;To install the JDBC interface classes, I copied&lt;br /&gt;the jar file named&lt;br /&gt;mysql-connector-java-3.0.15-ga-bin.jar into the&lt;br /&gt;jre\lib\ext folder of my Java installation. I&lt;br /&gt;did this to avoid having to make changes to the&lt;br /&gt;classpath.&lt;br /&gt;&lt;br /&gt;I am currently running SDK v1.4.2. When I&lt;br /&gt;upgrade to a newer version of the SDK, it will be&lt;br /&gt;necessary for me to copy the JDBC jar file into&lt;br /&gt;the jre\lib\ext folder for the new version of the&lt;br /&gt;SDK.&lt;br /&gt;&lt;br /&gt;This program produces the following output under&lt;br /&gt;normal conditions where the table named myTable&lt;br /&gt;does not exist when the program is started (the&lt;br /&gt;specifics regarding the Connection object may&lt;br /&gt;vary from one run to the next):&lt;br /&gt;&lt;br /&gt;Copyright 2004, R.G.Baldwin&lt;br /&gt;URL: jdbc:mysql://localhost:3306/JunkDB&lt;br /&gt;Connection: com.mysql.jdbc.Connection@1430b5c&lt;br /&gt;java.sql.SQLException: Base table or view not&lt;br /&gt;found message from server: "Unknown table&lt;br /&gt;'mytable'"No existing table to delete&lt;br /&gt;Display all results:&lt;br /&gt;test_id= 1 str = One&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;test_id= 3 str = Three&lt;br /&gt;test_id= 4 str = Four&lt;br /&gt;test_id= 5 str = Five&lt;br /&gt;Display row number 2:&lt;br /&gt;test_id= 2 str = Two&lt;br /&gt;&lt;br /&gt;Tested using SDK 1.4.2 under WinXP, MySQL&lt;br /&gt;version 4.0.21-win, and JDBC connector&lt;br /&gt;version mysql-connector-java-3.0.15-ga.&lt;br /&gt;************************************************/&lt;br /&gt;import java.sql.*;&lt;br /&gt;&lt;br /&gt;public class Jdbc10 {&lt;br /&gt; public static void main(String args[]){&lt;br /&gt;   System.out.println(&lt;br /&gt;                 "Copyright 2004, R.G.Baldwin");&lt;br /&gt;   try {&lt;br /&gt;     Statement stmt;&lt;br /&gt;     ResultSet rs;&lt;br /&gt;&lt;br /&gt;     //Register the JDBC driver for MySQL.&lt;br /&gt;     Class.forName("com.mysql.jdbc.Driver");&lt;br /&gt;&lt;br /&gt;     //Define URL of database server for&lt;br /&gt;     // database named JunkDB on the localhost&lt;br /&gt;     // with the default port number 3306.&lt;br /&gt;     String url =&lt;br /&gt;           "jdbc:mysql://localhost:3306/JunkDB";&lt;br /&gt;&lt;br /&gt;     //Get a connection to the database for a&lt;br /&gt;     // user named auser with the password&lt;br /&gt;     // drowssap, which is password spelled&lt;br /&gt;     // backwards.&lt;br /&gt;     Connection con =&lt;br /&gt;                    DriverManager.getConnection(&lt;br /&gt;                       url,"auser", "drowssap");&lt;br /&gt;&lt;br /&gt;     //Display URL and connection information&lt;br /&gt;     System.out.println("URL: " + url);&lt;br /&gt;     System.out.println("Connection: " + con);&lt;br /&gt;&lt;br /&gt;     //Get a Statement object&lt;br /&gt;     stmt = con.createStatement();&lt;br /&gt;&lt;br /&gt;     //As a precaution, delete myTable if it&lt;br /&gt;     // already exists as residue from a&lt;br /&gt;     // previous run.  Otherwise, if the table&lt;br /&gt;     // already exists and an attempt is made&lt;br /&gt;     // to create it, an exception will be&lt;br /&gt;     // thrown.&lt;br /&gt;     try{&lt;br /&gt;       stmt.executeUpdate("DROP TABLE myTable");&lt;br /&gt;     }catch(Exception e){&lt;br /&gt;       System.out.print(e);&lt;br /&gt;       System.out.println(&lt;br /&gt;                 "No existing table to delete");&lt;br /&gt;     }//end catch&lt;br /&gt;&lt;br /&gt;     //Create a table in the database named&lt;br /&gt;     // myTable.&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;           "CREATE TABLE myTable(test_id int," +&lt;br /&gt;                 "test_val char(15) not null)");&lt;br /&gt;&lt;br /&gt;     //Insert some values into the table&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                   "test_val) VALUES(1,'One')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                   "test_val) VALUES(2,'Two')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                 "test_val) VALUES(3,'Three')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                  "test_val) VALUES(4,'Four')");&lt;br /&gt;     stmt.executeUpdate(&lt;br /&gt;               "INSERT INTO myTable(test_id, " +&lt;br /&gt;                  "test_val) VALUES(5,'Five')");&lt;br /&gt;&lt;br /&gt;     //Get another statement object initialized&lt;br /&gt;     // as shown.&lt;br /&gt;     stmt = con.createStatement(&lt;br /&gt;              ResultSet.TYPE_SCROLL_INSENSITIVE,&lt;br /&gt;                    ResultSet.CONCUR_READ_ONLY);&lt;br /&gt;&lt;br /&gt;     //Query the database, storing the result&lt;br /&gt;     // in an object of type ResultSet&lt;br /&gt;     rs = stmt.executeQuery("SELECT * " +&lt;br /&gt;               "from myTable ORDER BY test_id");&lt;br /&gt;&lt;br /&gt;     //Use the methods of class ResultSet in a&lt;br /&gt;     // loop to display all of the data in the&lt;br /&gt;     // database.&lt;br /&gt;     System.out.println("Display all results:");&lt;br /&gt;     while(rs.next()){&lt;br /&gt;       int theInt= rs.getInt("test_id");&lt;br /&gt;       String str = rs.getString("test_val");&lt;br /&gt;       System.out.println("\ttest_id= " + theInt&lt;br /&gt;                            + "\tstr = " + str);&lt;br /&gt;     }//end while loop&lt;br /&gt;&lt;br /&gt;     //Display the data in a specific row using&lt;br /&gt;     // the rs.absolute method.&lt;br /&gt;     System.out.println(&lt;br /&gt;                       "Display row number 2:");&lt;br /&gt;     if( rs.absolute(2) ){&lt;br /&gt;       int theInt= rs.getInt("test_id");&lt;br /&gt;       String str = rs.getString("test_val");&lt;br /&gt;       System.out.println("\ttest_id= " + theInt&lt;br /&gt;                            + "\tstr = " + str);&lt;br /&gt;     }//end if&lt;br /&gt;&lt;br /&gt;     //Delete the table and close the connection&lt;br /&gt;     // to the database&lt;br /&gt;     stmt.executeUpdate("DROP TABLE myTable");&lt;br /&gt;     con.close();&lt;br /&gt;   }catch( Exception e ) {&lt;br /&gt;     e.printStackTrace();&lt;br /&gt;   }//end catch&lt;br /&gt; }//end main&lt;br /&gt;}//end class Jdbc10&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;Listing 42 Jdbc10.java&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-958029471819644560?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/958029471819644560/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=958029471819644560' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/958029471819644560'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/958029471819644560'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/using-jdbc-with-mysql-3.html' title='Using JDBC with MySQL 3'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-3978973108878054389</id><published>2007-04-15T21:54:00.000-07:00</published><updated>2007-04-15T22:33:55.756-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='developer.com'/><title type='text'>Using JDBC with MySQL 2</title><content type='html'>&lt;h3&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Creating a New Database using the Monitor Program&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Now that you know how to start the MySQL database server, it's time to learn  how to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Create a database that can be manipulated using JDBC in a Java program.&lt;/li&gt;  &lt;li&gt;Create a new user having the necessary privileges to manipulate the   database using JDBC in a Java program.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Three different approaches&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I'm going to show you three different ways to accomplish this:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Using a command-line program named &lt;b&gt;mysql&lt;/b&gt; coupled with manual data   entry at runtime.  &lt;i&gt;(I will refer to this as the &lt;b&gt;monitor&lt;/b&gt;   program for reasons that will become self-evident later.)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;Using the monitor program coupled with data input derived from a text   file.&lt;/li&gt;  &lt;li&gt;Using JDBC in a Java program.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will illustrate the first approach in this and the later section entitled &lt;a href="http://www.developer.com/java/data/article.php/3417381#Creating_a_New_User"&gt;Creating a New User using the Monitor Program&lt;/a&gt;  .&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will illustrate the second approach in the section entitled &lt;a href="http://www.developer.com/java/data/article.php/3417381#Creating_a_New_Database_using_Batch_Files"&gt;Administering the Database  Server using Text Files&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I will illustrate the third approach in the section entitled &lt;a href="http://www.developer.com/java/data/article.php/3417381#Discussion%20and%20Sample%20Programs"&gt;Discussion and Sample Code&lt;/a&gt;, which  shows how to use JDBC to manage and manipulate the database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The monitor program and manual data entry&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The monitor program is named &lt;b&gt;mysql.exe&lt;/b&gt;.  It is located in &lt;b&gt; c:\mysql\bin&lt;/b&gt;.  This program makes it possible to log onto the database  server and to enter commands at the command line to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Create databases&lt;/li&gt;  &lt;li&gt;Add new users&lt;/li&gt;  &lt;li&gt;Modify databases&lt;/li&gt;  &lt;li&gt;Perform ad-hoc queries, etc.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Unless you really enjoy typing, the monitor program is not a lot of   fun to use.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;To really learn MySQL ...&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In order to really learn how to use MySQL, you will need to study the MySQL  database server documentation in detail and probably some good SQL books as  well.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The purpose of this lesson is to teach you just enough to get you started.   When you finish this lesson, you should be able to successfully write and execute simple JDBC programs that will manipulate  database tables on the MySQL database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The default administrative user&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As I understand it, when the MySQL database server is first installed, there  is a default user named &lt;b&gt;root&lt;/b&gt; with full administrative privileges and no  password.  At this point, the server is totally wide open and insecure.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(The server documentation provides various suggestions as to what you should   do to add security to the server.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The user named &lt;b&gt;root&lt;/b&gt; has the ability to create new databases as well as  to create new users and to register those users on the databases.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Existing databases at MySQL installation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Also, as I understand it, there are two existing databases on the server when  it is first installed.  There is a database named &lt;b&gt;test&lt;/b&gt;, which  is wide open with no password requirements.  Any user can access this  database.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There is also a database named &lt;b&gt;mysql&lt;/b&gt;, which is apparently used to keep  track of things such as databases, users, etc.  I believe that this  database is accessible only by users having administrative privileges.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Adding a new database using the monitor program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The first step in adding a new database using the monitor program is to log  onto the database server as the administrative user named &lt;b&gt;root&lt;/b&gt; with  access to the database named &lt;b&gt;mysql&lt;/b&gt;.  Until a password is assigned to  the &lt;b&gt;root&lt;/b&gt; user, login can be accomplished by entering the following command at the  command prompt:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;c:\mysql\bin\mysql --user=root mysql&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(In case it isn't clear on your display, the word user is preceded by   two minus sign characters.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The screen output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Assuming that the MySQL database server is running, the screen output  produced by entering this command is shown in Figure 3.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note that in Figure 3, and several of the figures that follow, it was   necessary for me to manually enter line breaks in the screen output to force   the material to fit in this narrow publication format.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\jnk&gt;c:\mysql\bin\mysql --user=root mysql&lt;br /&gt;Welcome to the MySQL monitor. Commands end with ;&lt;br /&gt;or \g.&lt;br /&gt;Your MySQL connection id is 26 to server version:&lt;br /&gt;4.0.21&lt;br /&gt;&lt;br /&gt;Type 'help;' or '\h' for help. Type '\c' to clear&lt;br /&gt;the buffer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;mysql&gt;&lt;/b&gt;&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 3 Monitor output for administrator login.&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The monitor program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Note that this program refers to itself as the &lt;b&gt;MySQL monitor&lt;/b&gt;.   That explains why I refer to it as the monitor or the monitor program.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The purpose of this program is to make it possible for you to enter SQL  database commands from the keyboard.  Note in particular the prompt shown  in boldface at the end of Figure 3, which reads:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;mysql&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This is not a command-line prompt, which typically looks something like:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;C:\jnk&lt;/b&gt;&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Rather, this is a program-generated prompt where the monitor program is  requesting input from the user.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;SQL command terminators&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As indicated in Figure 3, SQL commands end with either a semicolon character  or \g &lt;i&gt;(note the difference in typeface for the character g in this text  relative to that shown in Figure 3).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;SQL commands are often quite long.  You can enter successive portions of  SQL commands at successive program prompts.  &lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Later, we will see that the monitor program uses a different syntax   for continuation prompts.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;It is not until you enter a semicolon character or a \g that the program  responds to and attempts to execute the entire SQL command.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Terminating the monitor program&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;You can terminate the monitor program by entering a \q at the program prompt.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note that this is a q as in quit and is not a g as in good.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Creating a new database named JunkDB&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 4 shows the screen output for the use of the monitor program by the  user named &lt;b&gt;root&lt;/b&gt; to create a new database named &lt;b&gt;JunkDB&lt;/b&gt; and then to  terminate the monitor program.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\jnk&gt;c:\mysql\bin\mysql --user=root mysql&lt;br /&gt;Welcome to the MySQL monitor. Commands end with ;&lt;br /&gt;or \g.&lt;br /&gt;Your MySQL connection id is 27 to server version:&lt;br /&gt;4.0.21&lt;br /&gt;&lt;br /&gt;Type 'help;' or '\h' for help. Type '\c' to clear&lt;br /&gt;the buffer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;mysql&gt; CREATE DATABASE JunkDB;&lt;br /&gt;Query OK, 1 row affected (0.13 sec)&lt;br /&gt;&lt;br /&gt;mysql&gt; \q&lt;br /&gt;Bye&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;C:\jnk&gt;&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 4 Creating database named JunkDB&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The new material in Figure 4 is shown in boldface in the bottom half of the  figure.  The material in the top half of Figure 4 is a repeat of the  material shown in Figure 3.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Using a batch file and a text file&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Shortly, I will show you how to create a new database using a Windows batch  file and an associated text file.  Later on, I will show you how to create a new database using a Java  JDBC program.&lt;/span&gt;&lt;/p&gt; &lt;h3&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;a name="Creating_a_New_User"&gt;Creating a New User&lt;/a&gt; with the Monitor  Program&lt;/span&gt;&lt;/h3&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 5 shows the screen output for the use of the monitor program by the  user named &lt;b&gt;root&lt;/b&gt; to add a new user named &lt;b&gt;auser&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The new material is shown in boldface in the lower half of the figure.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\jnk&gt;c:\mysql\bin\mysql --user=root mysql&lt;br /&gt;Welcome to the MySQL monitor. Commands end with ;&lt;br /&gt;or \g.&lt;br /&gt;Your MySQL connection id is 30 to server version:&lt;br /&gt;4.0.21&lt;br /&gt;&lt;br /&gt;Type 'help;' or '\h' for help. Type '\c' to clear&lt;br /&gt;the buffer.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;mysql&gt; GRANT SELECT,INSERT,UPDATE,&lt;br /&gt;-&gt; DELETE,CREATE,DROP&lt;br /&gt;-&gt; ON JunkDB.*&lt;br /&gt;-&gt; TO 'auser'@'localhost'&lt;br /&gt;-&gt; IDENTIFIED BY 'drowssap';&lt;br /&gt;Query OK, 0 rows affected (0.01 sec)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;mysql&gt; \q&lt;br /&gt;Bye&lt;br /&gt;&lt;br /&gt;C:\jnk&gt;&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 5 Adding new user named auser.&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;A longer SQL command&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This SQL command is much longer than the command used to create the new  database.  This command requires several continuation lines to complete to  prevent it from exceeding the screen width.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note the difference in the syntax of new program prompts and   continuation program prompts.  The prompts that look like an arrow are   the continuation prompts.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I won't try to explain the SQL command is detail.  I will simply refer  you to the MySQL database documentation and a good SQL book for that purpose.   However, the SQL command is relatively self explanatory.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The meaning of the SQL command&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This SQL command grants a list of six different privileges on the database  named &lt;b&gt;JunkDB&lt;/b&gt; to a user named &lt;b&gt;auser&lt;/b&gt; who will be accessing the  database from &lt;b&gt;localhost&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Granting access to the same user from a different machine on the   network would require a different syntax.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The user named &lt;b&gt;auser&lt;/b&gt; will be allowed to access the database named &lt;b&gt; JunkDB&lt;/b&gt; using the password &lt;b&gt;drowssap&lt;/b&gt;, &lt;i&gt;(which is password spelled  backwards to make it easy for me to remember).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;a name="Creating_a_New_Database_using_Batch_Files"&gt;Administering the  Database Server using Text Files&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;h3&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;a name="Creating_a_New_Database_using_Batch_Files"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As mentioned earlier, unless you really enjoy typing, using the monitor  program in manual data entry mode is not a fun way to work with the database.   For example, if you make a typing error, you must go back and retype the entire  command from the beginning.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Fortunately, there is a better approach.  That approach is to provide  the commands to the monitor program using a text file as input.  Then if  you make an error, you can simply edit the text file and rerun the process.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;How does it work?&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;To make a long story short, you start the monitor program by redirecting the  input so that the input is derived from a text file instead of from the  keyboard.  This process is described in the database server documentation,  Section 3.5 entitled &lt;i&gt;Using mysql in Batch Mode.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Creating a new database&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There are probably several ways to set this process up.  I elected to  use a combination of a batch file and a text file.  The batch file starts  the monitor program, logging in as &lt;b&gt;root&lt;/b&gt;, and redirects input to the  associated text file.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;For example, the files used to create a new database named &lt;b&gt;JunkDB&lt;/b&gt; are  shown in Listing 32 and Listing 33 near the end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Making a new user&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The files used to make a new user named &lt;b&gt;auser&lt;/b&gt; are shown in Listing 34  and Listing 35.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Compare the contents of these two files with the manual data entry shown  earlier in Figure 5.  The new user is granted six different privileges on  the database named &lt;b&gt;JunkDB&lt;/b&gt; from &lt;b&gt;localhost&lt;/b&gt; with a password of &lt;b&gt; drowssap&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;&lt;a name="Removing_the_user_named_auser"&gt;Removing the user named auser&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The files used to remove the user named &lt;b&gt;auser&lt;/b&gt; are shown in Listing 36  and Listing 37.  The procedure for revoking a user's privileges and  removing the user is explained near the end of Section 14.5.1.1 entitled &lt;i&gt;DROP  USER Syntax&lt;/i&gt; in the database server documentation.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;&lt;a name="Deleting_the_database_named_JunkDB"&gt;Deleting the database named JunkDB&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The files used to delete the database named &lt;b&gt;JunkDB&lt;/b&gt; are shown in  Listing 38 and Listing 39.  The procedure for deleting a database is  explained in the database server documentation, Section 14.2.8 entitled &lt;i&gt;DROP  DATABASE Syntax.&lt;/i&gt; &lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-3978973108878054389?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/3978973108878054389/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=3978973108878054389' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3978973108878054389'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3978973108878054389'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/using-jdbc-with-mysql-2.html' title='Using JDBC with MySQL 2'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-2718437797363594456</id><published>2007-04-15T21:18:00.000-07:00</published><updated>2007-04-15T21:53:55.446-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='developer.com'/><title type='text'>Using JDBC with MySQL 1</title><content type='html'>&lt;h2 style="font-weight: normal;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;Preface&lt;/span&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Purpose&lt;/b&gt;&lt;/span&gt;&lt;/span&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; The purpose of this lesson is to get you beyond the initial hurdles involved  in:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Downloading and installing a MySQL database server.&lt;/li&gt;  &lt;li&gt;Preparing that database for use with JDBC.&lt;/li&gt;  &lt;li&gt;Writing and testing your first JDBC programs to administer the database   and to manipulate the data stored in   the MySQL database.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;b&gt;What is JDBC?&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; JDBC technology is an API &lt;i&gt;(included in both J2SE and J2EE)&lt;/i&gt;  that provides cross-DBMS connectivity to a wide range of SQL databases and  access to other tabular data sources, such as spreadsheets or flat files.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;b&gt;What is MySQL?&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; The &lt;a href="http://www.mysql.com/"&gt;MySQL&lt;/a&gt; database server is probably  the world's most popular open source database software, with more than five  million active installations as of September 2004.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; The database server software from &lt;a href="http://dev.mysql.com/downloads/index.html"&gt;MySQL&lt;/a&gt; is available under  a &lt;i&gt;"dual licensing"&lt;/i&gt; model. Under this model, users may choose to use MySQL  products under the &lt;a href="http://www.mysql.com/company/legal/licensing/opensource-license.html"&gt; free software/open source GNU General Public License&lt;/a&gt; &lt;i&gt;(commonly known as  the "GPL")&lt;/i&gt; or under a &lt;a href="http://www.mysql.com/company/legal/licensing/commercial-license.html"&gt; commercial license&lt;/a&gt;. &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;span style="color:#ff0000;"&gt;&lt;b&gt;A powerful combination&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; Simply stated, JDBC makes it possible to write platform independent Java  programs that can be used to manipulate the data in a wide range of SQL  databases without the requirement to modify and/or recompile the Java programs when moving from  platform to platform or from DBMS to DBMS.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; MySQL is available for a wide variety of platforms.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; Since both JDBC and MySQL are freely available for many purposes, the  combination of JDBC and MySQL is a powerful combination that should be of  interest for a wide variety of applications.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Preview&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Download, install, and prepare the database server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;It is often possible to use the same Java program to manipulate the data in a  wide variety of SQL databases without a requirement to modify and/or recompile the Java program.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;However, the installation and preparation procedures for different SQL databases  vary widely.  A large part of the battle in using JDBC with a particular  database is getting the database installed and properly prepared for use with  JDBC.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;In this lesson, I will show you how to download, install, and prepare a MySQL  database as a &lt;i&gt;localhost&lt;/i&gt; server on a Windows platform for use with JDBC.   Then I will show you how to write three simple JDBC programs to administer the  database server and to manipulate data stored on the database server after it is installed.&lt;/span&gt;&lt;/p&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;&lt;span style="color:#000000;"&gt;&lt;a name="Installation_and_Preparation_of_MySQL"&gt;Installation and Preparation of  MySQL&lt;br /&gt;&lt;br /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Getting things up and running&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As a minimum, getting up and running with MySQL and JDBC involves at least  the following steps:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Download and install the appropriate release of the MySQL database   server software &lt;i&gt;  (several different releases are available).&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;Download and install the MySQL Connector/J -- for connecting to a MySQL   database server from Java.&lt;/li&gt;  &lt;li&gt;Download and install the documentation for the MySQL database server.&lt;/li&gt;  &lt;li&gt;Download and install the documentation for the Connector,   which is a separate documentation package from the database server   documentation.&lt;/li&gt;  &lt;li&gt;Write and test one or more JDBC programs that will act as a database administrator, creating one or more users and   possibly one or more   databases on the database server.  &lt;i&gt;(I will show you three different   ways to accomplish this.)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;Write and test a JDBC program that will log in as a user and   manipulate data stored in one of those databases.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Additional MySQL software&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Beyond the minimum, there are a variety of additional software packages, &lt;i&gt; (such as GUI administrator packages)&lt;/i&gt; that can be downloaded from MySQL and  installed on your computer.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Since the main thrust of this lesson  has to do with JDBC rather than database administration, I won't get into that.   Rather, I will show you how to use a command-line monitor program that is  included with the MySQL database software to perform the minimal database administrative  tasks required to satisfy the objectives of this lesson.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;Documentation&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;&lt;span style="color:#ff0000;"&gt;MySQL database server documentation&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The MySQL Reference Manual can be downloaded from &lt;a href="http://dev.mysql.com/doc/"&gt;http://dev.mysql.com/doc/&lt;/a&gt;.  In  addition, there is an online searchable version of the Reference Manual available at &lt;a href="http://dev.mysql.com/doc/mysql/en/Reference.html"&gt; http://dev.mysql.com/doc/mysql/en/Reference.html&lt;/a&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The downloadable version is available in several different formats, including:&lt;/span&gt;&lt;/p&gt;  &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;     &lt;/span&gt;&lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;      &lt;li&gt;       &lt;a href="http://dev.mysql.com/get/Downloads/Manual/manual-split.zip/from/pick"&gt;       HTML, one page per chapter, ZIP&lt;/a&gt;        &lt;/li&gt;       &lt;li&gt;       &lt;a href="http://dev.mysql.com/get/Downloads/Manual/manual.zip/from/pick"&gt;       HTML, all on one page, ZIP&lt;/a&gt; &lt;/li&gt;      &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;You would probably do well to have both of these formats locally available on  your computer if you have sufficient disk space.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The one-page-per-chapter formatted manual&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The first format in the above list consists of a large number of HTML files.   There is one HTML file for the table of contents plus about thirty-three additional files  containing the text of the reference manual.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This format has a major advantage over the second format in terms of speed.   It is relatively fast to click on a hyperlink in the table of contents and to  see that material appear in the browser window.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;There are a couple of downsides to this format, however.  One downside  is that this format is not very useful for searching the entire manual for  keywords, &lt;i&gt;(using your browser)&lt;/i&gt; because it is broken down into a large  number of separate HTML files.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;A second downside is that even though MySQL 4.0 is the recommended release in  September of 2004, this manual contains information up through version  5.0.1-alpha.  Sometimes information about the newer versions tends to  obscure information about version 4.0.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The all-on-one-page formatted manual&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This format is very useful for searching &lt;i&gt;(using your browser)&lt;/i&gt; because  all of the text is in a single HTML file.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(There are actually two HTML  files.  One file contains a hyperlinked table of contents.  The second  file contains the text of the entire manual.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The primary downside to this format is speed, or lack thereof.  The HTML  file containing the text of the manual is about four megabytes in size.  On  my machine, navigating this manual in a browser is a very slow process.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The downloadable version in this format also contains information up through  version 5.0.1-alpha, resulting in the same disadvantage mentioned earlier.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Included in the software distribution&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;When you download and install the currently recommended version of  MySQL, &lt;i&gt;(which is version 4.0.21),&lt;/i&gt; the Docs folder in the installation  tree structure will contain a copy of the manual in the all-on-one-page format that purports to  be for version 4.0.21.  Thus, you don't need to download this format  separately.  You will get it when you download the software.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Even though this version purports to be for version 4.0.21, it also   contains a lot of information about later versions.  It may be exactly   the same as the version that can be downloaded separately except that the   title page is different.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Installation of the all-on-one-page formatted manual&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As mentioned above, you don't need to do anything special to install this  format of the manual.  It will be installed automatically when you install  the MySQL 4.0.21 version of the database.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Presumably, later versions of the software will also contain a copy   of the current manual in this format.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Once you have installed the database, the Docs folder of the installation  tree will contain the files named &lt;b&gt;manual_toc.html&lt;/b&gt; and &lt;b&gt;manual.html&lt;/b&gt;.   The first file contains a hyperlinked table of contents, and the second file  contains the entire text of the manual. &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Using both formats&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Because I have plenty of space on my disk, I have both formats installed on  my computer with an icon on the desktop for each.  I occasionally open the  version that contains the entire manual in a single HTML file when I need to  search the entire document for something.  Most of the time, however, I  open and use the multi-file version due to its increased speed.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;MySQL Connector/J documentation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I did not find a separate downloadable version of the connector documentation at the MySQL site.   However, I did find an online version at &lt;a href="http://dev.mysql.com/doc/connector/j/en/index.html"&gt; http://dev.mysql.com/doc/connector/j/en/index.html&lt;/a&gt;.  I was able to  save the connector documentation locally by selecting the&lt;i&gt; Save Page As... &lt;/i&gt;item on  the &lt;i&gt;File&lt;/i&gt; menu of my Netscape 7.2 browser.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(I was unable to save the page locally using Internet Explorer version   6 for some reason.  However, I also discovered later that essentially the same   documentation is contained in the downloadable zip file for the connector   software in a file named mysql-connector-java-3.0.15-ga\docs\index.html.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Saving the page in Netscape 7.2 resulted in a local file named &lt;b&gt;index.html&lt;/b&gt;  and an associated folder named &lt;b&gt;index_files&lt;/b&gt;.  The file contains the  text of the connector documentation.  The folder contains style sheets and other  related material.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Installation of the connector documentation consisted simply of saving this  material locally and creating a desktop icon linked to the file named &lt;b&gt; index.html&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;Downloading the MySQL Database Server&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The download page&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The main download page for both the database server and the connector as of  September 2004 is &lt;a href="http://dev.mysql.com/downloads/"&gt; http://dev.mysql.com/downloads/&lt;/a&gt;.  Hopefully, this URL will also remain  intact as MySQL releases later versions of the software.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Several different versions of the  database server are available for downloading as of September 2004, including:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/mysql/4.0.html"&gt;MySQL 4.0&lt;/a&gt; --   Generally Available &lt;i&gt;(GA)&lt;/i&gt; release (recommended)&lt;/li&gt;  &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/mysql/4.1.html"&gt;MySQL 4.1&lt;/a&gt; --   Gamma release &lt;i&gt;(use this for new development)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/mysql/5.0.html"&gt;MySQL 5.0&lt;/a&gt; --   Alpha release &lt;i&gt;(use this for previewing and testing new features)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/mysql/5.0.1-snapshot.html"&gt;MySQL   5.0.1&lt;/a&gt; -- Snapshot release &lt;i&gt;(use this for previewing and testing new   features)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;&lt;a href="http://downloads.mysql.com/archives.php"&gt;Older releases&lt;/a&gt; --   older releases &lt;i&gt;(only recommended for special needs)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;&lt;a href="http://downloads.mysql.com/snapshots.php"&gt;Snapshots&lt;/a&gt; --   source code snapshots of the development trees&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This list can be expected to change over time as new versions of  the database server are released.  Thus, the links in the above list will become obsolete.   When that happens, you should revert back to the download page at &lt;a href="http://dev.mysql.com/downloads/"&gt;http://dev.mysql.com/downloads/&lt;/a&gt;  and download the version that best suits your needs at that time.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The different versions of the database server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As of September 2004, the database server documentation has this to say about these different  versions:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;MySQL 5.0 is the newest development release series and is under very   active development for new features. Alpha releases have been issued to   allow more widespread testing. &lt;/li&gt;  &lt;li&gt;MySQL 4.1 is in gamma status, soon moving to production status. &lt;/li&gt;  &lt;li&gt;MySQL 4.0 is the current stable &lt;i&gt;(production-quality)&lt;/i&gt; release   series. New releases are issued for bugfixes. No new features are added that   could diminish the code stability. &lt;/li&gt;  &lt;li&gt;MySQL 3.23 is the old stable &lt;i&gt;(production-quality)&lt;/i&gt; release series.   This series is retired, so new releases are issued only to fix critical   bugs.   &lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;I elected MySQL 4.0&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I elected to download MySQL 4.0 since it is the stable production quality  version as of September 2004.  This resulted in the downloading of a  distribution file named &lt;b&gt;mysql-4.0.21-win.zip&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;Installing MySQL Database Server&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Installation instructions&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Installation instructions for the database server are provided in the  database server documentation, Section 2, entitled &lt;i&gt;Installing MySQL.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Since I was installing on Windows XP and had no desire to deal with source  code, I quickly skipped down to Section 2.2.1.2 entitled &lt;i&gt;Installing a Windows  Binary Distribution.&lt;/i&gt;   &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;In my case, installation was easy&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Because I did not have an earlier version of MySQL installed and I was logged  onto Windows as an administrator, all that I needed to do was to execute the  following instructions from the database server documentation to install the MySQL  database server on my computer.&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Unzip the distribution file to a temporary directory. &lt;/li&gt;  &lt;li&gt;Run the &lt;b&gt;setup.exe&lt;/b&gt; program to begin the installation process. &lt;i&gt;  If you want to install MySQL into a location other than the default   directory (`C:\mysql'), use the Browse button to specify your preferred   directory. If you do not install MySQL into the default location, you will   need to specify the location whenever you start the server. The easiest way   to do this is to use an option file, as described in Section 2.2.1.3   Preparing the Windows MySQL Environment. &lt;/i&gt;&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Because I didn't want to deal with option files, I elected to allow the  software to be installed in the default directory, C:\mysql.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Testing the installation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;After completing the installation, I performed some of the procedures shown in the  database server documentation, Section 2.4.1 entitled &lt;i&gt;Windows Post-Installation Procedures.&lt;/i&gt;   Although I didn't get exactly the same results as those shown in the documentation, my  results were close enough to convince me that the MySQL database server was  correctly installed on my computer.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Not installed as a Windows service&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Section 2.2.1.7 of the database server documentation entitled &lt;i&gt;Starting MySQL as a Windows Service&lt;/i&gt; contains  the following:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"On the NT family (Windows NT, 2000, or XP), the recommended way to   run MySQL is to install it as a Windows service. Then Windows starts and   stops the MySQL server automatically when Windows starts and stops."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I have no desire for the MySQL database server to start running every time I  start Windows running.  I already waste enough time waiting for Windows XP  to become ready for use on my laptop each time I start it.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Therefore, I did not install the database server as a service.  I will  explain how I manually &lt;i&gt;start&lt;/i&gt; and &lt;i&gt;stop&lt;/i&gt; the database server whenever I need to use  it later in this lesson.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;h3&gt;&lt;span style="font-size:100%;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;Downloading MySQL Connector/J&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;&lt;span style="color:#ff0000;"&gt;What is MySQL Connector/J?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;For those who don't know, let me begin by explaining the purpose of MySQL  Connector/J.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The JDBC API is designed to make it possible for you to write a single Java  program and to use it to manipulate the data in a variety of different SQL database servers  without a requirement to modify and/or recompile the program.   In order to do this, it is necessary for you to:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;Inform the Java program as to the URL of the database server.  You   can accomplish this with input data when you start the program.&lt;/li&gt;  &lt;li&gt;Provide the Java program with a programming interface to the specific   database server that you intend to use.  Assuming that the programming   interface has been installed on your computer, you can also accomplish this   with input data when you run the program.&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The programming interface&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The programming interface deals with the interface peculiarities of the  different database servers.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Sun refers to the process of providing this information to the program as registering the  database server with the Java program.  You will see how this is done in  the sample programs later in this lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The connector download page&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The download page for MySQL Connector/J is &lt;a href="http://dev.mysql.com/downloads/index.html"&gt; http://dev.mysql.com/downloads/index.html&lt;/a&gt;.  As of September 2004, the  following versions are available for downloading from this page:&lt;/span&gt;&lt;/p&gt; &lt;ul&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/connector/j/3.1.html"&gt;MySQL  Connector/J 3.1&lt;/a&gt; -- development release&lt;/li&gt;  &lt;li&gt;&lt;a href="http://dev.mysql.com/downloads/connector/j/3.0.html"&gt;MySQL  Connector/J 3.0&lt;/a&gt; -- production release&lt;/li&gt;  &lt;li&gt;&lt;a href="http://downloads.mysql.com/archives.php"&gt;Older releases&lt;/a&gt; -- older  releases &lt;i&gt;(only recommended for special needs)&lt;/i&gt;&lt;/li&gt;  &lt;li&gt;&lt;a href="http://downloads.mysql.com/snapshots.php"&gt;Snapshots&lt;/a&gt; -- source  code snapshots of the development trees&lt;/li&gt; &lt;/span&gt;&lt;/ul&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As with the MySQL database server software, these individual links are likely  to become obsolete as new versions of the software are released.  Hopefully  the link to &lt;a href="http://dev.mysql.com/downloads/index.html"&gt; http://dev.mysql.com/downloads/index.html&lt;/a&gt; will remain intact.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The MySQL Connector/J 3.0 distribution file&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Because I was very interested in stability, I elected to download and install &lt;b&gt;MySQL Connector/J 3.0&lt;/b&gt;, identified above as the production release.   This resulted in the download of a file named &lt;b&gt; mysql-connector-java-3.0.15-ga.zip&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This zip file encapsulates 194 individual files in different folders  including source code files, class files, pdf files, xml files, jar files,  license files, files with no extensions, a manifest file, HTML files, and other file types  not listed here.  &lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The zip file also contains several java programs in a folder named &lt;b&gt; testsuite&lt;/b&gt; that can be used to test your installation.    You  may find them useful for that purpose.  In addition, these programs  illustrate a variety of database operations using JDBC, so you may find them  useful as example programs as well.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Fortunately, as I will explain below, all but one of these files can be  ignored insofar as installation of the connector software is concerned.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="font-weight: bold;"&gt;&lt;/span&gt;&lt;span style="font-weight: bold;font-size:100%;" &gt;Installing MySQL Connector/J&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;General installation instructions&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The following statement appears in the connector documentation Section  2.2.1. entitled &lt;i&gt;Setting the CLASSPATH (For Standalone Use).&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"Once you have un-archived the distribution archive, you can install   the driver in one of two ways: Either copy the "com" and "org"   subdirectories and all of their contents to anywhere you like, and put the   directory holding the "com" and "org" subdirectories in your classpath, or   put mysql-connector-java-[version]-bin.jar in your classpath, either by   adding the FULL path to it to your CLASSPATH environment variable, or by   copying the .jar file to $JAVA_HOME/jre/lib/ext.&lt;/i&gt;"&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;My installation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Actually, the above quotation describes three options instead of just two.  To make a long story short, I elected  the third option.  I extracted  the jar file named &lt;b&gt;mysql-connector-java-3.0.15-ga-bin.jar&lt;/b&gt; from the zip  file and copied it into the folder named &lt;b&gt;c:\j2sdk1.4.2\jre\lib\ext&lt;/b&gt;, which  is the installation directory tree for the currently installed version of Java  on my computer.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The advantage of doing it this way was that I didn't have to modify the  classpath environment variable.  The disadvantage is that the next time I  upgrade to a new version of Java, I must remember to save the MySQL connector  jar file and copy it into the directory tree for my new Java installation.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Your installation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;If you prefer the first option, the connector documentation contains a wealth  of information to help you perform the necessary steps to modify the classpath,  etc.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Testing the installation&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I didn't use any of the test programs mentioned above in the folder named &lt;b&gt; testsuite&lt;/b&gt;.  Rather, I tested my installation using JDBC programs that I  had developed earlier using a different SQL database server.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;You can use the test programs mentioned earlier in the &lt;b&gt;testsuite &lt;/b&gt; folder to test your installation.  Also, I will provide and explain three  sample JDBC programs later in this lesson that you can use to test your  installation.  Before you can test the installation, however, you must  start the MySQL database server running.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Starting the database server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;At this point, all of the software necessary to use the database server  in a JDBC program should be installed on your computer ready for use.  The next step is to  confirm that you can start the database server running.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Selecting a Windows server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;I found it necessary to pull together several pieces of information from the  database server documentation to determine the best way to start the server from  a command line.  For example, the following table is found in the database  server documentation, Section  2.2.1.4 entitled &lt;i&gt;Selecting a Windows  Server.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;table border="1"&gt;  &lt;tbody&gt;&lt;tr&gt;   &lt;td&gt;&lt;strong&gt;Binary&lt;/strong&gt; &lt;/td&gt;   &lt;td width="263"&gt;&lt;strong&gt;Description&lt;/strong&gt;   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr&gt;   &lt;td&gt;mysqld &lt;/td&gt;   &lt;td width="263"&gt;Compiled with full debugging and automatic memory    allocation checking, symbolic links, and InnoDB and BDB tables.   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr&gt;   &lt;td&gt;mysqld-opt &lt;/td&gt;   &lt;td width="263"&gt;Optimized binary. From version 4.0 on, InnoDB is    enabled. Before 4.0, this server includes no transactional table    support.   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr&gt;   &lt;td&gt;mysqld-nt &lt;/td&gt;   &lt;td width="263"&gt;Optimized binary for Windows NT, 2000, and XP with    support for named pipes.   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr&gt;   &lt;td&gt;mysqld-max &lt;/td&gt;   &lt;td width="263"&gt;Optimized binary with support for symbolic links, and    InnoDB and BDB tables.   &lt;/td&gt;  &lt;/tr&gt;  &lt;tr&gt;   &lt;td&gt;mysqld-max-nt &lt;/td&gt;   &lt;td width="263"&gt;Like mysqld-max, but compiled with support for named    pipes.   &lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Explaining the different types of servers&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The following explanation follows the table:&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;"We have found that the server with the most generic name (mysqld) is   the one that many users are likely to choose by default. However, that is   also the server that results in the highest memory and CPU use due to the   inclusion of full debugging support. The server named &lt;b&gt;mysqld-opt&lt;/b&gt; is a   better general-use server choice to make instead if you don't need debugging   support and don't want the maximal feature set offered by the -max servers   or named pipe support offered by the -nt servers."&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Changes in MySQL 4.1.2&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;This is followed by another explanation indicating that beginning with MySQL  4.1.2, the server names were changed eliminating the server name &lt;b&gt;mysqld-opt&lt;/b&gt;  and replacing the debug version &lt;i&gt;(&lt;b&gt;mysqld&lt;/b&gt;)&lt;/i&gt; with &lt;b&gt;mysqld-debug&lt;/b&gt;.   Therefore, if you are installing MySQL 4.1.2 or a later version, you should use the  syntax &lt;b&gt;mysqld&lt;/b&gt; instead of &lt;b&gt;mysqld-opt&lt;/b&gt; to start the server running  from an optimized binary file.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Starting MySQL 4.0.21&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Since I am running MySQL 4.0.21 and need to make certain that what I am doing  is compatible with a large number of students having different operating systems, I concluded that I should start the server  running by using the syntax &lt;b&gt;mysqld-opt&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Section &lt;span style="font-weight: 400;"&gt;2.2.1.5 of the database server  documentation entitled &lt;i&gt;Starting the Server for the First Time&lt;/i&gt; indicates  that the following command should be used at the command prompt to start the  server running:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-weight: 700;"&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;C:\mysql\bin\mysqld --console&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;As I understand it, the purpose of &lt;b&gt;--console&lt;/b&gt; is to cause error messages to be displayed on  the standard error device &lt;i&gt;(typically the screen)&lt;/i&gt; rather than to be  entered into an error log file.  This is what I want to happen.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Combining the two pieces of information given above, I concluded that I  should start the MySQL database server by entering the following command at a  command prompt:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;C:\mysql\bin\mysqld-opt --console&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Encapsulated in a batch file&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Therefore, I created a batch file named &lt;b&gt;MySqlStart.bat&lt;/b&gt; and linked that  file to an icon on the desktop for convenience.  The batch file contains  the two commands shown in Listing 1 and repeated later in Listing 30 near the  end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;pause&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 1 Contents of MySqlStart.bat&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The startup screen output&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 1 shows the screen output following the execution of the batch file  named &lt;b&gt;MySqlStart.bat&lt;/b&gt;.&lt;/span&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(Note that it was necessary for me to manually enter a line break   ahead of the word port to cause the screen output to fit in this narrow   publication format.)&lt;/i&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\mysql&gt;C:\mysql\bin\mysqld-opt --console&lt;br /&gt;040918 13:59:47 InnoDB: Started&lt;br /&gt;C:\mysql\bin\mysqld-opt: ready for connections.&lt;br /&gt;Version: '4.0.21' socket: ''&lt;br /&gt;port: 3306 Source distribution&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 1 MySQL database server startup sequence&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;The process window shown in Figure 1 remains open and active until the server  is stopped.  It should be possible to connect to the server using JDBC  during this period.&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;Stopping the database server&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;It is probably a good idea to shut down the server before shutting down the  computer.  Section 2.2.1.6 of the database server documentation entitled &lt;i&gt; Starting MySQL from the Windows Command Line&lt;/i&gt; states that you can stop the  MySQL server by executing the following command:&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;b&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;/b&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Therefore, I created a batch file named &lt;b&gt;MySqlStop.bat&lt;/b&gt; and linked that  file to an icon on my desktop to make it convenient to stop the server.   The batch file contains the two commands shown in Listing 2 and repeated in  Listing 31 near the end of the lesson.&lt;/span&gt;&lt;/p&gt; &lt;table bg border="1" cols="1" width="400" style="color:#ffff00;"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;C:\mysql\bin\mysqladmin -u root shutdown&lt;br /&gt;pause&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family:Courier New,Courier;"&gt;Listing 2 Contents of MySqlStop.bat&lt;/span&gt;&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt;  &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;b&gt;The screen output at server shutdown&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Figure 2 shows the screen output in the server process window when the file  named &lt;b&gt;MySqlStop.bat&lt;/b&gt; is executed.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;table bgcolor="#ccffff" border="1" cols="1" width="400"&gt;   &lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;       &lt;pre&gt;040918 14:00:02 C:\mysql\bin\mysqld-opt:&lt;br /&gt;Normal shutdown&lt;br /&gt;&lt;br /&gt;040918 14:00:03 InnoDB: Starting shutdown...&lt;br /&gt;040918 14:00:05 InnoDB: Shutdown completed&lt;br /&gt;040918 14:00:05 C:\mysql\bin\mysqld-opt:&lt;br /&gt;Shutdown Complete&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C:\mysql&gt;pause&lt;br /&gt;Press any key to continue . . .&lt;br /&gt;&lt;/pre&gt;       &lt;pre&gt;&lt;b&gt;Figure 2 Screen output when MySQL server is&lt;br /&gt;        stopped.&lt;/b&gt;&lt;/pre&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt; &lt;/table&gt; &lt;blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt; &lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;i&gt;(As before, it was necessary for me to manually enter line breaks in   Figure 2 to cause the screen output to fit in this narrow publication   format.)&lt;/i&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt; &lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;Once the server is shut down, attempts to connect to the server from JDBC will  fail.&lt;/span&gt;&lt;p&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;font-size:-1;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;a name="Installation_and_Preparation_of_MySQL"&gt;&lt;/a&gt;&lt;span style="font-weight: bold;font-size:85%;" &gt;&lt;span&gt;&lt;span style="font-family:Verdana, Arial, Helvetica;"&gt;&lt;span style="color:#000000;"&gt;&lt;a name="Installation_and_Preparation_of_MySQL"&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-2718437797363594456?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/2718437797363594456/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=2718437797363594456' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/2718437797363594456'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/2718437797363594456'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/using-jdbc-with-mysql-1.html' title='Using JDBC with MySQL 1'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-1749690317724522739</id><published>2007-04-11T00:05:00.000-07:00</published><updated>2007-04-11T00:28:55.295-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='oracle.automation.inc'/><title type='text'>Oracle basic training</title><content type='html'>- What is Oracle?&lt;br /&gt;   a relational database management system (rdbms) produced by Oracle corporation in the late seventies.&lt;br /&gt;&lt;br /&gt;- What is a database?&lt;br /&gt;   a mechanism for storage, retrieval, and maintenance of data or related information.&lt;br /&gt;&lt;br /&gt;- What is a relational database?&lt;br /&gt;   a series of separate (but relatable) tables of information&lt;br /&gt;&lt;br /&gt;- what is table?&lt;br /&gt;   a set of closely related data items comprised of rows and columns.&lt;br /&gt;&lt;br /&gt;- what is a row?&lt;br /&gt;   a record of information which is comprised of logicaly related data itmes.&lt;br /&gt;&lt;br /&gt;- What is a column?&lt;br /&gt;   a specific type or category of information.&lt;br /&gt;&lt;br /&gt;- Each table contains data about some major item of inerest.&lt;br /&gt;   typical tables may contain data on students or animals.&lt;br /&gt;   tables are frequently referred to as entities.&lt;br /&gt;&lt;br /&gt;- Each individual row in a table represents one unique occurrence of that item or enity.&lt;br /&gt;   a row in the student table represents a unique student.&lt;br /&gt;   a row in the animal table represents a unique animal.&lt;br /&gt;&lt;br /&gt;- Each column in the table contains a specific piece of information for each row.&lt;br /&gt;   the last name column will contain different values (last name) for each row (or student) in the student table.&lt;br /&gt;&lt;br /&gt;- All rdbms's provide a method to query (i.e., extract and display) data contained in tables.&lt;br /&gt;   this is refered to as the "Query Language."&lt;br /&gt;&lt;br /&gt;- many rdbms's have chosen to standardize on a query language called SQL (pronounced SEE-QUIL).&lt;br /&gt;   SQL stands for "Structured Query Language."&lt;br /&gt;&lt;br /&gt;- SQL provides many different ways to manipulate and display data in tables.&lt;br /&gt;&lt;br /&gt;- SQL allows specifie columns and rows from a table to be displayed.&lt;br /&gt;&lt;br /&gt;- Data are frequently required from two or more seperate tables on the same report.&lt;br /&gt;   this requires that the tables be "joined" together to obtain the desired output.&lt;br /&gt;   this requires that the tables have a common column to match rows.&lt;br /&gt;&lt;br /&gt;- To join tables, an rdbms follows these steps:&lt;br /&gt;    1. retrieve a row from one table.&lt;br /&gt;    2. match the value found in a specified column to the values in a similar column for all rows in the second tables.&lt;br /&gt;    3. if a matching value is found, retrieve that row from the second table and return (display) the rows from both tables.&lt;br /&gt;&lt;br /&gt;- The Oracle rdbms is comprised of several discrete pieces:&lt;br /&gt;    1. the database data: the actual tables containing the raw data.&lt;br /&gt;    2. the kernel: a software program that contains all access to the database.&lt;br /&gt;    2. the Oracle products (tools): seperate programs that perform various tasks and functions. each product communicates with the database through the kernel.&lt;br /&gt;&lt;br /&gt;- SQL *Plus is the primary query tool in Oracle.  an ansi standard strucured query language "plus" additional commands.&lt;br /&gt;&lt;br /&gt;- SQL *Form is used to create data entry screens and menus.&lt;br /&gt;&lt;br /&gt;- SQL *Menu is used to create menu.&lt;br /&gt;&lt;br /&gt;- SQL *ReportWriter is used to create complex reports.&lt;br /&gt;&lt;br /&gt;- Oracle *Forms is used to create data entry screens and menus in a GUI environment.&lt;br /&gt;&lt;br /&gt;- Oracle *Reports is used to create complex reports in a GUI environment.&lt;br /&gt;&lt;br /&gt;- Oracle *Graphics is used to create applications incorporating charts and displays in a GUI environment.&lt;br /&gt;&lt;br /&gt;- Procedure Builder is used to develop PL/SQL program units.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-1749690317724522739?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/1749690317724522739/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=1749690317724522739' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/1749690317724522739'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/1749690317724522739'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/oracle-basic-training.html' title='Oracle basic training'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-1915493642626904587</id><published>2007-04-09T23:10:00.000-07:00</published><updated>2007-04-09T23:13:06.304-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>Chapter 5. Database Administration</title><content type='html'>&lt;h2 class="title"&gt;5.1. Overview of Server-Side Programs&lt;/h2&gt;&lt;p&gt;       &lt;/p&gt;&lt;p&gt;The MySQL server, &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;, is the main program       that does most of the work in a MySQL installation. The server is       accompanied by several related scripts that perform setup       operations when you install MySQL or that assist you in starting       and stopping the server. This section provides an overview of the       server and related programs. The following sections provide more       detailed information about each of these programs.     &lt;/p&gt; &lt;p&gt;       Each MySQL program takes many different options. Most programs       provide a &lt;code class="option"&gt;--help&lt;/code&gt; option that you can use to get a       description of the program's different options. For example, try       &lt;span&gt;&lt;strong class="command"&gt;mysqld --help&lt;/strong&gt;&lt;/span&gt;.     &lt;/p&gt;        You can override default option values for MySQL programs by       specifying options on the command line or in an option file.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;       The following list briefly describes the MySQL server and       server-related programs:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636658"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           The SQL daemon (that is, the MySQL server). To use client           programs, &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; must be running, because           clients gain access to databases by connecting to the server.           See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysqld.html" title="5.2. mysqld — The MySQL Server"&gt;Section 5.2, “&lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; — The MySQL Server”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636691"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           A server startup script. &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt;           attempts to start &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysqld-safe.html" title="5.3.1. mysqld_safe — MySQL Server Startup Script"&gt;Section 5.3.1, “&lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; — MySQL Server Startup Script”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636725"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql.server&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           A server startup script. This script is used on systems that           use System V-style run directories containing scripts that           start system services for particular run levels. It invokes           &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; to start the MySQL server. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-server.html" title="5.3.2. mysql.server — MySQL Server Startup Script"&gt;Section 5.3.2, “&lt;span&gt;&lt;strong class="command"&gt;mysql.server&lt;/strong&gt;&lt;/span&gt; — MySQL Server Startup Script”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636759"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysqld_multi&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           A server startup script that can start or stop multiple           servers installed on the system. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysqld-multi.html" title="5.3.3. mysqld_multi — Manage Multiple MySQL Servers"&gt;Section 5.3.3, “&lt;span&gt;&lt;strong class="command"&gt;mysqld_multi&lt;/strong&gt;&lt;/span&gt; — Manage Multiple MySQL Servers”&lt;/a&gt;. As of MySQL 5.0.3 (Unix-like           systems) or 5.0.13 (Windows), an alternative to           &lt;span&gt;&lt;strong class="command"&gt;mysqld_multi&lt;/strong&gt;&lt;/span&gt; is           &lt;code class="literal"&gt;mysqlmanager&lt;/code&gt;, the MySQL Instance Manager.           See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/instance-manager.html" title="5.4. mysqlmanager — The MySQL Instance Manager"&gt;Section 5.4, “&lt;span&gt;&lt;strong class="command"&gt;mysqlmanager&lt;/strong&gt;&lt;/span&gt; — The MySQL Instance Manager”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636802"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysqlmanager&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           The MySQL Instance Manager, a program for monitoring and           managing MySQL servers. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/instance-manager.html" title="5.4. mysqlmanager — The MySQL Instance Manager"&gt;Section 5.4, “&lt;span&gt;&lt;strong class="command"&gt;mysqlmanager&lt;/strong&gt;&lt;/span&gt; — The MySQL Instance Manager”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       There are several other programs that are related to MySQL       installation or upgrading:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636844"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;comp_err&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program is used during the MySQL build/installation           process. It compiles error message files from the error source           files. See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/comp-err.html" title="5.5.1. comp_err — Compile MySQL Error Message File"&gt;Section 5.5.1, “&lt;span&gt;&lt;strong class="command"&gt;comp_err&lt;/strong&gt;&lt;/span&gt; — Compile MySQL Error Message File”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636872"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;make_binary_distribution&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program makes a binary release of a compiled MySQL. This           could be sent by FTP to           &lt;code class="filename"&gt;/pub/mysql/upload/&lt;/code&gt; on           &lt;code class="literal"&gt;ftp.mysql.com&lt;/code&gt; for the convenience of other           MySQL users.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636904"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;make_win_bin_dist&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program is used on Windows. It packages a MySQL           distribution for installation after the source distribution           has been built. See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/make-win-bin-dist.html" title="5.5.2. make_win_bin_dist — Package MySQL Distribution as ZIP Archive"&gt;Section 5.5.2, “&lt;span&gt;&lt;strong class="command"&gt;make_win_bin_dist&lt;/strong&gt;&lt;/span&gt; — Package MySQL Distribution as ZIP Archive”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636932"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql_fix_privilege_tables&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program is used after a MySQL upgrade operation. It           updates the grant tables with any changes that have been made           in newer versions of MySQL. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-fix-privilege-tables.html" title="5.5.4. mysql_fix_privilege_tables — Upgrade MySQL System Tables"&gt;Section 5.5.4, “&lt;span&gt;&lt;strong class="command"&gt;mysql_fix_privilege_tables&lt;/strong&gt;&lt;/span&gt; — Upgrade MySQL System Tables”&lt;/a&gt;.         &lt;/p&gt; &lt;p&gt;           Note: As of MySQL 5.0.19, this program has been superseded by           &lt;span&gt;&lt;strong class="command"&gt;mysql_upgrade&lt;/strong&gt;&lt;/span&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2636969"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql_install_db&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This script creates the MySQL database and initializes the           grant tables with default privileges. It is usually executed           only once, when first installing MySQL on a system. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/unix-post-installation.html" title="2.4.15.2. Unix Post-Installation Procedures"&gt;Section 2.4.15.2, “Unix Post-Installation Procedures”&lt;/a&gt;, and           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-install-db.html" title="5.5.5. mysql_install_db — MySQL Data Directory Initialization Script"&gt;Section 5.5.5, “&lt;span&gt;&lt;strong class="command"&gt;mysql_install_db&lt;/strong&gt;&lt;/span&gt; — MySQL Data Directory Initialization Script”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2637003"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql_secure_installation&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program enables you to improve the security of your MySQL           installation. SQL. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-secure-installation.html" title="5.5.6. mysql_secure_installation — Improve MySQL Installation Security"&gt;Section 5.5.6, “&lt;span&gt;&lt;strong class="command"&gt;mysql_secure_installation&lt;/strong&gt;&lt;/span&gt; — Improve MySQL Installation Security”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2637031"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql_tzinfo_to_sql&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program loads the time zone tables in the           &lt;code class="literal"&gt;mysql&lt;/code&gt; database using the contents of the           host system &lt;em class="firstterm"&gt;zoneinfo&lt;/em&gt; database (the set           of files describing time zones). SQL. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-tzinfo-to-sql.html" title="5.5.7. mysql_tzinfo_to_sql — Load the Time Zone Tables"&gt;Section 5.5.7, “&lt;span&gt;&lt;strong class="command"&gt;mysql_tzinfo_to_sql&lt;/strong&gt;&lt;/span&gt; — Load the Time Zone Tables”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2637066"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;mysql_upgrade&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program is used after a MySQL upgrade operation. It           checks tables for incompatibilities and repairs them if           necessary, and updates the grant tables with any changes that           have been made in newer versions of MySQL. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-upgrade.html" title="5.5.8. mysql_upgrade — Check Tables for MySQL Upgrade"&gt;Section 5.5.8, “&lt;span&gt;&lt;strong class="command"&gt;mysql_upgrade&lt;/strong&gt;&lt;/span&gt; — Check Tables for MySQL Upgrade”&lt;/a&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           &lt;a class="indexterm" name="id2637096"&gt;&lt;/a&gt;            &lt;span&gt;&lt;strong class="command"&gt;make_win_src_distribution&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           This program is used on Unix or Unix-like systems to create a           MySQL source distribution that can be compiled on Windows. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-source-build.html#windows-bitkeeper-build" title="2.4.14.6.4. Creating a Windows Source Package from the Latest Development Source"&gt;Section 2.4.14.6.4, “Creating a Windows Source Package from the Latest Development Source”&lt;/a&gt;, and           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/make-win-src-distribution.html" title="5.5.3. make_win_src_distribution — Create Source Distribution for Windows"&gt;Section 5.5.3, “&lt;span&gt;&lt;strong class="command"&gt;make_win_src_distribution&lt;/strong&gt;&lt;/span&gt; — Create Source Distribution for Windows”&lt;/a&gt;.         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-1915493642626904587?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/1915493642626904587/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=1915493642626904587' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/1915493642626904587'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/1915493642626904587'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/chapter-5-database-administration.html' title='Chapter 5. Database Administration'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-3787700480061068711</id><published>2007-04-09T22:33:00.000-07:00</published><updated>2007-04-09T23:08:27.889-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>Chapter 4. Using MySQL Programs</title><content type='html'>&lt;p&gt;     This chapter provides a brief overview of the command-line programs     provided by MySQL AB and discusses the general syntax for specifying     options when you run these programs. Most programs have options that     are specific to their own operation, but the option syntax is     similar for all of them. Later chapters provide more detailed     descriptions of individual programs, including which options they     recognize.   &lt;/p&gt; &lt;p&gt;     MySQL AB also provides three GUI client programs for use with MySQL     Server:   &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;         MySQL Administrator: This tool is used for administering MySQL         servers, databases, tables, and user accounts.       &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;         MySQL Query Browser: This graphical tool is provided by MySQL AB         for creating, executing, and optimizing queries on MySQL         databases.       &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;         MySQL Migration Toolkit: This tool helps you migrate schemas and         data from other relational database management systems for use         with MySQL.       &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;     These GUI programs each have their own manuals that you can access     at &lt;a href="http://dev.mysql.com/doc/" target="_top"&gt;http://dev.mysql.com/doc/&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h2 class="title"&gt;4.1. Overview of MySQL Programs&lt;/h2&gt; &lt;p&gt;       MySQL AB provides several types of programs:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;           The MySQL server and server startup scripts:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; is the MySQL server.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt;,               &lt;span&gt;&lt;strong class="command"&gt;mysql.server&lt;/strong&gt;&lt;/span&gt;, and               &lt;span&gt;&lt;strong class="command"&gt;mysqld_multi&lt;/strong&gt;&lt;/span&gt; are server startup               scripts.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysql_install_db&lt;/strong&gt;&lt;/span&gt; initializes the data               directory and the initial databases.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               MySQL Instance Manager monitors and manages MySQL Server               instances.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;  &lt;/li&gt;&lt;li&gt; &lt;p&gt;           Client programs that access the server:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is a command-line client for               executing SQL statements interactively or in batch mode.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; is an administrative client.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqlcheck&lt;/strong&gt;&lt;/span&gt; performs table maintenance               operations.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqldump&lt;/strong&gt;&lt;/span&gt; and               &lt;span&gt;&lt;strong class="command"&gt;mysqlhotcopy&lt;/strong&gt;&lt;/span&gt; make database backups.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqlimport&lt;/strong&gt;&lt;/span&gt; imports data files.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqlshow&lt;/strong&gt;&lt;/span&gt; displays information about               databases and tables.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;  &lt;/li&gt;&lt;li&gt; &lt;p&gt;           Utility programs that operate independently of the server:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;myisamchk&lt;/strong&gt;&lt;/span&gt; performs table maintenance               operations.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;myisampack&lt;/strong&gt;&lt;/span&gt; produces compressed,               read-only tables.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;mysqlbinlog&lt;/strong&gt;&lt;/span&gt; is a tool for processing               binary log files.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span&gt;&lt;strong class="command"&gt;perror&lt;/strong&gt;&lt;/span&gt; displays the meaning of error               codes.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       Most MySQL distributions include all of these programs, except for       those programs that are platform-specific. (For example, the       server startup scripts are not used on Windows.) The exception is       that RPM distributions are more specialized. There is one RPM for       the server, another for client programs, and so forth. If you       appear to be missing one or more programs, see       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/installing.html" title="Chapter 2. Installing and Upgrading MySQL"&gt;Chapter 2, &lt;i&gt;Installing and Upgrading MySQL&lt;/i&gt;&lt;/a&gt;, for information on types of       distributions and what they contain. It may be that you have a       distribution that does not include all programs and you need to       install something else.&lt;/p&gt;&lt;br /&gt;&lt;h2 class="title"&gt;4.2. Invoking MySQL Programs&lt;/h2&gt;&lt;p&gt;       To invoke a MySQL program from the command line (that is, from       your shell or command prompt), enter the program name followed by       any options or other arguments needed to instruct the program what       you want it to do. The following commands show some sample program       invocations. “&lt;span class="quote"&gt;&lt;code class="literal"&gt;shell&gt;&lt;/code&gt;&lt;/span&gt;”       represents the prompt for your command interpreter; it is not part       of what you type. The particular prompt you see depends on your       command interpreter. Typical prompts are &lt;code class="literal"&gt;$&lt;/code&gt; for       &lt;span&gt;&lt;strong class="command"&gt;sh&lt;/strong&gt;&lt;/span&gt; or &lt;span&gt;&lt;strong class="command"&gt;bash&lt;/strong&gt;&lt;/span&gt;,       &lt;code class="literal"&gt;%&lt;/code&gt; for &lt;span&gt;&lt;strong class="command"&gt;csh&lt;/strong&gt;&lt;/span&gt; or       &lt;span&gt;&lt;strong class="command"&gt;tcsh&lt;/strong&gt;&lt;/span&gt;, and &lt;code class="literal"&gt;C:\&gt;&lt;/code&gt; for the       Windows &lt;span&gt;&lt;strong class="command"&gt;command.com&lt;/strong&gt;&lt;/span&gt; or       &lt;span&gt;&lt;strong class="command"&gt;cmd.exe&lt;/strong&gt;&lt;/span&gt; command interpreters.     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -u root test&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysqladmin extended-status variables&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysqlshow --help&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysqldump --user=root personnel&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt;        Arguments that begin with a single or double dash       (‘&lt;code class="literal"&gt;-&lt;/code&gt;’,       ‘&lt;code class="literal"&gt;--&lt;/code&gt;’) are option arguments. Options       typically specify the type of connection a program should make to       the server or affect its operational mode.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;       Non-option arguments (arguments with no leading dash) provide       additional information to the program. For example, the       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; program interprets the first non-option       argument as a database name, so the command &lt;code class="literal"&gt;mysql -u root       test&lt;/code&gt; indicates that you want to use the       &lt;code class="literal"&gt;test&lt;/code&gt; database.     &lt;/p&gt; &lt;p&gt;       Later sections that describe individual programs indicate which       options a program understands and describe the meaning of any       additional non-option arguments.     &lt;/p&gt; &lt;p&gt;       Some options are common to a number of programs. The most common       of these are the &lt;code class="option"&gt;--host&lt;/code&gt; (or &lt;code class="option"&gt;-h&lt;/code&gt;),       &lt;code class="option"&gt;--user&lt;/code&gt; (or &lt;code class="option"&gt;-u&lt;/code&gt;), and       &lt;code class="option"&gt;--password&lt;/code&gt; (or &lt;code class="option"&gt;-p&lt;/code&gt;) options that       specify connection parameters. They indicate the host where the       MySQL server is running, and the username and password of your       MySQL account. All MySQL client programs understand these options;       they allow you to specify which server to connect to and the       account to use on that server.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;       Other connection options are &lt;code class="option"&gt;--port&lt;/code&gt; (or       &lt;code class="option"&gt;-P&lt;/code&gt;) to specify a TCP/IP port number and       &lt;code class="option"&gt;--socket&lt;/code&gt; (or &lt;code class="option"&gt;-S&lt;/code&gt;) to specify a       Unix socket file on Unix (or named pipe name on Windows).     &lt;/p&gt; &lt;p&gt;       The default hostname is &lt;code class="literal"&gt;localhost&lt;/code&gt;. For client       programs on Unix, the hostname &lt;code class="literal"&gt;localhost&lt;/code&gt; is       special. It causes the client to connect to the MySQL server       through a Unix socket file. This occurs even if a       &lt;code class="option"&gt;--port&lt;/code&gt; or &lt;code class="option"&gt;-P&lt;/code&gt; option is given to       specify a port number. To ensure that the client makes a TCP/IP       connection to the local server, use &lt;code class="option"&gt;--host&lt;/code&gt; or       &lt;code class="option"&gt;-h&lt;/code&gt; to specify a hostname value of       &lt;code class="literal"&gt;127.0.0.1&lt;/code&gt;, or the IP address or name of the       local server. You can also specify the connection protocol       explicitly, even for &lt;code class="literal"&gt;localhost&lt;/code&gt;, by using the       &lt;code class="option"&gt;--protocol=tcp&lt;/code&gt; option.     &lt;/p&gt; &lt;a class="indexterm" name="id2634063"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2634070"&gt;&lt;/a&gt;&lt;p&gt;       You may find it necessary to invoke MySQL programs using the       pathname to the &lt;code class="filename"&gt;bin&lt;/code&gt; directory in which they       are installed. This is likely to be the case if you get a       “&lt;span class="quote"&gt;program not found&lt;/span&gt;” error whenever you attempt to run       a MySQL program from any directory other than the       &lt;code class="filename"&gt;bin&lt;/code&gt; directory. To make it more convenient to       use MySQL, you can add the pathname of the       &lt;code class="filename"&gt;bin&lt;/code&gt; directory to your &lt;code class="literal"&gt;PATH&lt;/code&gt;       environment variable setting. That enables you to run a program by       typing only its name, not its entire pathname. For example, if       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is installed in       &lt;code class="filename"&gt;/usr/local/mysql/bin&lt;/code&gt;, you'll be able to run       it by invoking it as &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;; it will not be       necessary to invoke it as       &lt;span&gt;&lt;strong class="command"&gt;/usr/local/mysql/bin/mysql&lt;/strong&gt;&lt;/span&gt;.     &lt;/p&gt;        Consult the documentation for your command interpreter for       instructions on setting your &lt;code class="literal"&gt;PATH&lt;/code&gt; variable. The       syntax for setting environment variables is interpreter-specific.&lt;br /&gt;&lt;br /&gt;&lt;h2 class="title"&gt;4.3. Specifying Program Options&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;       There are several ways to specify options for MySQL programs:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           List the options on the command line following the program           name. This is most common for options that apply to a specific           invocation of the program.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           List the options in an option file that the program reads when           it starts. This is common for options that you want the           program to use each time it runs.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           List the options in environment variables. This method is           useful for options that you want to apply each time the           program runs. In practice, option files are used more commonly           for this purpose. However,           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/multiple-unix-servers.html" title="5.12.2. Running Multiple Servers on Unix"&gt;Section 5.12.2, “Running Multiple Servers on Unix”&lt;/a&gt;, discusses one           situation in which environment variables can be very helpful.           It describes a handy technique that uses such variables to           specify the TCP/IP port number and Unix socket file for both           the server and client programs.         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       MySQL programs determine which options are given first by       examining environment variables, then by reading option files, and       then by checking the command line. This means that environment       variables have the lowest precedence and command-line options the       highest.     &lt;/p&gt; &lt;p&gt;       Because options are processed in order, if an option is specified       multiple times, the last occurrence takes precedence. The       following command causes &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to connect to       the server running on &lt;code class="literal"&gt;localhost&lt;/code&gt;:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -h example.com -h localhost&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       If conflicting or related options are given, later options take       precedence over earlier options. The following command runs       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; in “&lt;span class="quote"&gt;no column names&lt;/span&gt;” mode:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql --column-names --skip-column-names&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       An option can be specified by writing it in full or as any       unambiguous prefix. For example, the &lt;code class="option"&gt;--compress&lt;/code&gt;       option can be given to &lt;span&gt;&lt;strong class="command"&gt;mysqldump&lt;/strong&gt;&lt;/span&gt; as       &lt;code class="option"&gt;--compr&lt;/code&gt;, but not as &lt;code class="option"&gt;--comp&lt;/code&gt;       because that is ambiguous:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysqldump --comp&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysqldump: ambiguous option '--comp' (compatible, compress)&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       Be aware that the use of option prefixes can cause problems in the       event that new options are implemented for a program. A prefix       that is unambigious now might become ambiguous in the future.     &lt;/p&gt; &lt;p&gt;       You can take advantage of the way that MySQL programs process       options by specifying default values for a program's options in an       option file. That enables you to avoid typing them each time you       run the program, but also allows you to override the defaults if       necessary by using command-line options.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;4.3.1. Using Options on the Command Line&lt;/h3&gt; &lt;p&gt;         Program options specified on the command line follow these         rules:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             Options are given after the command name.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             An option argument begins with one dash or two dashes,             depending on whether it has a short name or a long name.             Many options have both forms. For example,             &lt;code class="option"&gt;-?&lt;/code&gt; and &lt;code class="option"&gt;--help&lt;/code&gt; are the             short and long forms of the option that instructs a MySQL             program to display its help message.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Option names are case sensitive. &lt;code class="option"&gt;-v&lt;/code&gt; and             &lt;code class="option"&gt;-V&lt;/code&gt; are both legal and have different             meanings. (They are the corresponding short forms of the             &lt;code class="option"&gt;--verbose&lt;/code&gt; and &lt;code class="option"&gt;--version&lt;/code&gt;             options.)           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Some options take a value following the option name. For             example, &lt;code class="option"&gt;-h localhost&lt;/code&gt; or             &lt;code class="option"&gt;--host=localhost&lt;/code&gt; indicate the MySQL server             host to a client program. The option value tells the program             the name of the host where the MySQL server is running.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;p&gt;             For a long option that takes a value, separate the option             name and the value by an ‘&lt;code class="literal"&gt;=&lt;/code&gt;’             sign. For a short option that takes a value, the option             value can immediately follow the option letter, or there can             be a space between: &lt;code class="option"&gt;-hlocalhost&lt;/code&gt; and             &lt;code class="option"&gt;-h localhost&lt;/code&gt; are equivalent. An exception             to this rule is the option for specifying your MySQL             password. This option can be given in long form as             &lt;code class="option"&gt;--password=&lt;em class="replaceable"&gt;&lt;code&gt;pass_val&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;             or as &lt;code class="option"&gt;--password&lt;/code&gt;. In the latter case (with             no password value given), the program prompts you for the             password. The password option also may be given in short             form as             &lt;code class="option"&gt;-p&lt;em class="replaceable"&gt;&lt;code&gt;pass_val&lt;/code&gt;&lt;/em&gt;&lt;/code&gt; or as             &lt;code class="option"&gt;-p&lt;/code&gt;. However, for the short form, if the             password value is given, it must follow the option letter             with &lt;span class="emphasis"&gt;&lt;em&gt;no intervening space&lt;/em&gt;&lt;/span&gt;. The reason             for this is that if a space follows the option letter, the             program has no way to tell whether a following argument is             supposed to be the password value or some other kind of             argument. Consequently, the following two commands have two             completely different meanings:           &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -ptest&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -p test&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;             The first command instructs &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to use             a password value of &lt;code class="literal"&gt;test&lt;/code&gt;, but specifies             no default database. The second instructs             &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to prompt for the password value             and to use &lt;code class="literal"&gt;test&lt;/code&gt; as the default database.           &lt;/p&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         Some options control behavior that can be turned on or off. For         example, the &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; client supports a         &lt;code class="option"&gt;--column-names&lt;/code&gt; option that determines whether         or not to display a row of column names at the beginning of         query results. By default, this option is enabled. However, you         may want to disable it in some instances, such as when sending         the output of &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; into another program that         expects to see only data and not an initial header line.       &lt;/p&gt; &lt;p&gt;         To disable column names, you can specify the option using any of         these forms:       &lt;/p&gt; &lt;pre class="programlisting"&gt;--disable-column-names&lt;br /&gt;--skip-column-names&lt;br /&gt;--column-names=0&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The &lt;code class="option"&gt;--disable&lt;/code&gt; and &lt;code class="option"&gt;--skip&lt;/code&gt;         prefixes and the &lt;code class="literal"&gt;=0&lt;/code&gt; suffix all have the same         effect: They turn the option off.       &lt;/p&gt; &lt;p&gt;         The “&lt;span class="quote"&gt;enabled&lt;/span&gt;” form of the option may be specified         in any of these ways:       &lt;/p&gt; &lt;pre class="programlisting"&gt;--column-names&lt;br /&gt;--enable-column-names&lt;br /&gt;--column-names=1&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         If an option is prefixed by &lt;code class="option"&gt;--loose&lt;/code&gt;, a program         does not exit with an error if it does not recognize the option,         but instead issues only a warning:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql --loose-no-such-option&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql: WARNING: unknown option '--no-such-option'&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The &lt;code class="option"&gt;--loose&lt;/code&gt; prefix can be useful when you run         programs from multiple installations of MySQL on the same         machine and list options in an option file, An option that may         not be recognized by all versions of a program can be given         using the &lt;code class="option"&gt;--loose&lt;/code&gt; prefix (or         &lt;code class="literal"&gt;loose&lt;/code&gt; in an option file). Versions of the         program that recognize the option process it normally, and         versions that do not recognize it issue a warning and ignore it.       &lt;/p&gt; &lt;p&gt;         Another option that may occasionally be useful with         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is the &lt;code class="option"&gt;--execute&lt;/code&gt; or         &lt;code class="option"&gt;-e&lt;/code&gt; option, which can be used to pass SQL         statements to the server. When this option is used,         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; executes the statements and exits. The         statements must be enclosed by quotation marks. For example, you         can use the following command to obtain a list of user accounts:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -u root -p --execute="SELECT User, Host FROM user" mysql&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;******&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+------+-----------+&lt;br /&gt;| User | Host      |&lt;br /&gt;+------+-----------+&lt;br /&gt;|      | gigan     |&lt;br /&gt;| root | gigan     |&lt;br /&gt;|      | localhost |&lt;br /&gt;| jon  | localhost |&lt;br /&gt;| root | localhost |&lt;br /&gt;+------+-----------+&lt;br /&gt;shell&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Note that the long form (&lt;code class="option"&gt;--execute&lt;/code&gt;) is followed         by an equals sign (&lt;code class="literal"&gt;=&lt;/code&gt;).       &lt;/p&gt; &lt;p&gt;         If you wish to use quoted values within a statement, you will         either need to escape the inner quotes, or use a different type         of quotes within the statement from those used to quote the         statement itself. The capabilities of your command processor         dictate your choices for whether you can use single or double         quotation marks and the syntax for escaping quote characters.         For example, if your command processor supports quoting with         single or double quotes, you can double quotes around the         statement, and single quotes for any quoted values within the         statement.       &lt;/p&gt; &lt;p&gt;         In the preceding example, the name of the         &lt;code class="literal"&gt;mysql&lt;/code&gt; database was passed as a separate         argument. However, the same statement could have been executed         using this command, which specifies no default database:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -u root -p --execute="SELECT User, Host FROM mysql.user"&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Multiple SQL statements may be passed on the command line,         separated by semicolons:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -u root -p -e "SELECT VERSION();SELECT NOW()"&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;******&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+------------+&lt;br /&gt;| VERSION()  |&lt;br /&gt;+------------+&lt;br /&gt;| 5.0.19-log |&lt;br /&gt;+------------+&lt;br /&gt;+---------------------+&lt;br /&gt;| NOW()               |&lt;br /&gt;+---------------------+&lt;br /&gt;| 2006-01-05 21:19:04 |&lt;br /&gt;+---------------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The &lt;code class="option"&gt;--execute&lt;/code&gt; or &lt;code class="option"&gt;-e&lt;/code&gt; option may         also be used to pass commands in an analogous fashion to the         &lt;span&gt;&lt;strong class="command"&gt;ndb_mgm&lt;/strong&gt;&lt;/span&gt; management client for MySQL Cluster.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;4.3.2. Using Option Files&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;         Most MySQL programs can read startup options from option files         (also sometimes called configuration files). Option files         provide a convenient way to specify commonly used options so         that they need not be entered on the command line each time you         run a program. For the MySQL server, MySQL provides a number of         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/option-files-preconfigured.html" title="4.3.2.1. Preconfigured Option Files"&gt;preconfigured option         files&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         To determine whether a program reads option files, invoke it         with the &lt;code class="option"&gt;--help&lt;/code&gt; option         (&lt;code class="option"&gt;--verbose&lt;/code&gt; and &lt;code class="option"&gt;--help&lt;/code&gt; for         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;). If the program reads option files,         the help message indicates which files it looks for and which         option groups it recognizes.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: Option files used with         MySQL Cluster programs are covered in         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-cluster-configuration.html" title="15.4. MySQL Cluster Configuration"&gt;Section 15.4, “MySQL Cluster Configuration”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         On Windows, MySQL programs read startup options from the         following files:       &lt;/p&gt; &lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Filename&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;&lt;em class="replaceable"&gt;&lt;code&gt;WINDIR&lt;/code&gt;&lt;/em&gt;\my.ini&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Global options&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;C:\my.cnf&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Global options&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;&lt;em class="replaceable"&gt;&lt;code&gt;INSTALLDIR&lt;/code&gt;&lt;/em&gt;\my.ini&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Global Options&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;defaults-extra-file&lt;/code&gt;&lt;/td&gt; &lt;td&gt;The file specified with                 &lt;code class="option"&gt;--defaults-extra-file=&lt;em class="replaceable"&gt;&lt;code&gt;path&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;,                 if any&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;         &lt;em class="replaceable"&gt;&lt;code&gt;WINDIR&lt;/code&gt;&lt;/em&gt; represents the location of         your Windows directory. This is commonly         &lt;code class="filename"&gt;C:\WINDOWS&lt;/code&gt; or         &lt;code class="filename"&gt;C:\WINNT&lt;/code&gt;. You can determine its exact         location from the value of the &lt;code class="filename"&gt;WINDIR&lt;/code&gt;         environment variable using the following command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;echo %WINDIR%&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         &lt;em class="replaceable"&gt;&lt;code&gt;INSTALLDIR&lt;/code&gt;&lt;/em&gt; represents the         installation directory of MySQL. This is typically         &lt;code class="filename"&gt;C:\&lt;em class="replaceable"&gt;&lt;code&gt;PROGRAMDIR&lt;/code&gt;&lt;/em&gt;\MySQL\MySQL         5.0 Server&lt;/code&gt; where         &lt;em class="replaceable"&gt;&lt;code&gt;PROGRAMDIR&lt;/code&gt;&lt;/em&gt; represents the programs         directory (usually &lt;code class="filename"&gt;Program Files&lt;/code&gt; on         English-language versions of Windows), when MySQL         5.0 has been installed using the installation and         configuration wizards. See         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-config-wizard-starting.html#mysql-config-wizard-starting-windows" title="2.3.6.1.1. The MySQL Server Configuration Wizard on Windows"&gt;Section 2.3.6.1.1, “The MySQL Server Configuration Wizard on Windows”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         On Unix, MySQL programs read startup options from the following         files:       &lt;/p&gt; &lt;a class="indexterm" name="id2635001"&gt;&lt;/a&gt;&lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Filename&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Purpose&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;/etc/my.cnf&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Global options&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;$MYSQL_HOME/my.cnf&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Server-specific options&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;defaults-extra-file&lt;/code&gt;&lt;/td&gt; &lt;td&gt;The file specified with                 &lt;code class="option"&gt;--defaults-extra-file=&lt;em class="replaceable"&gt;&lt;code&gt;path&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;,                 if any&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="filename"&gt;~/.my.cnf&lt;/code&gt;&lt;/td&gt; &lt;td&gt;User-specific options&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;         &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; is an environment variable         containing the path to the directory in which the         server-specific &lt;code class="filename"&gt;my.cnf&lt;/code&gt; file resides. (This         was &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt; prior to MySQL version         5.0.3.)       &lt;/p&gt; &lt;p&gt;         If &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; is not set and you start the         server using the &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; program,         &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; attempts to set         &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; as follows:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             Let &lt;em class="replaceable"&gt;&lt;code&gt;BASEDIR&lt;/code&gt;&lt;/em&gt; and             &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt; represent the pathnames             of the MySQL base directory and data directory,             respectively.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             If there is a &lt;code class="filename"&gt;my.cnf&lt;/code&gt; file in             &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt; but not in             &lt;em class="replaceable"&gt;&lt;code&gt;BASEDIR&lt;/code&gt;&lt;/em&gt;,             &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; sets             &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; to             &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt;.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Otherwise, if &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; is not set and             there is no &lt;code class="filename"&gt;my.cnf&lt;/code&gt; file in             &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt;,             &lt;span&gt;&lt;strong class="command"&gt;mysqld_safe&lt;/strong&gt;&lt;/span&gt; sets             &lt;code class="literal"&gt;MYSQL_HOME&lt;/code&gt; to             &lt;em class="replaceable"&gt;&lt;code&gt;BASEDIR&lt;/code&gt;&lt;/em&gt;.           &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         In MySQL 5.0, use of         &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt; as the location for         &lt;code class="filename"&gt;my.cnf&lt;/code&gt; is deprecated.         &lt;em class="replaceable"&gt;&lt;code&gt;BASEDIR&lt;/code&gt;&lt;/em&gt; is a better location.       &lt;/p&gt; &lt;p&gt;         Typically, &lt;em class="replaceable"&gt;&lt;code&gt;DATADIR&lt;/code&gt;&lt;/em&gt; is         &lt;code class="filename"&gt;/usr/local/mysql/data&lt;/code&gt; for a binary         installation or &lt;code class="filename"&gt;/usr/local/var&lt;/code&gt; for a source         installation. Note that this is the data directory location that         was specified at configuration time, not the one specified with         the &lt;code class="option"&gt;--datadir&lt;/code&gt; option when         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; starts. Use of         &lt;code class="option"&gt;--datadir&lt;/code&gt; at runtime has no effect on where the         server looks for option files, because it looks for them before         processing any options.       &lt;/p&gt; &lt;p&gt;         MySQL looks for option files in the order just described and         reads any that exist. If an option file that you want to use         does not exist, create it with a plain text editor.       &lt;/p&gt; &lt;p&gt;         If multiple instances of a given option are found, the last         instance takes precedence. There is one exception: For         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;, the &lt;span class="emphasis"&gt;&lt;em&gt;first&lt;/em&gt;&lt;/span&gt;         instance of the &lt;code class="option"&gt;--user&lt;/code&gt; option is used as a         security precaution, to prevent a user specified in an option         file from being overridden on the command line.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: On Unix platforms, MySQL         ignores configuration files that are world-writable. This is         intentional, and acts as a security measure.       &lt;/p&gt; &lt;p&gt;         Any long option that may be given on the command line when         running a MySQL program can be given in an option file as well.         To get the list of available options for a program, run it with         the &lt;code class="option"&gt;--help&lt;/code&gt; option.       &lt;/p&gt; &lt;p&gt;         The syntax for specifying options in an option file is similar         to command-line syntax, except that you omit the leading two         dashes. For example, &lt;code class="option"&gt;--quick&lt;/code&gt; or         &lt;code class="option"&gt;--host=localhost&lt;/code&gt; on the command line should be         specified as &lt;code class="literal"&gt;quick&lt;/code&gt; or         &lt;code class="literal"&gt;host=localhost&lt;/code&gt; in an option file. To specify         an option of the form         &lt;code class="option"&gt;--loose-&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt; in         an option file, write it as         &lt;code class="literal"&gt;loose-&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;.       &lt;/p&gt; &lt;p&gt;         Empty lines in option files are ignored. Non-empty lines can         take any of the following forms:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;             &lt;code class="literal"&gt;#&lt;em class="replaceable"&gt;&lt;code&gt;comment&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;,             &lt;code class="literal"&gt;;&lt;em class="replaceable"&gt;&lt;code&gt;comment&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             Comment lines start with ‘&lt;code class="literal"&gt;#&lt;/code&gt;’             or ‘&lt;code class="literal"&gt;;&lt;/code&gt;’. A             ‘&lt;code class="literal"&gt;#&lt;/code&gt;’ comment can start in the             middle of a line as well.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;code class="literal"&gt;[&lt;em class="replaceable"&gt;&lt;code&gt;group&lt;/code&gt;&lt;/em&gt;]&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             &lt;em class="replaceable"&gt;&lt;code&gt;group&lt;/code&gt;&lt;/em&gt; is the name of the program             or group for which you want to set options. After a group             line, any option-setting lines apply to the named group             until the end of the option file or another group line is             given.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;code class="literal"&gt;&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             This is equivalent to             &lt;code class="option"&gt;--&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt; on             the command line.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;code class="literal"&gt;&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;=&lt;em class="replaceable"&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             This is equivalent to             &lt;code class="option"&gt;--&lt;em class="replaceable"&gt;&lt;code&gt;opt_name&lt;/code&gt;&lt;/em&gt;=&lt;em class="replaceable"&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;             on the command line. In an option file, you can have spaces             around the ‘&lt;code class="literal"&gt;=&lt;/code&gt;’ character,             something that is not true on the command line. You can             enclose the value within single quotes or double quotes,             which is useful if the value contains a             ‘&lt;code class="literal"&gt;#&lt;/code&gt;’ comment character or             whitespace.           &lt;/p&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         For options that take a numeric value, the value can be given         with a suffix of &lt;code class="literal"&gt;K&lt;/code&gt;, &lt;code class="literal"&gt;M&lt;/code&gt;, or         &lt;code class="literal"&gt;G&lt;/code&gt; (either uppercase or lowercase) to indicate         a multiplier of 1024, 1024&lt;sup&gt;2&lt;/sup&gt; or         1024&lt;sup&gt;3&lt;/sup&gt;. For example, the following         command tells &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; to ping the server         1024 times, sleeping 10 seconds between each ping:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysqladmin --count=1K --sleep=10 ping&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Leading and trailing blanks are automatically deleted from         option names and values. You may use the escape sequences         ‘&lt;code class="literal"&gt;\b&lt;/code&gt;’,         ‘&lt;code class="literal"&gt;\t&lt;/code&gt;’,         ‘&lt;code class="literal"&gt;\n&lt;/code&gt;’,         ‘&lt;code class="literal"&gt;\r&lt;/code&gt;’,         ‘&lt;code class="literal"&gt;\\&lt;/code&gt;’, and         ‘&lt;code class="literal"&gt;\s&lt;/code&gt;’ in option values to         represent the backspace, tab, newline, carriage return,         backslash, and space characters.       &lt;/p&gt; &lt;p&gt;         Because the ‘&lt;code class="literal"&gt;\\&lt;/code&gt;’ escape sequence         represents a single backslash, you must write each         ‘&lt;code class="literal"&gt;\&lt;/code&gt;’ as         ‘&lt;code class="literal"&gt;\\&lt;/code&gt;’. Alternatively, you can         specify the value using ‘&lt;code class="literal"&gt;/&lt;/code&gt;’         rather than ‘&lt;code class="literal"&gt;\&lt;/code&gt;’ as the pathname         separator.       &lt;/p&gt; &lt;p&gt;         If an option group name is the same as a program name, options         in the group apply specifically to that program. For example,         the &lt;code class="literal"&gt;[mysqld]&lt;/code&gt; and &lt;code class="literal"&gt;[mysql]&lt;/code&gt;         groups apply to the &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; server and the         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; client program, respectively.       &lt;/p&gt; &lt;p&gt;         The &lt;code class="literal"&gt;[client]&lt;/code&gt; option group is read by all         client programs (but &lt;span class="emphasis"&gt;&lt;em&gt;not&lt;/em&gt;&lt;/span&gt; by         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;). This allows you to specify options         that apply to all clients. For example,         &lt;code class="literal"&gt;[client]&lt;/code&gt; is the perfect group to use to         specify the password that you use to connect to the server. (But         make sure that the option file is readable and writable only by         yourself, so that other people cannot find out your password.)         Be sure not to put an option in the &lt;code class="literal"&gt;[client]&lt;/code&gt;         group unless it is recognized by &lt;span class="emphasis"&gt;&lt;em&gt;all&lt;/em&gt;&lt;/span&gt; client         programs that you use. Programs that do not understand the         option quit after displaying an error message if you try to run         them.       &lt;/p&gt; &lt;p&gt;         Here is a typical global option file:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[client]&lt;br /&gt;port=3306&lt;br /&gt;socket=/tmp/mysql.sock&lt;br /&gt;&lt;br /&gt;[mysqld]&lt;br /&gt;port=3306&lt;br /&gt;socket=/tmp/mysql.sock&lt;br /&gt;key_buffer_size=16M&lt;br /&gt;max_allowed_packet=8M&lt;br /&gt;&lt;br /&gt;[mysqldump]&lt;br /&gt;quick&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The preceding option file uses         &lt;code class="literal"&gt;&lt;em class="replaceable"&gt;&lt;code&gt;var_name&lt;/code&gt;&lt;/em&gt;=&lt;em class="replaceable"&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;         syntax for the lines that set the         &lt;code class="literal"&gt;key_buffer_size&lt;/code&gt; and         &lt;code class="literal"&gt;max_allowed_packet&lt;/code&gt; variables.       &lt;/p&gt; &lt;p&gt;         Here is a typical user option file:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[client]&lt;br /&gt;# The following password will be sent to all standard MySQL clients&lt;br /&gt;password="my_password"&lt;br /&gt;&lt;br /&gt;[mysql]&lt;br /&gt;no-auto-rehash&lt;br /&gt;connect_timeout=2&lt;br /&gt;&lt;br /&gt;[mysqlhotcopy]&lt;br /&gt;interactive-timeout&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         If you want to create option groups that should be read by         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; servers from a specific MySQL release         series only, you can do this by using groups with names of         &lt;code class="literal"&gt;[mysqld-4.1]&lt;/code&gt;,         &lt;code class="literal"&gt;[mysqld-5.0]&lt;/code&gt;, and so forth. The         following group indicates that the &lt;code class="option"&gt;--new&lt;/code&gt; option         should be used only by MySQL servers with 5.0.x         version numbers:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysqld-5.0]&lt;br /&gt;new&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Beginning with MySQL 5.0.4, it is possible to use         &lt;code class="literal"&gt;!include&lt;/code&gt; directives in option files to         include other option files and &lt;code class="literal"&gt;!includedir&lt;/code&gt; to         search specific directories for option files. For example, to         include the &lt;code class="filename"&gt;/home/mydir/myopt.cnf&lt;/code&gt; file, use         the following directive:       &lt;/p&gt; &lt;pre class="programlisting"&gt;!include /home/mydir/myopt.cnf&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         To search the &lt;code class="filename"&gt;/home/mydir&lt;/code&gt; directory and         read option files found there, use this directive:       &lt;/p&gt; &lt;pre class="programlisting"&gt;!includedir /home/mydir&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         There is no guarantee about the order in which the option files         in the directory will be read.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: Currently, any files to         be found and included using the &lt;code class="literal"&gt;!includedir&lt;/code&gt;         directive on Unix operating systems &lt;span class="emphasis"&gt;&lt;em&gt;must&lt;/em&gt;&lt;/span&gt;         have filenames ending in &lt;code class="filename"&gt;.cnf&lt;/code&gt;. On Windows,         this directive checks for files with the         &lt;code class="filename"&gt;.ini&lt;/code&gt; or &lt;code class="filename"&gt;.cnf&lt;/code&gt;         extension.       &lt;/p&gt; &lt;p&gt;         Write the contents of an included option file like any other         option file. That is, it should contain groups of options, each         preceded by a         &lt;code class="literal"&gt;[&lt;em class="replaceable"&gt;&lt;code&gt;group&lt;/code&gt;&lt;/em&gt;]&lt;/code&gt; line that         indicates the program to which the options apply.       &lt;/p&gt; &lt;p&gt;         While an included file is being processed, only those options in         groups that the current program is looking for are used. Other         groups are ignored. Suppose that a &lt;code class="filename"&gt;my.cnf&lt;/code&gt;         file contains this line:       &lt;/p&gt; &lt;pre class="programlisting"&gt;!include /home/mydir/myopt.cnf&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         And suppose that &lt;code class="filename"&gt;/home/mydir/myopt.cnf&lt;/code&gt;         looks like this:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysqladmin]&lt;br /&gt;force&lt;br /&gt;&lt;br /&gt;[mysqld]&lt;br /&gt;key_buffer_size=16M&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         If &lt;code class="filename"&gt;my.cnf&lt;/code&gt; is processed by         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;, only the &lt;code class="literal"&gt;[mysqld]&lt;/code&gt;         group in &lt;code class="filename"&gt;/home/mydir/myopt.cnf&lt;/code&gt; is used. If         the file is processed by &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt;, only the         &lt;code class="literal"&gt;[mysqldamin]&lt;/code&gt; group is used. If the file is         processed by any other program, no options in         &lt;code class="filename"&gt;/home/mydir/myopt.cnf&lt;/code&gt; are used.       &lt;/p&gt; &lt;p&gt;         The &lt;code class="literal"&gt;!includedir&lt;/code&gt; directive is processed         similarly except that all option files in the named directory         are read.       &lt;/p&gt; &lt;p&gt;         If you have a source distribution, you can find sample option         files named         &lt;code class="filename"&gt;my-&lt;em class="replaceable"&gt;&lt;code&gt;xxxx&lt;/code&gt;&lt;/em&gt;.cnf&lt;/code&gt; in         the &lt;code class="filename"&gt;support-files&lt;/code&gt; directory. If you have a         binary distribution, look in the         &lt;code class="filename"&gt;support-files&lt;/code&gt; directory under your MySQL         installation directory. On Windows, the sample option files may         be located in the MySQL installation directory (see earlier in         this section or &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/installing.html" title="Chapter 2. Installing and Upgrading MySQL"&gt;Chapter 2, &lt;i&gt;Installing and Upgrading MySQL&lt;/i&gt;&lt;/a&gt;, if you do not know         where this is). Currently, there are sample option files for         small, medium, large, and very large systems. To experiment with         one of these files, copy it to &lt;code class="filename"&gt;C:\my.cnf&lt;/code&gt; on         Windows or to &lt;code class="filename"&gt;.my.cnf&lt;/code&gt; in your home         directory on Unix.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: On Windows, the         &lt;code class="filename"&gt;.cnf&lt;/code&gt; or &lt;code class="filename"&gt;.ini&lt;/code&gt; option         file extension might not be displayed.       &lt;/p&gt; &lt;p&gt;         Most MySQL programs that support option files handle the         following options. They affect option-file handling, so they         must be given on the command line and not in an option file. To         work properly, each of these options must immediately follow the         command name, with the exception that         &lt;code class="option"&gt;--print-defaults&lt;/code&gt; may be used immediately after         &lt;code class="option"&gt;--defaults-file&lt;/code&gt; or         &lt;code class="option"&gt;--defaults-extra-file&lt;/code&gt;. Also, you should avoid         the use of the ‘&lt;code class="literal"&gt;~&lt;/code&gt;’ shell         metacharacter when specifying filenames because it might not be         interpreted as you expect.       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;             &lt;a class="indexterm" name="id2635950"&gt;&lt;/a&gt;              &lt;code class="option"&gt;--no-defaults&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             Don't read any option files.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;a class="indexterm" name="id2635971"&gt;&lt;/a&gt;              &lt;code class="option"&gt;--print-defaults&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             Print the program name and all options that it gets from             option files.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;a class="indexterm" name="id2635993"&gt;&lt;/a&gt;              &lt;code class="option"&gt;--defaults-file=&lt;em class="replaceable"&gt;&lt;code&gt;file_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             Use only the given option file.             &lt;em class="replaceable"&gt;&lt;code&gt;file_name&lt;/code&gt;&lt;/em&gt; is the full pathname to             the file. If the file does not exist or is otherwise             inaccessible, the program will exit with an error.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;a class="indexterm" name="id2636022"&gt;&lt;/a&gt;              &lt;code class="option"&gt;--defaults-extra-file=&lt;em class="replaceable"&gt;&lt;code&gt;file_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             Read this option file after the global option file but (on             Unix) before the user option file.             &lt;em class="replaceable"&gt;&lt;code&gt;file_name&lt;/code&gt;&lt;/em&gt; is the full pathname to             the file. As of MySQL 5.0.6, if the file does not exist or             is otherwise inaccessible, the program will exit with an             error.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             &lt;a class="indexterm" name="id2636054"&gt;&lt;/a&gt;              &lt;code class="option"&gt;--defaults-group-suffix=&lt;em class="replaceable"&gt;&lt;code&gt;str&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             If this option is given, the program reads not only its             usual option groups, but also groups with the usual names             and a suffix of &lt;em class="replaceable"&gt;&lt;code&gt;str&lt;/code&gt;&lt;/em&gt;. For example,             the &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; client normally reads the             &lt;code class="literal"&gt;[client]&lt;/code&gt; and &lt;code class="literal"&gt;[mysql]&lt;/code&gt;             groups. If the             &lt;code class="option"&gt;--default-group-suffix=_other&lt;/code&gt; option is             given, &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; also reads the             &lt;code class="literal"&gt;[client_other]&lt;/code&gt; and             &lt;code class="literal"&gt;[mysql_other]&lt;/code&gt; groups. This option was             added in MySQL 5.0.10.           &lt;/p&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         In shell scripts, you can use the         &lt;span&gt;&lt;strong class="command"&gt;my_print_defaults&lt;/strong&gt;&lt;/span&gt; program to parse option         files and see what options would be used by a given program. The         following example shows the output that         &lt;span&gt;&lt;strong class="command"&gt;my_print_defaults&lt;/strong&gt;&lt;/span&gt; might produce when asked to         show the options found in the &lt;code class="literal"&gt;[client]&lt;/code&gt; and         &lt;code class="literal"&gt;[mysql]&lt;/code&gt; groups:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;my_print_defaults client mysql&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;--port=3306&lt;br /&gt;--socket=/tmp/mysql.sock&lt;br /&gt;--no-auto-rehash&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note for developers&lt;/strong&gt;&lt;/span&gt;: Option         file handling is implemented in the C client library simply by         processing all options in the appropriate group or groups before         any command-line arguments. This works well for programs that         use the last instance of an option that is specified multiple         times. If you have a C or C++ program that handles multiply         specified options this way but that doesn't read option files,         you need add only two lines to give it that capability. Check         the source code of any of the standard MySQL clients to see how         to do this.       &lt;/p&gt; &lt;p&gt;         Several other language interfaces to MySQL are based on the C         client library, and some of them provide a way to access option         file contents. These include Perl and Python. For details, see         the documentation for your preferred interface.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;4.3.2.1. Preconfigured Option Files&lt;/h4&gt; &lt;p&gt;           MySQL provides a number of preconfigured option files that can           be used as a basis for tuning the MySQL server. Look in your           installation directory for files such as           &lt;code class="filename"&gt;my-small.cnf&lt;/code&gt;,           &lt;code class="filename"&gt;my-medium.cnf&lt;/code&gt;,           &lt;code class="filename"&gt;my-large.cnf&lt;/code&gt;, and           &lt;code class="filename"&gt;my-huge.cnf&lt;/code&gt;, which you can rename and           copy to the appropriate location for use as a base           configuration file. Regarding names and appropriate location,           see the general information provided in           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/option-files.html" title="4.3.2. Using Option Files"&gt;Section 4.3.2, “Using Option Files”&lt;/a&gt;. On Windows, those files have a           &lt;code class="filename"&gt;.ini&lt;/code&gt; rather than a           &lt;code class="filename"&gt;.cnf&lt;/code&gt; extension.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;4.3.3. Using Environment Variables to Specify Options&lt;/h3&gt; &lt;a class="indexterm" name="id2636235"&gt;&lt;/a&gt;&lt;p&gt;         To specify an option using an environment variable, set the         variable using the syntax appropriate for your command         processor. For example, on Windows or NetWare, you can set the         &lt;code class="literal"&gt;USER&lt;/code&gt; variable to specify your MySQL account         name. To do so, use this syntax:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SET USER=&lt;em class="replaceable"&gt;&lt;code&gt;your_name&lt;/code&gt;&lt;/em&gt; &lt;/pre&gt; &lt;p&gt;         The syntax on Unix depends on your shell. Suppose that you want         to specify the TCP/IP port number using the         &lt;code class="literal"&gt;MYSQL_TCP_PORT&lt;/code&gt; variable. Typical syntax (such         as for &lt;span&gt;&lt;strong class="command"&gt;sh&lt;/strong&gt;&lt;/span&gt;, &lt;code class="literal"&gt;bash&lt;/code&gt;,         &lt;span&gt;&lt;strong class="command"&gt;zsh&lt;/strong&gt;&lt;/span&gt;, and so on) is as follows:       &lt;/p&gt; &lt;pre class="programlisting"&gt;MYSQL_TCP_PORT=3306&lt;br /&gt;export MYSQL_TCP_PORT&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The first command sets the variable, and the         &lt;code class="literal"&gt;export&lt;/code&gt; command exports the variable to the         shell environment so that its value becomes accessible to MySQL         and other processes.       &lt;/p&gt; &lt;p&gt;         For &lt;span&gt;&lt;strong class="command"&gt;csh&lt;/strong&gt;&lt;/span&gt; and &lt;span&gt;&lt;strong class="command"&gt;tcsh&lt;/strong&gt;&lt;/span&gt;, use         &lt;span&gt;&lt;strong class="command"&gt;setenv&lt;/strong&gt;&lt;/span&gt; to make the shell variable available         to the environment:       &lt;/p&gt; &lt;pre class="programlisting"&gt;setenv MYSQL_TCP_PORT 3306&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The commands to set environment variables can be executed at         your command prompt to take effect immediately, but the settings         persist only until you log out. To have the settings take effect         each time you log in, place the appropriate command or commands         in a startup file that your command interpreter reads each time         it starts. Typical startup files are         &lt;code class="filename"&gt;AUTOEXEC.BAT&lt;/code&gt; for Windows,         &lt;code class="filename"&gt;.bash_profile&lt;/code&gt; for &lt;span&gt;&lt;strong class="command"&gt;bash&lt;/strong&gt;&lt;/span&gt;,         or &lt;code class="filename"&gt;.tcshrc&lt;/code&gt; for &lt;span&gt;&lt;strong class="command"&gt;tcsh&lt;/strong&gt;&lt;/span&gt;.         Consult the documentation for your command interpreter for         specific details.       &lt;/p&gt; &lt;p&gt;         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/environment-variables.html" title="2.4.19. Environment Variables"&gt;Section 2.4.19, “Environment Variables”&lt;/a&gt;, lists all environment         variables that affect MySQL program operation.       &lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;4.3.4. Using Options to Set Program Variables&lt;/h3&gt; &lt;a class="indexterm" name="id2636366"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2636376"&gt;&lt;/a&gt;&lt;p&gt;         Many MySQL programs have internal variables that can be set at         runtime. Program variables are set the same way as any other         long option that takes a value. For example,         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; has a         &lt;code class="literal"&gt;max_allowed_packet&lt;/code&gt; variable that controls the         maximum size of its communication buffer. To set the         &lt;code class="literal"&gt;max_allowed_packet&lt;/code&gt; variable for         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to a value of 16MB, use either of the         following commands:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql --max_allowed_packet=16777216&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql --max_allowed_packet=16M&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         The first command specifies the value in bytes. The second         specifies the value in megabytes. For variables that take a         numeric value, the value can be given with a suffix of         &lt;code class="literal"&gt;K&lt;/code&gt;, &lt;code class="literal"&gt;M&lt;/code&gt;, or         &lt;code class="literal"&gt;G&lt;/code&gt; (either uppercase or lowercase) to indicate         a multiplier of 1024, 1024&lt;sup&gt;2&lt;/sup&gt; or         1024&lt;sup&gt;3&lt;/sup&gt;. (For example, when used to set         &lt;code class="literal"&gt;max_allowed_packet&lt;/code&gt;, the suffixes indicate         units of kilobytes, megabytes, or gigabytes.)       &lt;/p&gt; &lt;p&gt;         In an option file, variable settings are given without the         leading dashes:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysql]&lt;br /&gt;max_allowed_packet=16777216&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Or:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysql]&lt;br /&gt;max_allowed_packet=16M&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         If you like, underscores in a variable name can be specified as         dashes. The following option groups are equivalent. Both set the         size of the server's key buffer to 512MB:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysqld]&lt;br /&gt;key_buffer_size=512M&lt;br /&gt;&lt;br /&gt;[mysqld]&lt;br /&gt;key-buffer-size=512M&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: Before MySQL 4.0.2, the         only syntax for setting program variables was         &lt;code class="option"&gt;--set-variable=&lt;em class="replaceable"&gt;&lt;code&gt;option&lt;/code&gt;&lt;/em&gt;=&lt;em class="replaceable"&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;         (or         &lt;code class="option"&gt;set-variable=&lt;em class="replaceable"&gt;&lt;code&gt;option&lt;/code&gt;&lt;/em&gt;=&lt;em class="replaceable"&gt;&lt;code&gt;value&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;         in option files). This syntax still is recognized, but is         deprecated as of MySQL 4.0.2.       &lt;/p&gt; &lt;p&gt;         Many server system variables can also be set at runtime.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-3787700480061068711?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/3787700480061068711/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=3787700480061068711' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3787700480061068711'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3787700480061068711'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/chapter-4-using-mysql-programs.html' title='Chapter 4. Using MySQL Programs'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-4728029913740355781</id><published>2007-04-09T22:31:00.001-07:00</published><updated>2007-04-09T22:31:43.346-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.8. Using MySQL with Apache</title><content type='html'>&lt;h2 class="title"&gt;&lt;span style="font-size:100%;"&gt;There are programs that let you authenticate your users from a       MySQL database and also let you write your log files into a MySQL       table.&lt;/span&gt;      &lt;/h2&gt;&lt;p&gt;       You can change the Apache logging format to be easily readable by       MySQL by putting the following into the Apache configuration file:     &lt;/p&gt; &lt;pre class="programlisting"&gt;LogFormat \&lt;br /&gt;       "\"%h\",%{%Y%m%d%H%M%S}t,%&gt;s,\"%b\",\"%{Content-Type}o\",  \&lt;br /&gt;       \"%U\",\"%{Referer}i\",\"%{User-Agent}i\""&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       To load a log file in that format into MySQL, you can use a       statement something like this:     &lt;/p&gt; &lt;pre class="programlisting"&gt;LOAD DATA INFILE '&lt;em class="replaceable"&gt;&lt;code&gt;/local/access_log&lt;/code&gt;&lt;/em&gt;' INTO TABLE &lt;em class="replaceable"&gt;&lt;code&gt;tbl_name&lt;/code&gt;&lt;/em&gt;&lt;br /&gt;FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The named table should be created to have columns that correspond       to those that the &lt;code class="literal"&gt;LogFormat&lt;/code&gt; line writes to the       log file.     &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-4728029913740355781?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/4728029913740355781/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=4728029913740355781' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4728029913740355781'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4728029913740355781'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/38-using-mysql-with-apache.html' title='3.8. Using MySQL with Apache'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-684652525980116063</id><published>2007-04-09T22:13:00.000-07:00</published><updated>2007-04-09T22:29:47.396-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.7. Queries from the Twin Project</title><content type='html'>&lt;p&gt;       At Analytikerna and Lentus, we have been doing the systems and       field work for a big research project. This project is a       collaboration between the Institute of Environmental Medicine at       Karolinska Institutet Stockholm and the Section on Clinical       Research in Aging and Psychology at the University of Southern       California.     &lt;/p&gt; &lt;p&gt;       The project involves a screening part where all twins in Sweden       older than 65 years are interviewed by telephone. Twins who meet       certain criteria are passed on to the next stage. In this latter       stage, twins who want to participate are visited by a doctor/nurse       team. Some of the examinations include physical and       neuropsychological examination, laboratory testing, neuroimaging,       psychological status assessment, and family history collection. In       addition, data are collected on medical and environmental risk       factors.     &lt;/p&gt; &lt;p&gt;       More information about Twin studies can be found at:       &lt;a href="http://www.mep.ki.se/twinreg/index_en.html" target="_top"&gt;http://www.mep.ki.se/twinreg/index_en.html&lt;/a&gt;     &lt;/p&gt; &lt;p&gt;       The latter part of the project is administered with a Web       interface written using Perl and MySQL.     &lt;/p&gt; &lt;p&gt;       Each night all data from the interviews is moved into a MySQL       database.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.7.1. Find All Non-distributed Twins&lt;/h3&gt; &lt;p&gt;         The following query is used to determine who goes into the         second part of the project:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT&lt;br /&gt;   CONCAT(p1.id, p1.tvab) + 0 AS tvid,&lt;br /&gt;   CONCAT(p1.christian_name, ' ', p1.surname) AS Name,&lt;br /&gt;   p1.postal_code AS Code,&lt;br /&gt;   p1.city AS City,&lt;br /&gt;   pg.abrev AS Area,&lt;br /&gt;   IF(td.participation = 'Aborted', 'A', ' ') AS A,&lt;br /&gt;   p1.dead AS dead1,&lt;br /&gt;   l.event AS event1,&lt;br /&gt;   td.suspect AS tsuspect1,&lt;br /&gt;   id.suspect AS isuspect1,&lt;br /&gt;   td.severe AS tsevere1,&lt;br /&gt;   id.severe AS isevere1,&lt;br /&gt;   p2.dead AS dead2,&lt;br /&gt;   l2.event AS event2,&lt;br /&gt;   h2.nurse AS nurse2,&lt;br /&gt;   h2.doctor AS doctor2,&lt;br /&gt;   td2.suspect AS tsuspect2,&lt;br /&gt;   id2.suspect AS isuspect2,&lt;br /&gt;   td2.severe AS tsevere2,&lt;br /&gt;   id2.severe AS isevere2,&lt;br /&gt;   l.finish_date&lt;br /&gt;FROM&lt;br /&gt;   twin_project AS tp&lt;br /&gt;   /* For Twin 1 */&lt;br /&gt;   LEFT JOIN twin_data AS td ON tp.id = td.id&lt;br /&gt;             AND tp.tvab = td.tvab&lt;br /&gt;   LEFT JOIN informant_data AS id ON tp.id = id.id&lt;br /&gt;             AND tp.tvab = id.tvab&lt;br /&gt;   LEFT JOIN harmony AS h ON tp.id = h.id&lt;br /&gt;             AND tp.tvab = h.tvab&lt;br /&gt;   LEFT JOIN lentus AS l ON tp.id = l.id&lt;br /&gt;             AND tp.tvab = l.tvab&lt;br /&gt;   /* For Twin 2 */&lt;br /&gt;   LEFT JOIN twin_data AS td2 ON p2.id = td2.id&lt;br /&gt;             AND p2.tvab = td2.tvab&lt;br /&gt;   LEFT JOIN informant_data AS id2 ON p2.id = id2.id&lt;br /&gt;             AND p2.tvab = id2.tvab&lt;br /&gt;   LEFT JOIN harmony AS h2 ON p2.id = h2.id&lt;br /&gt;             AND p2.tvab = h2.tvab&lt;br /&gt;   LEFT JOIN lentus AS l2 ON p2.id = l2.id&lt;br /&gt;             AND p2.tvab = l2.tvab,&lt;br /&gt;   person_data AS p1,&lt;br /&gt;   person_data AS p2,&lt;br /&gt;   postal_groups AS pg&lt;br /&gt;WHERE&lt;br /&gt;   /* p1 gets main twin and p2 gets his/her twin. */&lt;br /&gt;   /* ptvab is a field inverted from tvab */&lt;br /&gt;   p1.id = tp.id AND p1.tvab = tp.tvab AND&lt;br /&gt;   p2.id = p1.id AND p2.ptvab = p1.tvab AND&lt;br /&gt;   /* Just the screening survey */&lt;br /&gt;   tp.survey_no = 5 AND&lt;br /&gt;   /* Skip if partner died before 65 but allow emigration (dead=9) */&lt;br /&gt;   (p2.dead = 0 OR p2.dead = 9 OR&lt;br /&gt;    (p2.dead = 1 AND&lt;br /&gt;     (p2.death_date = 0 OR&lt;br /&gt;      (((TO_DAYS(p2.death_date) - TO_DAYS(p2.birthday)) / 365)&lt;br /&gt;       &gt;= 65))))&lt;br /&gt;   AND&lt;br /&gt;   (&lt;br /&gt;   /* Twin is suspect */&lt;br /&gt;   (td.future_contact = 'Yes' AND td.suspect = 2) OR&lt;br /&gt;   /* Twin is suspect - Informant is Blessed */&lt;br /&gt;   (td.future_contact = 'Yes' AND td.suspect = 1&lt;br /&gt;                              AND id.suspect = 1) OR&lt;br /&gt;   /* No twin - Informant is Blessed */&lt;br /&gt;   (ISNULL(td.suspect) AND id.suspect = 1&lt;br /&gt;                       AND id.future_contact = 'Yes') OR&lt;br /&gt;   /* Twin broken off - Informant is Blessed */&lt;br /&gt;   (td.participation = 'Aborted'&lt;br /&gt;    AND id.suspect = 1 AND id.future_contact = 'Yes') OR&lt;br /&gt;   /* Twin broken off - No inform - Have partner */&lt;br /&gt;   (td.participation = 'Aborted' AND ISNULL(id.suspect)&lt;br /&gt;                                 AND p2.dead = 0))&lt;br /&gt;   AND&lt;br /&gt;   l.event = 'Finished'&lt;br /&gt;   /* Get at area code */&lt;br /&gt;   AND SUBSTRING(p1.postal_code, 1, 2) = pg.code&lt;br /&gt;   /* Not already distributed */&lt;br /&gt;   AND (h.nurse IS NULL OR h.nurse=00 OR h.doctor=00)&lt;br /&gt;   /* Has not refused or been aborted */&lt;br /&gt;   AND NOT (h.status = 'Refused' OR h.status = 'Aborted'&lt;br /&gt;   OR h.status = 'Died' OR h.status = 'Other')&lt;br /&gt;ORDER BY&lt;br /&gt;   tvid;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Some explanations:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;             &lt;code class="literal"&gt;CONCAT(p1.id, p1.tvab) + 0 AS tvid&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             We want to sort on the concatenated &lt;code class="literal"&gt;id&lt;/code&gt;             and &lt;code class="literal"&gt;tvab&lt;/code&gt; in numerical order. Adding             &lt;code class="literal"&gt;0&lt;/code&gt; to the result causes MySQL to treat the             result as a number.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             column &lt;code class="literal"&gt;id&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             This identifies a pair of twins. It is a key in all tables.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             column &lt;code class="literal"&gt;tvab&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             This identifies a twin in a pair. It has a value of             &lt;code class="literal"&gt;1&lt;/code&gt; or &lt;code class="literal"&gt;2&lt;/code&gt;.           &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;             column &lt;code class="literal"&gt;ptvab&lt;/code&gt;           &lt;/p&gt; &lt;p&gt;             This is an inverse of &lt;code class="literal"&gt;tvab&lt;/code&gt;. When             &lt;code class="literal"&gt;tvab&lt;/code&gt; is &lt;code class="literal"&gt;1&lt;/code&gt; this is             &lt;code class="literal"&gt;2&lt;/code&gt;, and vice versa. It exists to save             typing and to make it easier for MySQL to optimize the             query.           &lt;/p&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         This query demonstrates, among other things, how to do lookups         on a table from the same table with a join         (&lt;code class="literal"&gt;p1&lt;/code&gt; and &lt;code class="literal"&gt;p2&lt;/code&gt;). In the         example, this is used to check whether a twin's partner died         before the age of 65. If so, the row is not returned.       &lt;/p&gt; &lt;p&gt;         All of the above exist in all tables with twin-related         information. We have a key on both &lt;code class="literal"&gt;id,tvab&lt;/code&gt;         (all tables), and &lt;code class="literal"&gt;id,ptvab&lt;/code&gt;         (&lt;code class="literal"&gt;person_data&lt;/code&gt;) to make queries faster.       &lt;/p&gt; &lt;p&gt;         On our production machine (A 200MHz UltraSPARC), this query         returns about 150-200 rows and takes less than one second.       &lt;/p&gt; &lt;p&gt;         The current number of records in the tables used in the query:       &lt;/p&gt; &lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Table&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Rows&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;person_data&lt;/code&gt;&lt;/td&gt; &lt;td&gt;71074&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;lentus&lt;/code&gt;&lt;/td&gt; &lt;td&gt;5291&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;twin_project&lt;/code&gt;&lt;/td&gt; &lt;td&gt;5286&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;twin_data&lt;/code&gt;&lt;/td&gt; &lt;td&gt;2012&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;informant_data&lt;/code&gt;&lt;/td&gt; &lt;td&gt;663&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;harmony&lt;/code&gt;&lt;/td&gt; &lt;td&gt;381&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;postal_groups&lt;/code&gt;&lt;/td&gt; &lt;td&gt;100&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.7.2. Show a Table of Twin Pair Status&lt;/h3&gt; &lt;p&gt;         Each interview ends with a status code called         &lt;code class="literal"&gt;event&lt;/code&gt;. The query shown here is used to         display a table over all twin pairs combined by event. This         indicates in how many pairs both twins are finished, in how many         pairs one twin is finished and the other refused, and so on.       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT&lt;br /&gt;       t1.event,&lt;br /&gt;       t2.event,&lt;br /&gt;       COUNT(*)&lt;br /&gt;FROM&lt;br /&gt;       lentus AS t1,&lt;br /&gt;       lentus AS t2,&lt;br /&gt;       twin_project AS tp&lt;br /&gt;WHERE&lt;br /&gt;       /* We are looking at one pair at a time */&lt;br /&gt;       t1.id = tp.id&lt;br /&gt;       AND t1.tvab=tp.tvab&lt;br /&gt;       AND t1.id = t2.id&lt;br /&gt;       /* Just the screening survey */&lt;br /&gt;       AND tp.survey_no = 5&lt;br /&gt;       /* This makes each pair only appear once */&lt;br /&gt;       AND t1.tvab='1' AND t2.tvab='2'&lt;br /&gt;GROUP BY&lt;br /&gt;       t1.event, t2.event;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-684652525980116063?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/684652525980116063/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=684652525980116063' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/684652525980116063'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/684652525980116063'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/37-queries-from-twin-project.html' title='3.7. Queries from the Twin Project'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-5649643605336333680</id><published>2007-04-03T22:53:00.000-07:00</published><updated>2007-04-03T23:09:02.530-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.6. Examples of Common Queries</title><content type='html'>&lt;p&gt;       Here are examples of how to solve some common problems with MySQL.     &lt;/p&gt; &lt;p&gt;       Some of the examples use the table &lt;code class="literal"&gt;shop&lt;/code&gt; to hold       the price of each article (item number) for certain traders       (dealers). Supposing that each trader has a single fixed price per       article, then (&lt;code class="literal"&gt;article&lt;/code&gt;,       &lt;code class="literal"&gt;dealer&lt;/code&gt;) is a primary key for the records.     &lt;/p&gt; &lt;p&gt;       Start the command-line tool &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; and select a       database:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql &lt;em class="replaceable"&gt;&lt;code&gt;your-database-name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       (In most MySQL installations, you can use the database named       &lt;code class="literal"&gt;test&lt;/code&gt;).     &lt;/p&gt; &lt;p&gt;       You can create and populate the example table with these       statements:     &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE shop (&lt;br /&gt; article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,&lt;br /&gt; dealer  CHAR(20)                 DEFAULT ''     NOT NULL,&lt;br /&gt; price   DOUBLE(16,2)             DEFAULT '0.00' NOT NULL,&lt;br /&gt; PRIMARY KEY(article, dealer));&lt;br /&gt;INSERT INTO shop VALUES&lt;br /&gt; (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),&lt;br /&gt; (3,'C',1.69),(3,'D',1.25),(4,'D',19.95);&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       After issuing the statements, the table should have the following       contents:     &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT * FROM shop;&lt;br /&gt;&lt;br /&gt;+---------+--------+-------+&lt;br /&gt;| article | dealer | price |&lt;br /&gt;+---------+--------+-------+&lt;br /&gt;|    0001 | A      |  3.45 |&lt;br /&gt;|    0001 | B      |  3.99 |&lt;br /&gt;|    0002 | A      | 10.99 |&lt;br /&gt;|    0003 | B      |  1.45 |&lt;br /&gt;|    0003 | C      |  1.69 |&lt;br /&gt;|    0003 | D      |  1.25 |&lt;br /&gt;|    0004 | D      | 19.95 |&lt;br /&gt;+---------+--------+-------+&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h3 class="title"&gt;3.6.1. The Maximum Value for a Column&lt;/h3&gt; &lt;p&gt;         “&lt;span class="quote"&gt;What's the highest item number?&lt;/span&gt;”       &lt;/p&gt; SELECT MAX(article) AS article FROM shop;&lt;br /&gt;+---------+ | article | +---------+ |       4 | +---------+&lt;br /&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.6.2. The Row Holding the Maximum of a Certain Column&lt;/h3&gt; &lt;p&gt;         &lt;span class="emphasis"&gt;&lt;em&gt;Task: Find the number, dealer, and price of the most         expensive article.&lt;/em&gt;&lt;/span&gt;       &lt;/p&gt; &lt;p&gt;         This is easily done with a subquery:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT article, dealer, price&lt;br /&gt;FROM   shop&lt;br /&gt;WHERE  price=(SELECT MAX(price) FROM shop);&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Another solution is to sort all rows descending by price and get         only the first row using the MySQL-specific         &lt;code class="literal"&gt;LIMIT&lt;/code&gt; clause:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT article, dealer, price&lt;br /&gt;FROM shop&lt;br /&gt;ORDER BY price DESC&lt;br /&gt;LIMIT 1;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: If there were several         most expensive articles, each with a price of 19.95, the         &lt;code class="literal"&gt;LIMIT&lt;/code&gt; solution would show only one of them.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.6.3. Maximum of Column per Group&lt;/h3&gt; &lt;p&gt;         &lt;span class="emphasis"&gt;&lt;em&gt;Task: Find the highest price per article.&lt;/em&gt;&lt;/span&gt;       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT article, MAX(price) AS price&lt;br /&gt;FROM   shop&lt;br /&gt;GROUP BY article&lt;br /&gt;&lt;br /&gt;+---------+-------+&lt;br /&gt;| article | price |&lt;br /&gt;+---------+-------+&lt;br /&gt;|    0001 |  3.99 |&lt;br /&gt;|    0002 | 10.99 |&lt;br /&gt;|    0003 |  1.69 |&lt;br /&gt;|    0004 | 19.95 |&lt;br /&gt;+---------+-------+&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h3 class="title"&gt;3.6.4. The Rows Holding the Group-wise Maximum of a Certain Field&lt;/h3&gt; &lt;p&gt;         &lt;span class="emphasis"&gt;&lt;em&gt;Task: For each article, find the dealer or dealers         with the most expensive price.&lt;/em&gt;&lt;/span&gt;       &lt;/p&gt; &lt;p&gt;         This problem can be solved with a subquery like this one:       &lt;/p&gt; SELECT article, dealer, price FROM   shop s1 WHERE  price=(SELECT MAX(s2.price)               FROM shop s2               WHERE s1.article = s2.article);  +---------+--------+-------+ | article | dealer | price | +---------+--------+-------+ |    0001 | B      |  3.99 |  |    0002 | A      | 10.99 |  |    0003 | C      |  1.69 |  |    0004 | D      | 19.95 |  +---------+--------+-------+  &lt;p&gt;         The preceding example uses a correlated subquery, which can be         inefficient (see &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/correlated-subqueries.html" title="13.2.8.7. Correlated Subqueries"&gt;Section 13.2.8.7, “Correlated Subqueries”&lt;/a&gt;). Other         possibilities for solving the problem are to use a         non-correlated subquery in the &lt;code class="literal"&gt;FROM&lt;/code&gt; clause or         a &lt;code class="literal"&gt;LEFT JOIN&lt;/code&gt;:       &lt;/p&gt; SELECT s1.article, dealer, s1.price FROM shop s1 JOIN (   SELECT article, MAX(price) AS price   FROM shop   GROUP BY article) AS s2   ON s1.article = s2.article AND s1.price = s2.price;  SELECT s1.article, s1.dealer, s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price &lt; class="title"&gt;3.6.5. Using User-Defined Variables &lt;p&gt;         You can employ MySQL user variables to remember results without         having to store them in temporary variables in the client. (See         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/user-variables.html" title="9.4. User-Defined Variables"&gt;Section 9.4, “User-Defined Variables”&lt;/a&gt;.)       &lt;/p&gt; &lt;p&gt;         For example, to find the articles with the highest and lowest         price you can do this       &lt;/p&gt; &lt;!--UdmComment--&gt;   &lt;div id="container"&gt;&lt;div id="header"&gt;&lt;div id="logo"&gt;&lt;a href="http://www.mysql.com/"&gt;&lt;/a&gt;&lt;h3 class="title"&gt;3.6.9. Using &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;&lt;/h3&gt; &lt;a class="indexterm" name="id3080510"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080517"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080524"&gt;&lt;/a&gt;&lt;p&gt;         The &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; attribute can be used to         generate a unique identity for new rows:       &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE animals (&lt;br /&gt;   id MEDIUMINT NOT NULL AUTO_INCREMENT,&lt;br /&gt;   name CHAR(30) NOT NULL,&lt;br /&gt;   PRIMARY KEY (id)&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;INSERT INTO animals (name) VALUES&lt;br /&gt;  ('dog'),('cat'),('penguin'),&lt;br /&gt;  ('lax'),('whale'),('ostrich');&lt;br /&gt;&lt;br /&gt;SELECT * FROM animals;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Which returns:       &lt;/p&gt; &lt;pre class="programlisting"&gt;+----+---------+&lt;br /&gt;| id | name    |&lt;br /&gt;+----+---------+&lt;br /&gt;|  1 | dog     |&lt;br /&gt;|  2 | cat     |&lt;br /&gt;|  3 | penguin |&lt;br /&gt;|  4 | lax     |&lt;br /&gt;|  5 | whale   |&lt;br /&gt;|  6 | ostrich |&lt;br /&gt;+----+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         You can retrieve the most recent         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value with the         &lt;code class="literal"&gt;LAST_INSERT_ID()&lt;/code&gt; SQL function or the         &lt;code class="literal"&gt;mysql_insert_id()&lt;/code&gt; C API function. These         functions are connection-specific, so their return values are         not affected by another connection which is also performing         inserts.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: For a multiple-row         insert, &lt;code class="literal"&gt;LAST_INSERT_ID()&lt;/code&gt; and         &lt;code class="literal"&gt;mysql_insert_id()&lt;/code&gt; actually return the         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; key from the         &lt;span class="emphasis"&gt;&lt;em&gt;first&lt;/em&gt;&lt;/span&gt; of the inserted rows. This allows         multiple-row inserts to be reproduced correctly on other servers         in a replication setup.       &lt;/p&gt; &lt;p&gt;         For &lt;code class="literal"&gt;MyISAM&lt;/code&gt; and &lt;code class="literal"&gt;BDB&lt;/code&gt; tables         you can specify &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; on a secondary         column in a multiple-column index. In this case, the generated         value for the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is         calculated as         &lt;code class="literal"&gt;MAX(&lt;em class="replaceable"&gt;&lt;code&gt;auto_increment_column&lt;/code&gt;&lt;/em&gt;) +         1 WHERE         prefix=&lt;em class="replaceable"&gt;&lt;code&gt;given-prefix&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;. This         is useful when you want to put data into ordered groups.       &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE animals (&lt;br /&gt;  grp ENUM('fish','mammal','bird') NOT NULL,&lt;br /&gt;  id MEDIUMINT NOT NULL AUTO_INCREMENT,&lt;br /&gt;  name CHAR(30) NOT NULL,&lt;br /&gt;  PRIMARY KEY (grp,id)&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;INSERT INTO animals (grp,name) VALUES&lt;br /&gt;  ('mammal','dog'),('mammal','cat'),&lt;br /&gt;  ('bird','penguin'),('fish','lax'),('mammal','whale'),&lt;br /&gt;  ('bird','ostrich');&lt;br /&gt;&lt;br /&gt;SELECT * FROM animals ORDER BY grp,id;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Which returns:       &lt;/p&gt; &lt;pre class="programlisting"&gt;+--------+----+---------+&lt;br /&gt;| grp    | id | name    |&lt;br /&gt;+--------+----+---------+&lt;br /&gt;| fish   |  1 | lax     |&lt;br /&gt;| mammal |  1 | dog     |&lt;br /&gt;| mammal |  2 | cat     |&lt;br /&gt;| mammal |  3 | whale   |&lt;br /&gt;| bird   |  1 | penguin |&lt;br /&gt;| bird   |  2 | ostrich |&lt;br /&gt;+--------+----+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Note that in this case (when the         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is part of a         multiple-column index), &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; values         are reused if you delete the row with the biggest         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value in any group. This         happens even for &lt;code class="literal"&gt;MyISAM&lt;/code&gt; tables, for which         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; values normally are not         reused.       &lt;/p&gt; &lt;p&gt;         If the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is part of         multiple indexes, MySQL will generate sequence values using the         index that begins with the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;         column, if there is one. For example, if the         &lt;code class="literal"&gt;animals&lt;/code&gt; table contained indexes         &lt;code class="literal"&gt;PRIMARY KEY (grp, id)&lt;/code&gt; and &lt;code class="literal"&gt;INDEX         (id)&lt;/code&gt;, MySQL would ignore the &lt;code class="literal"&gt;PRIMARY         KEY&lt;/code&gt; for generating sequence values. As a result, the         table would contain a single sequence, not a sequence per         &lt;code class="literal"&gt;grp&lt;/code&gt; value.       &lt;/p&gt; &lt;p&gt;         To start with an &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value other         than 1, you can set that value with &lt;code class="literal"&gt;CREATE         TABLE&lt;/code&gt; or &lt;code class="literal"&gt;ALTER TABLE&lt;/code&gt;, like this:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;ALTER TABLE tbl AUTO_INCREMENT = 100;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         More information about &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; is         available here:          &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               How to assign the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;               attribute to a column: &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/create-table.html" title="13.1.5. CREATE TABLE Syntax"&gt;Section 13.1.5, “&lt;code class="literal"&gt;CREATE TABLE&lt;/code&gt; Syntax”&lt;/a&gt;, and               &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/alter-table.html" title="13.1.2. ALTER TABLE Syntax"&gt;Section 13.1.2, “&lt;code class="literal"&gt;ALTER TABLE&lt;/code&gt; Syntax”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               How &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; behaves depending on               the SQL mode: &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" title="5.2.6. SQL Modes"&gt;Section 5.2.6, “SQL Modes”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               Find the row that contains the most recent AUTO_INCREMENT               value: &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html" title="12.2.3. Comparison Functions and Operators"&gt;Section 12.2.3, “Comparison Functions and Operators”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               Set the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value to be               used: &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/set-option.html" title="13.5.3. SET Syntax"&gt;Section 13.5.3, “&lt;code class="literal"&gt;SET&lt;/code&gt; Syntax”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; and replication:               &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/replication-features.html" title="6.7. Replication Features and Known Problems"&gt;Section 6.7, “Replication Features and Known Problems”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               Server-system variables related to               &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;               (&lt;code class="literal"&gt;auto_increment_increment&lt;/code&gt; and               &lt;code class="literal"&gt;auto_increment_offset&lt;/code&gt;) that can be used               for replication:               &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html" title="5.2.3. System Variables"&gt;Section 5.2.3, “System Variables”&lt;/a&gt;.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       &lt;/p&gt;  &lt;!--UdmComment--&gt;&lt;div id="docnav"&gt;&lt;a rel="prev" href="http://dev.mysql.com/doc/refman/5.0/en/calculating-days.html" title="3.6.8 Calculating Visits Per Day"&gt;Previous&lt;/a&gt; / &lt;a rel="next" href="http://dev.mysql.com/doc/refman/5.0/en/twin.html" title="3.7 Queries from the Twin Project"&gt;Next&lt;/a&gt; / &lt;a rel="up" href="http://dev.mysql.com/doc/refman/5.0/en/examples.html" title="3.6 Examples of Common Queries"&gt;Up&lt;/a&gt; / &lt;a rel="contents" href="http://dev.mysql.com/doc/refman/5.0/en/index.html"&gt;Table of Contents&lt;/a&gt;&lt;/div&gt;&lt;!--/UdmComment--&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="page" class="sidebar"&gt;&lt;div class="section" lang="en"&gt;&lt;pre class="programlisting"&gt;&lt;br /&gt;*************************** 1. row ***************************&lt;br /&gt;Table: shirt&lt;br /&gt;Create Table: CREATE TABLE `shirt` (&lt;br /&gt;`id` smallint(5) unsigned NOT NULL auto_increment,&lt;br /&gt;`style` enum('t-shirt','polo','dress') NOT NULL,&lt;br /&gt;`color` enum('red','blue','orange','white','black') NOT NULL,&lt;br /&gt;`owner` smallint(5) unsigned NOT NULL,&lt;br /&gt;PRIMARY KEY  (`id`)&lt;br /&gt;) ENGINE=MyISAM DEFAULT CHARSET=latin1&lt;br /&gt;&lt;span style="font-family:Georgia,serif;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;h3 class="title"&gt;3.6.7. Searching on Two Keys&lt;/h3&gt; &lt;a class="indexterm" name="id3080309"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080315"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080322"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080332"&gt;&lt;/a&gt;&lt;p&gt;         An &lt;code class="literal"&gt;OR&lt;/code&gt; using a single key is well optimized,         as is the handling of &lt;code class="literal"&gt;AND&lt;/code&gt;.       &lt;/p&gt; &lt;p&gt;         The one tricky case is that of searching on two different keys         combined with &lt;code class="literal"&gt;OR&lt;/code&gt;:       &lt;/p&gt; SELECT field1_index, field2_index FROM test_table WHERE field1_index = '1' OR  field2_index = '1'  &lt;p&gt;         This case is optimized from MySQL 5.0.0. See         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html" title="7.2.6. Index Merge Optimization"&gt;Section 7.2.6, “Index Merge Optimization”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         You can also solve the problem efficiently by using a         &lt;code class="literal"&gt;UNION&lt;/code&gt; that combines the output of two         separate &lt;code class="literal"&gt;SELECT&lt;/code&gt; statements. See         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/union.html" title="13.2.7.3. UNION Syntax"&gt;Section 13.2.7.3, “&lt;code class="literal"&gt;UNION&lt;/code&gt; Syntax”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         Each &lt;code class="literal"&gt;SELECT&lt;/code&gt; searches only one key and can be         optimized:       &lt;/p&gt; SELECT field1_index, field2_index     FROM test_table WHERE field1_index = '1' UNION SELECT field1_index, field2_index     FROM test_table WHERE field2_index = '1';&lt;br /&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.6.8. Calculating Visits Per Day&lt;/h3&gt; &lt;a class="indexterm" name="id3080418"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080424"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080431"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080438"&gt;&lt;/a&gt;&lt;p&gt;         The following example shows how you can use the bit group         functions to calculate the number of days per month a user has         visited a Web page.       &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL,&lt;br /&gt;          day INT(2) UNSIGNED ZEROFILL);&lt;br /&gt;INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),&lt;br /&gt;         (2000,2,23),(2000,2,23);&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The example table contains year-month-day values representing         visits by users to the page. To determine how many different         days in each month these visits occur, use this query:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT year,month,BIT_COUNT(BIT_OR(1&lt;&lt;day)) as="" days="" from="" t1="" group="" by=""&gt;&lt;/day))&gt;&lt;/pre&gt; &lt;p&gt;         Which returns:       &lt;/p&gt; &lt;pre class="programlisting"&gt;+------+-------+------+&lt;br /&gt;| year | month | days |&lt;br /&gt;+------+-------+------+&lt;br /&gt;| 2000 |    01 |    3 |&lt;br /&gt;| 2000 |    02 |    2 |&lt;br /&gt;+------+-------+------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The query calculates how many different days appear in the table         for each year/month combination, with automatic removal of         duplicate entries.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.6.9. Using &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;&lt;/h3&gt; &lt;a class="indexterm" name="id3080510"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080517"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3080524"&gt;&lt;/a&gt;&lt;p&gt;         The &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; attribute can be used to         generate a unique identity for new rows:       &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE animals (&lt;br /&gt;  id MEDIUMINT NOT NULL AUTO_INCREMENT,&lt;br /&gt;  name CHAR(30) NOT NULL,&lt;br /&gt;  PRIMARY KEY (id)&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;INSERT INTO animals (name) VALUES&lt;br /&gt; ('dog'),('cat'),('penguin'),&lt;br /&gt; ('lax'),('whale'),('ostrich');&lt;br /&gt;&lt;br /&gt;SELECT * FROM animals;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Which returns:       &lt;/p&gt; &lt;pre class="programlisting"&gt;+----+---------+&lt;br /&gt;| id | name    |&lt;br /&gt;+----+---------+&lt;br /&gt;|  1 | dog     |&lt;br /&gt;|  2 | cat     |&lt;br /&gt;|  3 | penguin |&lt;br /&gt;|  4 | lax     |&lt;br /&gt;|  5 | whale   |&lt;br /&gt;|  6 | ostrich |&lt;br /&gt;+----+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         You can retrieve the most recent         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value with the         &lt;code class="literal"&gt;LAST_INSERT_ID()&lt;/code&gt; SQL function or the         &lt;code class="literal"&gt;mysql_insert_id()&lt;/code&gt; C API function. These         functions are connection-specific, so their return values are         not affected by another connection which is also performing         inserts.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: For a multiple-row         insert, &lt;code class="literal"&gt;LAST_INSERT_ID()&lt;/code&gt; and         &lt;code class="literal"&gt;mysql_insert_id()&lt;/code&gt; actually return the         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; key from the         &lt;span class="emphasis"&gt;&lt;em&gt;first&lt;/em&gt;&lt;/span&gt; of the inserted rows. This allows         multiple-row inserts to be reproduced correctly on other servers         in a replication setup.       &lt;/p&gt; &lt;p&gt;         For &lt;code class="literal"&gt;MyISAM&lt;/code&gt; and &lt;code class="literal"&gt;BDB&lt;/code&gt; tables         you can specify &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; on a secondary         column in a multiple-column index. In this case, the generated         value for the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is         calculated as         &lt;code class="literal"&gt;MAX(&lt;em class="replaceable"&gt;&lt;code&gt;auto_increment_column&lt;/code&gt;&lt;/em&gt;) +         1 WHERE         prefix=&lt;em class="replaceable"&gt;&lt;code&gt;given-prefix&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;. This         is useful when you want to put data into ordered groups.       &lt;/p&gt; &lt;pre class="programlisting"&gt;CREATE TABLE animals (&lt;br /&gt; grp ENUM('fish','mammal','bird') NOT NULL,&lt;br /&gt; id MEDIUMINT NOT NULL AUTO_INCREMENT,&lt;br /&gt; name CHAR(30) NOT NULL,&lt;br /&gt; PRIMARY KEY (grp,id)&lt;br /&gt;);&lt;br /&gt;&lt;br /&gt;INSERT INTO animals (grp,name) VALUES&lt;br /&gt; ('mammal','dog'),('mammal','cat'),&lt;br /&gt; ('bird','penguin'),('fish','lax'),('mammal','whale'),&lt;br /&gt; ('bird','ostrich');&lt;br /&gt;&lt;br /&gt;SELECT * FROM animals ORDER BY grp,id;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Which returns:       &lt;/p&gt; &lt;pre class="programlisting"&gt;+--------+----+---------+&lt;br /&gt;| grp    | id | name    |&lt;br /&gt;+--------+----+---------+&lt;br /&gt;| fish   |  1 | lax     |&lt;br /&gt;| mammal |  1 | dog     |&lt;br /&gt;| mammal |  2 | cat     |&lt;br /&gt;| mammal |  3 | whale   |&lt;br /&gt;| bird   |  1 | penguin |&lt;br /&gt;| bird   |  2 | ostrich |&lt;br /&gt;+--------+----+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Note that in this case (when the         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is part of a         multiple-column index), &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; values         are reused if you delete the row with the biggest         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value in any group. This         happens even for &lt;code class="literal"&gt;MyISAM&lt;/code&gt; tables, for which         &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; values normally are not         reused.       &lt;/p&gt; &lt;p&gt;         If the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; column is part of         multiple indexes, MySQL will generate sequence values using the         index that begins with the &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt;         column, if there is one. For example, if the         &lt;code class="literal"&gt;animals&lt;/code&gt; table contained indexes         &lt;code class="literal"&gt;PRIMARY KEY (grp, id)&lt;/code&gt; and &lt;code class="literal"&gt;INDEX         (id)&lt;/code&gt;, MySQL would ignore the &lt;code class="literal"&gt;PRIMARY         KEY&lt;/code&gt; for generating sequence values. As a result, the         table would contain a single sequence, not a sequence per         &lt;code class="literal"&gt;grp&lt;/code&gt; value.       &lt;/p&gt; &lt;p&gt;         To start with an &lt;code class="literal"&gt;AUTO_INCREMENT&lt;/code&gt; value other         than 1, you can set that value with &lt;code class="literal"&gt;CREATE         TABLE&lt;/code&gt; or &lt;code class="literal"&gt;ALTER TABLE&lt;/code&gt;, like this:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;ALTER TABLE tbl AUTO_INCREMENT = 100;&lt;/code&gt;&lt;/strong&gt; &lt;span style="font-family:Georgia,serif;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;!--/UdmComment--&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-5649643605336333680?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/5649643605336333680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=5649643605336333680' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/5649643605336333680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/5649643605336333680'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/36-examples-of-common-queries.html' title='3.6. Examples of Common Queries'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-5195028492173842560</id><published>2007-04-03T22:33:00.000-07:00</published><updated>2007-04-03T22:52:44.229-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.5. Using mysql in Batch Mode</title><content type='html'>&lt;p&gt;       In the previous sections, you used &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       interactively to enter queries and view the results. You can also       run &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; in batch mode. To do this, put the       commands you want to run in a file, then tell       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to read its input from the file:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql &lt; &lt;em class="replaceable"&gt;&lt;code&gt;batch-file&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       If you are running &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; under Windows and have       some special characters in the file that cause problems, you can       do this:     &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -e "source &lt;em class="replaceable"&gt;&lt;code&gt;batch-file&lt;/code&gt;&lt;/em&gt;"&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       If you need to specify connection parameters on the command line,       the command might look like this:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -h &lt;em class="replaceable"&gt;&lt;code&gt;host&lt;/code&gt;&lt;/em&gt; -u &lt;em class="replaceable"&gt;&lt;code&gt;user&lt;/code&gt;&lt;/em&gt; -p &lt; &lt;em class="replaceable"&gt;&lt;code&gt;batch-file&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;********&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       When you use &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; this way, you are creating a       script file, then executing the script.     &lt;/p&gt; &lt;p&gt;       If you want the script to continue even if some of the statements       in it produce errors, you should use the &lt;code class="option"&gt;--force&lt;/code&gt;       command-line option.     &lt;/p&gt; &lt;p&gt;       Why use a script? Here are a few reasons:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           If you run a query repeatedly (say, every day or every week),           making it a script allows you to avoid retyping it each time           you execute it.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           You can generate new queries from existing ones that are           similar by copying and editing script files.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Batch mode can also be useful while you're developing a query,           particularly for multiple-line commands or multiple-statement           sequences of commands. If you make a mistake, you don't have           to retype everything. Just edit your script to correct the           error, then tell &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; to execute it again.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;p&gt;           If you have a query that produces a lot of output, you can run           the output through a pager rather than watching it scroll off           the top of your screen:         &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql &lt; &lt;em class="replaceable"&gt;&lt;code&gt;batch-file&lt;/code&gt;&lt;/em&gt; | more&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;           You can catch the output in a file for further processing:         &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql &lt; &lt;em class="replaceable"&gt;&lt;code&gt;batch-file&lt;/code&gt;&lt;/em&gt; &gt; mysql.out&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;/li&gt;&lt;li&gt;&lt;p&gt;           You can distribute your script to other people so that they           can also run the commands.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Some situations do not allow for interactive use, for example,           when you run a query from a &lt;span&gt;&lt;strong class="command"&gt;cron&lt;/strong&gt;&lt;/span&gt; job. In           this case, you must use batch mode.         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       The default output format is different (more concise) when you run       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; in batch mode than when you use it       interactively. For example, the output of &lt;code class="literal"&gt;SELECT DISTINCT       species FROM pet&lt;/code&gt; looks like this when       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is run interactively:     &lt;/p&gt; &lt;pre class="programlisting"&gt;+---------+&lt;br /&gt;| species |&lt;br /&gt;+---------+&lt;br /&gt;| bird    |&lt;br /&gt;| cat     |&lt;br /&gt;| dog     |&lt;br /&gt;| hamster |&lt;br /&gt;| snake   |&lt;br /&gt;+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       In batch mode, the output looks like this instead:     &lt;/p&gt; &lt;pre class="programlisting"&gt;species&lt;br /&gt;bird&lt;br /&gt;cat&lt;br /&gt;dog&lt;br /&gt;hamster&lt;br /&gt;snake&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       If you want to get the interactive output format in batch mode,       use &lt;code class="literal"&gt;mysql -t&lt;/code&gt;. To echo to the output the       commands that are executed, use &lt;code class="literal"&gt;mysql -vvv&lt;/code&gt;.     &lt;/p&gt; &lt;a class="indexterm" name="id3079602"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3079609"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3079616"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id3079624"&gt;&lt;/a&gt;&lt;p&gt;       You can also use scripts from the &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; prompt       by using the &lt;code class="literal"&gt;source&lt;/code&gt; command or       &lt;code class="literal"&gt;\.&lt;/code&gt; command:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;source &lt;em class="replaceable"&gt;&lt;code&gt;filename&lt;/code&gt;&lt;/em&gt;;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;\. &lt;em class="replaceable"&gt;&lt;code&gt;filename&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;&lt;/strong&gt;How to measure total batch running time for several SQLs:&lt;br /&gt;&lt;br /&gt;# at start of your script file&lt;br /&gt;SET @start=UNIX_TIMESTAMP();&lt;br /&gt;&lt;br /&gt;# great job&lt;br /&gt;...&lt;br /&gt;...&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;# at bottom of your script file&lt;br /&gt;SET&lt;br /&gt; @s=@seconds:=UNIX_TIMESTAMP()-@start,&lt;br /&gt; @d=TRUNCATE(@s/86400,0), @s=MOD(@s,86400),&lt;br /&gt; @h=TRUNCATE(@s/3600,0), @s=MOD(@s,3600),&lt;br /&gt; @m=TRUNCATE(@s/60,0), @s=MOD(@s,60),&lt;br /&gt; @day=IF(@d&gt;0,CONCAT(@d,' day'),''),&lt;br /&gt; @hour=IF(@d+@h&gt;0,CONCAT(IF(@d&gt;0,LPAD(@h,2,'0'),@h),' hour'),''),&lt;br /&gt; @min=IF(@d+@h+@m&gt;0,CONCAT(IF(@d+@h&gt;0,LPAD(@m,2,'0'),@m),' min.'),''),&lt;br /&gt; @sec=CONCAT(IF(@d+@h+@m&gt;0,LPAD(@s,2,'0'),@s),' sec.');&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt; CONCAT(@seconds,' sec.') AS seconds,&lt;br /&gt; CONCAT_WS(' ',@day,@hour,@min,@sec) AS elapsed;&lt;br /&gt;&lt;br /&gt;# enjoy :)&lt;br /&gt;&lt;br /&gt;p.s. Tested &amp; works&lt;br /&gt;p.p.s. No fractions of seconds :(&lt;br /&gt;&lt;br /&gt;Example of a Korn Shell Script&lt;br /&gt;&lt;br /&gt;#!/bin/ksh&lt;br /&gt;mysql --user=&lt;user&gt; --password=&lt;password&gt; -h &lt;host&gt; &lt;&lt;!--! SELECT VERSION(), CURRENT_DATE; quit !!  When using mysql in batch mode there is an odd requirement that cost me hours. Your .sql file cannot contain any #comment lines. Delete them and importing will be a breeze. There is no indication that this is the  Lines beginning with two dashes are comment lines. Inside a shell script you will use #comment lines in the shell part and if you use a here-document you must use --comment lines inside the here document.  A more secure way to use the shell.  So that passwords are not embedded in the shell source file create a password file:  echo "batchpassword" --&gt; /etc/security/mysqlpassword&lt;br /&gt;chmod 200 /etc/security/mysqlpassword&lt;br /&gt;&lt;br /&gt;Then in your script:&lt;br /&gt;&lt;br /&gt;echo "update tablex set x=1 where a=2;" | mysql mydb --user=batchdb --password=`cat /etc/security/mysqlpassword`&lt;br /&gt;&lt;br /&gt;This&lt;br /&gt;assumes you have created a user called "batchdb" with that password and&lt;br /&gt;&lt;br /&gt;When using mysql in batch mode you can use pipes to write an&lt;br /&gt;interactive but pre-scripted shell script. Be aware that you need to&lt;br /&gt;use the "-n" command line option to flush the buffer otherwise your&lt;br /&gt;&lt;br /&gt;N.B. On most Unix systems, by placing the password in the command line&lt;br /&gt;with --password (even the above method for using a password file) you&lt;br /&gt;are making the password visible to local users, who can see the command&lt;br /&gt;string with a "ps" or "w" command.&lt;br /&gt;&lt;br /&gt;Whilst some systems can be&lt;br /&gt;set to block this, and others would let you wrap the command in&lt;br /&gt;something that would overwrite what users could see as your command,&lt;br /&gt;the best way to do any automations like this is to create a specific&lt;br /&gt;unix user for the job (or use a user that is already secure) and place&lt;br /&gt;the password in the .my.cnf file for that user - making sure the&lt;br /&gt;&lt;br /&gt;For newbies like me:  This exact command allowed me to run a script from outside MySQL (using the DOS command line in Win98):&lt;br /&gt;&lt;br /&gt;C:\WINDOWS\Desktop&gt;c:\mysql\bin\mysql -u root -p &lt; "c:\mysql\scripts\20060416_ShowInnodbstatusscript.txt" | more  Note: My text file had two commands:      Use db_name;     Show InnoDB Status;  FYI: I had been plagued by a foreign key error, but was unable figure out how to see the result of my Show Innodb Status command due to my 50-line DOS screen limitation. To get around this problem I    a) created the simple script shown above   b) created a foreign key error while logged into my MySQL user account, then   c) opened an additional DOS window to execute the command shown above.  Running the command outside of MySQL essentially creates a way to view command results one page at a time for folks administering MySQL at the command   line (using Windows). permissions are set so that only the owner can read it read will hang. Here is a code sample in ksh:  ------------------------------------------------- #!/bin/ksh mysql -u username -ppassword -D dbname -ss -n -q |&amp; print -p -- "select count(*) from some_table;" read -p get_row_count1 print -p -- "select count(*) from some_other_table;" read -p get_row_count2 print -p exit ; # echo $get_row_count1 echo $get_row_count2 # exit ------------------------------------------------- (The -q option is optional)  Note:  If you dislike using "-n" then make sure all your read statements are after the exit. the correct access rights to the database called "mydb". Example: ===file petquery.sh=== #!/bin/sh #  This is a comment mysql -t &lt;&lt;stop is="" a="" comment="" an="" use="" menagerie="" select="" from="" pet="" q="" stop="" test="" echo="" your="" batch="" job="" terminated="" gracefully="" sh="==" do="" not="" cut="" paste="" i="" don="" t="" know="" how="" make="" source="" listings="" inside="" dk="" experimentation="" revealed="" this="" to="" be="" the="=="&gt;&lt;/stop&gt;&lt;/host&gt;&lt;/password&gt;&lt;/user&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-5195028492173842560?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/5195028492173842560/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=5195028492173842560' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/5195028492173842560'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/5195028492173842560'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/35-using-mysql-in-batch-mode.html' title='3.5. Using mysql in Batch Mode'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-3729057585828248424</id><published>2007-04-03T22:22:00.000-07:00</published><updated>2007-04-03T22:33:20.728-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.4. Getting Information About Databases and Tables</title><content type='html'>What if you forget the name of a database or table, or what the structure of a given table is (for example, what its columns are called)? MySQL addresses this problem through several statements that provide information about the databases and tables it supports.&lt;br /&gt;&lt;br /&gt;You have previously seen SHOW DATABASES, which lists the databases managed by the server. To find out which database is currently selected, use the DATABASE() function:&lt;br /&gt;&lt;br /&gt;mysql&gt; SELECT DATABASE();&lt;br /&gt;+------------+&lt;br /&gt;| DATABASE() |&lt;br /&gt;+------------+&lt;br /&gt;| menagerie  |&lt;br /&gt;+------------+&lt;br /&gt;&lt;br /&gt;If you have not yet selected any database, the result is NULL.&lt;br /&gt;&lt;br /&gt;To find out what tables the default database contains (for example, when you are not sure about the name of a table), use this command:&lt;br /&gt;&lt;br /&gt;mysql&gt; SHOW TABLES;&lt;br /&gt;+---------------------+&lt;br /&gt;| Tables in menagerie |&lt;br /&gt;+---------------------+&lt;br /&gt;| event               |&lt;br /&gt;| pet                 |&lt;br /&gt;+---------------------+&lt;br /&gt;&lt;br /&gt;If you want to find out about the structure of a table, the DESCRIBE command is useful; it displays information about each of a table's columns:&lt;br /&gt;&lt;br /&gt;mysql&gt; DESCRIBE pet;&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;| Field   | Type        | Null | Key | Default | Extra |&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;| name    | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| owner   | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| species | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| sex     | char(1)     | YES  |     | NULL    |       |&lt;br /&gt;| birth   | date        | YES  |     | NULL    |       |&lt;br /&gt;| death   | date        | YES  |     | NULL    |       |&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;&lt;br /&gt;Field indicates the column name, Type is the data type for the column, NULL indicates whether the column can contain NULL values, Key indicates whether the column is indexed, and Default specifies the column's default value.&lt;br /&gt;&lt;br /&gt;If you have indexes on a table, SHOW INDEX FROM tbl_name produces information about them.&lt;br /&gt;&lt;br /&gt;mysql&gt; SHOW CREATE TABLE &lt;table-name&gt;;&lt;br /&gt;&lt;br /&gt;The "Extra" field records special information about columns. &lt;br /&gt;If you have selected the "auto_increment" functionality for a column, for example, that would show up in the "Extra" field when doing a "describe".&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-3729057585828248424?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/3729057585828248424/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=3729057585828248424' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3729057585828248424'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/3729057585828248424'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/34-getting-information-about-databases.html' title='3.4. Getting Information About Databases and Tables'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-4916534054440222903</id><published>2007-04-02T21:22:00.000-07:00</published><updated>2007-04-02T23:00:49.960-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.3. Creating and Using a Database</title><content type='html'>&lt;p&gt;       Once you know how to enter commands, you are ready to access a       database.     &lt;/p&gt; &lt;p&gt;       Suppose that you have several pets in your home (your menagerie)       and you would like to keep track of various types of information       about them. You can do so by creating tables to hold your data and       loading them with the desired information. Then you can answer       different sorts of questions about your animals by retrieving data       from the tables. This section shows you how to:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           Create a database         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Create a table         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Load data into the table         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Retrieve data from the table in various ways         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Use multiple tables         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       The menagerie database is simple (deliberately), but it is not       difficult to think of real-world situations in which a similar       type of database might be used. For example, a database like this       could be used by a farmer to keep track of livestock, or by a       veterinarian to keep track of patient records. A menagerie       distribution containing some of the queries and sample data used       in the following sections can be obtained from the MySQL Web site.       It is available in both compressed &lt;span&gt;&lt;strong class="command"&gt;tar&lt;/strong&gt;&lt;/span&gt; file and       Zip formats at &lt;a href="http://dev.mysql.com/doc/" target="_top"&gt;http://dev.mysql.com/doc/&lt;/a&gt;.     &lt;/p&gt; &lt;p&gt;       Use the &lt;code class="literal"&gt;SHOW&lt;/code&gt; statement to find out what       databases currently exist on the server:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SHOW DATABASES;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+&lt;br /&gt;| Database |&lt;br /&gt;+----------+&lt;br /&gt;| mysql    |&lt;br /&gt;| test     |&lt;br /&gt;| tmp      |&lt;br /&gt;+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The &lt;code class="literal"&gt;mysql&lt;/code&gt; database describes user access       privileges. The &lt;code class="literal"&gt;test&lt;/code&gt; database often is       available as a workspace for users to try things out.     &lt;/p&gt; &lt;p&gt;       The list of databases displayed by the statement may be different       on your machine; &lt;code class="literal"&gt;SHOW DATABASES&lt;/code&gt; does not show       databases that you have no privileges for if you do not have the       &lt;code class="literal"&gt;SHOW DATABASES&lt;/code&gt; privilege. See       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/show-databases.html" title="13.5.4.8. SHOW DATABASES Syntax"&gt;Section 13.5.4.8, “&lt;code class="literal"&gt;SHOW DATABASES&lt;/code&gt; Syntax”&lt;/a&gt;.     &lt;/p&gt; &lt;p&gt;       If the &lt;code class="literal"&gt;test&lt;/code&gt; database exists, try to access it:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;USE test&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Database changed&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       Note that &lt;code class="literal"&gt;USE&lt;/code&gt;, like &lt;code class="literal"&gt;QUIT&lt;/code&gt;,       does not require a semicolon. (You can terminate such statements       with a semicolon if you like; it does no harm.) The       &lt;code class="literal"&gt;USE&lt;/code&gt; statement is special in another way, too:       it must be given on a single line.     &lt;/p&gt; &lt;p&gt;       You can use the &lt;code class="literal"&gt;test&lt;/code&gt; database (if you have       access to it) for the examples that follow, but anything you       create in that database can be removed by anyone else with access       to it. For this reason, you should probably ask your MySQL       administrator for permission to use a database of your own.       Suppose that you want to call yours &lt;code class="literal"&gt;menagerie&lt;/code&gt;.       The administrator needs to execute a command like this:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;GRANT ALL ON menagerie.* TO 'your_mysql_name'@'your_client_host';&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       where &lt;code class="literal"&gt;your_mysql_name&lt;/code&gt; is the MySQL user name       assigned to you and &lt;code class="literal"&gt;your_client_host&lt;/code&gt; is the       host from which you connect to the server.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.3.1. Creating and Selecting a Database&lt;/h3&gt; &lt;a class="indexterm" name="id2794228"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2794238"&gt;&lt;/a&gt;&lt;p&gt;         If the administrator creates your database for you when setting         up your permissions, you can begin using it. Otherwise, you need         to create it yourself:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;CREATE DATABASE menagerie;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Under Unix, database names are case sensitive (unlike SQL         keywords), so you must always refer to your database as         &lt;code class="literal"&gt;menagerie&lt;/code&gt;, not as         &lt;code class="literal"&gt;Menagerie&lt;/code&gt;, &lt;code class="literal"&gt;MENAGERIE&lt;/code&gt;, or         some other variant. This is also true for table names. (Under         Windows, this restriction does not apply, although you must         refer to databases and tables using the same lettercase         throughout a given query. However, for a variety of reasons, our         recommended best practice is always to use the same lettercase         that was used when the database was created.)       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: If you get an error such         as &lt;span class="errortext"&gt;ERROR 1044 (42000): Access denied for user         'monty'@'localhost' to database 'menagerie'&lt;/span&gt; when         attempting to create a database, this means that your user         account does not have the necessary privileges to do so. Discuss         this with the administrator or see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/privilege-system.html" title="5.7. The MySQL Access Privilege System"&gt;Section 5.7, “The MySQL Access Privilege System”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         Creating a database does not select it for use; you must do that         explicitly. To make &lt;code class="literal"&gt;menagerie&lt;/code&gt; the current         database, use this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;USE menagerie;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Database changed&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Your database needs to be created only once, but you must select         it for use each time you begin a &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;         session. You can do this by issuing a &lt;code class="literal"&gt;USE&lt;/code&gt;         statement as shown in the example. Alternatively, you can select         the database on the command line when you invoke         &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;. Just specify its name after any         connection parameters that you might need to provide. For         example:       &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -h &lt;em class="replaceable"&gt;&lt;code&gt;host&lt;/code&gt;&lt;/em&gt; -u &lt;em class="replaceable"&gt;&lt;code&gt;user&lt;/code&gt;&lt;/em&gt; -p menagerie&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;********&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Note that &lt;code class="literal"&gt;menagerie&lt;/code&gt; in the command just shown         is &lt;span class="bold"&gt;&lt;strong&gt;not&lt;/strong&gt;&lt;/span&gt; your password. If you         want to supply your password on the command line after the         &lt;code class="literal"&gt;-p&lt;/code&gt; option, you must do so with no intervening         space (for example, as &lt;code class="literal"&gt;-pmypassword&lt;/code&gt;,         &lt;span class="emphasis"&gt;&lt;em&gt;not&lt;/em&gt;&lt;/span&gt; as &lt;code class="literal"&gt;-p mypassword&lt;/code&gt;).         However, putting your password on the command line is not         recommended, because doing so exposes it to snooping by other         users logged in on your machine.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.3.2. Creating a Table&lt;/h3&gt; &lt;a class="indexterm" name="id2794408"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2794418"&gt;&lt;/a&gt;&lt;p&gt;         Creating the database is the easy part, but at this point it's         empty, as &lt;code class="literal"&gt;SHOW TABLES&lt;/code&gt; tells you:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SHOW TABLES;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Empty set (0.00 sec)&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The harder part is deciding what the structure of your database         should be: what tables you need and what columns should be in         each of them.       &lt;/p&gt; &lt;p&gt;         You want a table that contains a record for each of your pets.         This can be called the &lt;code class="literal"&gt;pet&lt;/code&gt; table, and it         should contain, as a bare minimum, each animal's name. Because         the name by itself is not very interesting, the table should         contain other information. For example, if more than one person         in your family keeps pets, you might want to list each animal's         owner. You might also want to record some basic descriptive         information such as species and sex.       &lt;/p&gt; &lt;p&gt;         How about age? That might be of interest, but it's not a good         thing to store in a database. Age changes as time passes, which         means you'd have to update your records often. Instead, it's         better to store a fixed value such as date of birth. Then,         whenever you need age, you can calculate it as the difference         between the current date and the birth date. MySQL provides         functions for doing date arithmetic, so this is not difficult.         Storing birth date rather than age has other advantages, too:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             You can use the database for tasks such as generating             reminders for upcoming pet birthdays. (If you think this             type of query is somewhat silly, note that it is the same             question you might ask in the context of a business database             to identify clients to whom you need to send out birthday             greetings in the current week or month, for that             computer-assisted personal touch.)           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             You can calculate age in relation to dates other than the             current date. For example, if you store death date in the             database, you can easily calculate how old a pet was when it             died.           &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         You can probably think of other types of information that would         be useful in the &lt;code class="literal"&gt;pet&lt;/code&gt; table, but the ones         identified so far are sufficient: name, owner, species, sex,         birth, and death.       &lt;/p&gt; &lt;p&gt;         Use a &lt;code class="literal"&gt;CREATE TABLE&lt;/code&gt; statement to specify the         layout of your table:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;-&gt; &lt;strong class="userinput"&gt;&lt;code&gt;species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         &lt;code class="literal"&gt;VARCHAR&lt;/code&gt; is a good choice for the         &lt;code class="literal"&gt;name&lt;/code&gt;, &lt;code class="literal"&gt;owner&lt;/code&gt;, and         &lt;code class="literal"&gt;species&lt;/code&gt; columns because the column values         vary in length. The lengths in those column definitions need not         all be the same, and need not be &lt;code class="literal"&gt;20&lt;/code&gt;. You can         normally pick any length from &lt;code class="literal"&gt;1&lt;/code&gt; to         &lt;code class="literal"&gt;65535&lt;/code&gt;, whatever seems most reasonable to you.         (&lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: Prior to MySQL 5.0.3,         the upper limit was 255.) If you make a poor choice and it turns         out later that you need a longer field, MySQL provides an         &lt;code class="literal"&gt;ALTER TABLE&lt;/code&gt; statement.       &lt;/p&gt; &lt;p&gt;         Several types of values can be chosen to represent sex in animal         records, such as &lt;code class="literal"&gt;'m'&lt;/code&gt; and         &lt;code class="literal"&gt;'f'&lt;/code&gt;, or perhaps &lt;code class="literal"&gt;'male'&lt;/code&gt; and         &lt;code class="literal"&gt;'female'&lt;/code&gt;. It is simplest to use the single         characters &lt;code class="literal"&gt;'m'&lt;/code&gt; and &lt;code class="literal"&gt;'f'&lt;/code&gt;.       &lt;/p&gt; &lt;p&gt;         The use of the &lt;code class="literal"&gt;DATE&lt;/code&gt; data type for the         &lt;code class="literal"&gt;birth&lt;/code&gt; and &lt;code class="literal"&gt;death&lt;/code&gt; columns is         a fairly obvious choice.       &lt;/p&gt; &lt;p&gt;         Once you have created a table, &lt;code class="literal"&gt;SHOW TABLES&lt;/code&gt;         should produce some output:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SHOW TABLES;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------------------+&lt;br /&gt;| Tables in menagerie |&lt;br /&gt;+---------------------+&lt;br /&gt;| pet                 |&lt;br /&gt;+---------------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         To verify that your table was created the way you expected, use         a &lt;code class="literal"&gt;DESCRIBE&lt;/code&gt; statement:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;DESCRIBE pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;| Field   | Type        | Null | Key | Default | Extra |&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;| name    | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| owner   | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| species | varchar(20) | YES  |     | NULL    |       |&lt;br /&gt;| sex     | char(1)     | YES  |     | NULL    |       |&lt;br /&gt;| birth   | date        | YES  |     | NULL    |       |&lt;br /&gt;| death   | date        | YES  |     | NULL    |       |&lt;br /&gt;+---------+-------------+------+-----+---------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         You can use &lt;code class="literal"&gt;DESCRIBE&lt;/code&gt; any time, for example,         if you forget the names of the columns in your table or what         types they have.       &lt;/p&gt; &lt;p&gt;         For more information about MySQL data types, see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/data-types.html" title="Chapter 11. Data Types"&gt;Chapter 11, &lt;i&gt;Data Types&lt;/i&gt;&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.3.3. Loading Data into a Table&lt;/h3&gt; &lt;a class="indexterm" name="id2794698"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2794708"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2794718"&gt;&lt;/a&gt;&lt;p&gt;         After creating your table, you need to populate it. The         &lt;code class="literal"&gt;LOAD DATA&lt;/code&gt; and &lt;code class="literal"&gt;INSERT&lt;/code&gt;         statements are useful for this.       &lt;/p&gt; &lt;p&gt;         Suppose that your pet records can be described as shown here.         (Observe that MySQL expects dates in         &lt;code class="literal"&gt;'YYYY-MM-DD'&lt;/code&gt; format; this may be different         from what you are used to.)       &lt;/p&gt; &lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;name&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;owner&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;species&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;sex&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;birth&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;death&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fluffy&lt;/td&gt; &lt;td&gt;Harold&lt;/td&gt; &lt;td&gt;cat&lt;/td&gt; &lt;td&gt;f&lt;/td&gt; &lt;td&gt;1993-02-04&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Claws&lt;/td&gt; &lt;td&gt;Gwen&lt;/td&gt; &lt;td&gt;cat&lt;/td&gt; &lt;td&gt;m&lt;/td&gt; &lt;td&gt;1994-03-17&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Buffy&lt;/td&gt; &lt;td&gt;Harold&lt;/td&gt; &lt;td&gt;dog&lt;/td&gt; &lt;td&gt;f&lt;/td&gt; &lt;td&gt;1989-05-13&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fang&lt;/td&gt; &lt;td&gt;Benny&lt;/td&gt; &lt;td&gt;dog&lt;/td&gt; &lt;td&gt;m&lt;/td&gt; &lt;td&gt;1990-08-27&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Bowser&lt;/td&gt; &lt;td&gt;Diane&lt;/td&gt; &lt;td&gt;dog&lt;/td&gt; &lt;td&gt;m&lt;/td&gt; &lt;td&gt;1979-08-31&lt;/td&gt; &lt;td&gt;1995-07-29&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Chirpy&lt;/td&gt; &lt;td&gt;Gwen&lt;/td&gt; &lt;td&gt;bird&lt;/td&gt; &lt;td&gt;f&lt;/td&gt; &lt;td&gt;1998-09-11&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Whistler&lt;/td&gt; &lt;td&gt;Gwen&lt;/td&gt; &lt;td&gt;bird&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;td&gt;1997-12-09&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Slim&lt;/td&gt; &lt;td&gt;Benny&lt;/td&gt; &lt;td&gt;snake&lt;/td&gt; &lt;td&gt;m&lt;/td&gt; &lt;td&gt;1996-04-29&lt;/td&gt; &lt;td&gt;&lt;br /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;         Because you are beginning with an empty table, an easy way to         populate it is to create a text file containing a row for each         of your animals, then load the contents of the file into the         table with a single statement.       &lt;/p&gt; &lt;p&gt;         You could create a text file &lt;code class="filename"&gt;pet.txt&lt;/code&gt;         containing one record per line, with values separated by tabs,         and given in the order in which the columns were listed in the         &lt;code class="literal"&gt;CREATE TABLE&lt;/code&gt; statement. For missing values         (such as unknown sexes or death dates for animals that are still         living), you can use &lt;code class="literal"&gt;NULL&lt;/code&gt; values. To         represent these in your text file, use &lt;code class="literal"&gt;\N&lt;/code&gt;         (backslash, capital-N). For example, the record for Whistler the         bird would look like this (where the whitespace between values         is a single tab character):       &lt;/p&gt; &lt;pre class="programlisting"&gt;Whistler        Gwen    bird    \N      1997-12-09      \N&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         To load the text file &lt;code class="filename"&gt;pet.txt&lt;/code&gt; into the         &lt;code class="literal"&gt;pet&lt;/code&gt; table, use this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Note that if you created the file on Windows with an editor that         uses &lt;code class="literal"&gt;\r\n&lt;/code&gt; as a line terminator, you should         use:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;-&gt; &lt;strong class="userinput"&gt;&lt;code&gt;LINES TERMINATED BY '\r\n';&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         (On an Apple machine running OS X, you would likely want to use         &lt;code class="literal"&gt;LINES TERMINATED BY '\r'&lt;/code&gt;.)       &lt;/p&gt; &lt;p&gt;         You can specify the column value separator and end of line         marker explicitly in the &lt;code class="literal"&gt;LOAD DATA&lt;/code&gt; statement         if you wish, but the defaults are tab and linefeed. These are         sufficient for the statement to read the file         &lt;code class="filename"&gt;pet.txt&lt;/code&gt; properly.       &lt;/p&gt; &lt;p&gt;         If the statement fails, it is likely that your MySQL         installation does not have local file capability enabled by         default. See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/load-data-local.html" title="5.6.4. Security Issues with LOAD DATA LOCAL"&gt;Section 5.6.4, “Security Issues with &lt;code class="literal"&gt;LOAD DATA LOCAL&lt;/code&gt;”&lt;/a&gt;, for information         on how to change this.       &lt;/p&gt; &lt;p&gt;         When you want to add new records one at a time, the         &lt;code class="literal"&gt;INSERT&lt;/code&gt; statement is useful. In its simplest         form, you supply values for each column, in the order in which         the columns were listed in the &lt;code class="literal"&gt;CREATE TABLE&lt;/code&gt;         statement. Suppose that Diane gets a new hamster named         “&lt;span class="quote"&gt;Puffball.&lt;/span&gt;” You could add a new record using an         &lt;code class="literal"&gt;INSERT&lt;/code&gt; statement like this:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;INSERT INTO pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;-&gt; &lt;strong class="userinput"&gt;&lt;code&gt;VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         Note that string and date values are specified as quoted strings         here. Also, with &lt;code class="literal"&gt;INSERT&lt;/code&gt;, you can insert         &lt;code class="literal"&gt;NULL&lt;/code&gt; directly to represent a missing value.         You do not use &lt;code class="literal"&gt;\N&lt;/code&gt; like you do with         &lt;code class="literal"&gt;LOAD DATA&lt;/code&gt;.       &lt;/p&gt; &lt;p&gt;         From this example, you should be able to see that there would be         a lot more typing involved to load your records initially using         several &lt;code class="literal"&gt;INSERT&lt;/code&gt; statements rather than a         single &lt;code class="literal"&gt;LOAD DATA&lt;/code&gt; statement.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h3 class="title"&gt;3.3.4. Retrieving Information from a Table&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;         The &lt;code class="literal"&gt;SELECT&lt;/code&gt; statement is used to pull         information from a table. The general form of the statement is:       &lt;/p&gt; &lt;pre class="programlisting"&gt;SELECT &lt;em class="replaceable"&gt;&lt;code&gt;what_to_select&lt;/code&gt;&lt;/em&gt;&lt;br /&gt;FROM &lt;em class="replaceable"&gt;&lt;code&gt;which_table&lt;/code&gt;&lt;/em&gt;&lt;br /&gt;WHERE &lt;em class="replaceable"&gt;&lt;code&gt;conditions_to_satisfy&lt;/code&gt;&lt;/em&gt;;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         &lt;em class="replaceable"&gt;&lt;code&gt;what_to_select&lt;/code&gt;&lt;/em&gt; indicates what you         want to see. This can be a list of columns, or         &lt;code class="literal"&gt;*&lt;/code&gt; to indicate “&lt;span class="quote"&gt;all columns.&lt;/span&gt;”         &lt;em class="replaceable"&gt;&lt;code&gt;which_table&lt;/code&gt;&lt;/em&gt; indicates the table from         which you want to retrieve data. The &lt;code class="literal"&gt;WHERE&lt;/code&gt;         clause is optional. If it is present,         &lt;em class="replaceable"&gt;&lt;code&gt;conditions_to_satisfy&lt;/code&gt;&lt;/em&gt; specifies one         or more conditions that rows must satisfy to qualify for         retrieval.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.1. Selecting All Data&lt;/h4&gt; &lt;p&gt;           The simplest form of &lt;code class="literal"&gt;SELECT&lt;/code&gt; retrieves           everything from a table:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+--------+---------+------+------------+------------+&lt;br /&gt;| name     | owner  | species | sex  | birth      | death      |&lt;br /&gt;+----------+--------+---------+------+------------+------------+&lt;br /&gt;| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |&lt;br /&gt;| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |&lt;br /&gt;| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |&lt;br /&gt;| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |&lt;br /&gt;| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |&lt;br /&gt;| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |&lt;br /&gt;| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |&lt;br /&gt;| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |&lt;br /&gt;| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL       |&lt;br /&gt;+----------+--------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           This form of &lt;code class="literal"&gt;SELECT&lt;/code&gt; is useful if you want           to review your entire table, for example, after you've just           loaded it with your initial dataset. For example, you may           happen to think that the birth date for Bowser doesn't seem           quite right. Consulting your original pedigree papers, you           find that the correct birth year should be 1989, not 1979.         &lt;/p&gt; &lt;p&gt;           There are at least two ways to fix this:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;               Edit the file &lt;code class="filename"&gt;pet.txt&lt;/code&gt; to correct the               error, then empty the table and reload it using               &lt;code class="literal"&gt;DELETE&lt;/code&gt; and &lt;code class="literal"&gt;LOAD               DATA&lt;/code&gt;:             &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;DELETE FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;LOAD DATA LOCAL INFILE 'pet.txt' INTO TABLE pet;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;               However, if you do this, you must also re-enter the record               for Puffball.             &lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p&gt;               Fix only the erroneous record with an               &lt;code class="literal"&gt;UPDATE&lt;/code&gt; statement:             &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;               The &lt;code class="literal"&gt;UPDATE&lt;/code&gt; changes only the record in               question and does not require you to reload the table.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.2. Selecting Particular Rows&lt;/h4&gt; &lt;a class="indexterm" name="id2795442"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795452"&gt;&lt;/a&gt;&lt;p&gt;           As shown in the preceding section, it is easy to retrieve an           entire table. Just omit the &lt;code class="literal"&gt;WHERE&lt;/code&gt; clause           from the &lt;code class="literal"&gt;SELECT&lt;/code&gt; statement. But typically           you don't want to see the entire table, particularly when it           becomes large. Instead, you're usually more interested in           answering a particular question, in which case you specify           some constraints on the information you want. Let's look at           some selection queries in terms of questions about your pets           that they answer.         &lt;/p&gt; &lt;p&gt;           You can select only particular rows from your table. For           example, if you want to verify the change that you made to           Bowser's birth date, select Bowser's record like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name = 'Bowser';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+-------+---------+------+------------+------------+&lt;br /&gt;| name   | owner | species | sex  | birth      | death      |&lt;br /&gt;+--------+-------+---------+------+------------+------------+&lt;br /&gt;| Bowser | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |&lt;br /&gt;+--------+-------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           The output confirms that the year is correctly recorded as           1989, not 1979.         &lt;/p&gt; &lt;p&gt;           String comparisons normally are case-insensitive, so you can           specify the name as &lt;code class="literal"&gt;'bowser'&lt;/code&gt;,           &lt;code class="literal"&gt;'BOWSER'&lt;/code&gt;, and so forth. The query result is           the same.         &lt;/p&gt; &lt;p&gt;           You can specify conditions on any column, not just           &lt;code class="literal"&gt;name&lt;/code&gt;. For example, if you want to know           which animals were born during or after 1998, test the           &lt;code class="literal"&gt;birth&lt;/code&gt; column:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE birth &gt;= '1998-1-1';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;| name     | owner | species | sex  | birth      | death |&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |&lt;br /&gt;| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           You can combine conditions, for example, to locate female           dogs:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| name  | owner  | species | sex  | birth      | death |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           The preceding query uses the &lt;code class="literal"&gt;AND&lt;/code&gt; logical           operator. There is also an &lt;code class="literal"&gt;OR&lt;/code&gt; operator:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;| name     | owner | species | sex  | birth      | death |&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |&lt;br /&gt;| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |&lt;br /&gt;| Slim     | Benny | snake   | m    | 1996-04-29 | NULL  |&lt;br /&gt;+----------+-------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           &lt;code class="literal"&gt;AND&lt;/code&gt; and &lt;code class="literal"&gt;OR&lt;/code&gt; may be           intermixed, although &lt;code class="literal"&gt;AND&lt;/code&gt; has higher           precedence than &lt;code class="literal"&gt;OR&lt;/code&gt;. If you use both           operators, it is a good idea to use parentheses to indicate           explicitly how conditions should be grouped:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE (species = 'cat' AND sex = 'm')&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;OR (species = 'dog' AND sex = 'f');&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| name  | owner  | species | sex  | birth      | death |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |&lt;br /&gt;| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.3. Selecting Particular Columns&lt;/h4&gt; &lt;a class="indexterm" name="id2795641"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795651"&gt;&lt;/a&gt;&lt;p&gt;           If you do not want to see entire rows from your table, just           name the columns in which you are interested, separated by           commas. For example, if you want to know when your animals           were born, select the &lt;code class="literal"&gt;name&lt;/code&gt; and           &lt;code class="literal"&gt;birth&lt;/code&gt; columns:&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+&lt;br /&gt;| name     | birth      |&lt;br /&gt;+----------+------------+&lt;br /&gt;| Fluffy   | 1993-02-04 |&lt;br /&gt;| Claws    | 1994-03-17 |&lt;br /&gt;| Buffy    | 1989-05-13 |&lt;br /&gt;| Fang     | 1990-08-27 |&lt;br /&gt;| Bowser   | 1989-08-31 |&lt;br /&gt;| Chirpy   | 1998-09-11 |&lt;br /&gt;| Whistler | 1997-12-09 |&lt;br /&gt;| Slim     | 1996-04-29 |&lt;br /&gt;| Puffball | 1999-03-30 |&lt;br /&gt;+----------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To find out who owns pets, use this query:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT owner FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+&lt;br /&gt;| owner  |&lt;br /&gt;+--------+&lt;br /&gt;| Harold |&lt;br /&gt;| Gwen   |&lt;br /&gt;| Harold |&lt;br /&gt;| Benny  |&lt;br /&gt;| Diane  |&lt;br /&gt;| Gwen   |&lt;br /&gt;| Gwen   |&lt;br /&gt;| Benny  |&lt;br /&gt;| Diane  |&lt;br /&gt;+--------+&lt;br /&gt;&lt;/pre&gt; &lt;a class="indexterm" name="id2795704"&gt;&lt;/a&gt;&lt;p&gt;           Notice that the query simply retrieves the           &lt;code class="literal"&gt;owner&lt;/code&gt; column from each record, and some of           them appear more than once. To minimize the output, retrieve           each unique output record just once by adding the keyword           &lt;code class="literal"&gt;DISTINCT&lt;/code&gt;:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT DISTINCT owner FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+&lt;br /&gt;| owner  |&lt;br /&gt;+--------+&lt;br /&gt;| Benny  |&lt;br /&gt;| Diane  |&lt;br /&gt;| Gwen   |&lt;br /&gt;| Harold |&lt;br /&gt;+--------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           You can use a &lt;code class="literal"&gt;WHERE&lt;/code&gt; clause to combine row           selection with column selection. For example, to get birth           dates for dogs and cats only, use this query:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, species, birth FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE species = 'dog' OR species = 'cat';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+---------+------------+&lt;br /&gt;| name   | species | birth      |&lt;br /&gt;+--------+---------+------------+&lt;br /&gt;| Fluffy | cat     | 1993-02-04 |&lt;br /&gt;| Claws  | cat     | 1994-03-17 |&lt;br /&gt;| Buffy  | dog     | 1989-05-13 |&lt;br /&gt;| Fang   | dog     | 1990-08-27 |&lt;br /&gt;| Bowser | dog     | 1989-08-31 |&lt;br /&gt;+--------+---------+------------+&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="title"&gt;3.3.4.4. Sorting Rows&lt;/h4&gt; &lt;a class="indexterm" name="id2795770"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795777"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795787"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795797"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795808"&gt;&lt;/a&gt;&lt;p&gt;           You may have noticed in the preceding examples that the result           rows are displayed in no particular order. It's often easier           to examine query output when the rows are sorted in some           meaningful way. To sort a result, use an &lt;code class="literal"&gt;ORDER           BY&lt;/code&gt; clause.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;           Here are animal birthdays, sorted by date:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet ORDER BY birth;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+&lt;br /&gt;| name     | birth      |&lt;br /&gt;+----------+------------+&lt;br /&gt;| Buffy    | 1989-05-13 |&lt;br /&gt;| Bowser   | 1989-08-31 |&lt;br /&gt;| Fang     | 1990-08-27 |&lt;br /&gt;| Fluffy   | 1993-02-04 |&lt;br /&gt;| Claws    | 1994-03-17 |&lt;br /&gt;| Slim     | 1996-04-29 |&lt;br /&gt;| Whistler | 1997-12-09 |&lt;br /&gt;| Chirpy   | 1998-09-11 |&lt;br /&gt;| Puffball | 1999-03-30 |&lt;br /&gt;+----------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           On character type columns, sorting — like all other           comparison operations — is normally performed in a           case-insensitive fashion. This means that the order is           undefined for columns that are identical except for their           case. You can force a case-sensitive sort for a column by           using &lt;code class="literal"&gt;BINARY&lt;/code&gt; like so: &lt;code class="literal"&gt;ORDER BY           BINARY &lt;em class="replaceable"&gt;&lt;code&gt;col_name&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           The default sort order is ascending, with smallest values           first. To sort in reverse (descending) order, add the           &lt;code class="literal"&gt;DESC&lt;/code&gt; keyword to the name of the column you           are sorting by:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet ORDER BY birth DESC;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+&lt;br /&gt;| name     | birth      |&lt;br /&gt;+----------+------------+&lt;br /&gt;| Puffball | 1999-03-30 |&lt;br /&gt;| Chirpy   | 1998-09-11 |&lt;br /&gt;| Whistler | 1997-12-09 |&lt;br /&gt;| Slim     | 1996-04-29 |&lt;br /&gt;| Claws    | 1994-03-17 |&lt;br /&gt;| Fluffy   | 1993-02-04 |&lt;br /&gt;| Fang     | 1990-08-27 |&lt;br /&gt;| Bowser   | 1989-08-31 |&lt;br /&gt;| Buffy    | 1989-05-13 |&lt;br /&gt;+----------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           You can sort on multiple columns, and you can sort different           columns in different directions. For example, to sort by type           of animal in ascending order, then by birth date within animal           type in descending order (youngest animals first), use the           following query:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, species, birth FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;ORDER BY species, birth DESC;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+---------+------------+&lt;br /&gt;| name     | species | birth      |&lt;br /&gt;+----------+---------+------------+&lt;br /&gt;| Chirpy   | bird    | 1998-09-11 |&lt;br /&gt;| Whistler | bird    | 1997-12-09 |&lt;br /&gt;| Claws    | cat     | 1994-03-17 |&lt;br /&gt;| Fluffy   | cat     | 1993-02-04 |&lt;br /&gt;| Fang     | dog     | 1990-08-27 |&lt;br /&gt;| Bowser   | dog     | 1989-08-31 |&lt;br /&gt;| Buffy    | dog     | 1989-05-13 |&lt;br /&gt;| Puffball | hamster | 1999-03-30 |&lt;br /&gt;| Slim     | snake   | 1996-04-29 |&lt;br /&gt;+----------+---------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Note that the &lt;code class="literal"&gt;DESC&lt;/code&gt; keyword applies only to           the column name immediately preceding it           (&lt;code class="literal"&gt;birth&lt;/code&gt;); it does not affect the           &lt;code class="literal"&gt;species&lt;/code&gt; column sort order.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.5. Date Calculations&lt;/h4&gt; &lt;a class="indexterm" name="id2795951"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795958"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795968"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2795978"&gt;&lt;/a&gt;&lt;p&gt;           MySQL provides several functions that you can use to perform           calculations on dates, for example, to calculate ages or           extract parts of dates.         &lt;/p&gt; &lt;p&gt;           To determine how many years old each of your pets is, compute           the difference in the year part of the current date and the           birth date, then subtract one if the current date occurs           earlier in the calendar year than the birth date. The           following query shows, for each pet, the birth date, the           current date, and the age in years.         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth, CURDATE(),&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;(YEAR(CURDATE())-YEAR(birth))&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;- (RIGHT(CURDATE(),5)&lt;right(birth,5))&gt;&lt;/right(birth,5))&gt;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;AS age&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| name     | birth      | CURDATE()  | age  |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| Fluffy   | 1993-02-04 | 2003-08-19 |   10 |&lt;br /&gt;| Claws    | 1994-03-17 | 2003-08-19 |    9 |&lt;br /&gt;| Buffy    | 1989-05-13 | 2003-08-19 |   14 |&lt;br /&gt;| Fang     | 1990-08-27 | 2003-08-19 |   12 |&lt;br /&gt;| Bowser   | 1989-08-31 | 2003-08-19 |   13 |&lt;br /&gt;| Chirpy   | 1998-09-11 | 2003-08-19 |    4 |&lt;br /&gt;| Whistler | 1997-12-09 | 2003-08-19 |    5 |&lt;br /&gt;| Slim     | 1996-04-29 | 2003-08-19 |    7 |&lt;br /&gt;| Puffball | 1999-03-30 | 2003-08-19 |    4 |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Here, &lt;code class="literal"&gt;YEAR()&lt;/code&gt; pulls out the year part of a           date and &lt;code class="literal"&gt;RIGHT()&lt;/code&gt; pulls off the rightmost           five characters that represent the &lt;code class="literal"&gt;MM-DD&lt;/code&gt;           (calendar year) part of the date. The part of the expression           that compares the &lt;code class="literal"&gt;MM-DD&lt;/code&gt; values evaluates to           1 or 0, which adjusts the year difference down a year if           &lt;code class="literal"&gt;CURDATE()&lt;/code&gt; occurs earlier in the year than           &lt;code class="literal"&gt;birth&lt;/code&gt;. The full expression is somewhat           ungainly, so an &lt;span class="emphasis"&gt;&lt;em&gt;alias&lt;/em&gt;&lt;/span&gt;           (&lt;code class="literal"&gt;age&lt;/code&gt;) is used to make the output column           label more meaningful.         &lt;/p&gt; &lt;p&gt;           The query works, but the result could be scanned more easily           if the rows were presented in some order. This can be done by           adding an &lt;code class="literal"&gt;ORDER BY name&lt;/code&gt; clause to sort the           output by name:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth, CURDATE(),&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;(YEAR(CURDATE())-YEAR(birth))&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;- (RIGHT(CURDATE(),5)&lt;right(birth,5))&gt;&lt;/right(birth,5))&gt;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;AS age&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet ORDER BY name;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| name     | birth      | CURDATE()  | age  |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| Bowser   | 1989-08-31 | 2003-08-19 |   13 |&lt;br /&gt;| Buffy    | 1989-05-13 | 2003-08-19 |   14 |&lt;br /&gt;| Chirpy   | 1998-09-11 | 2003-08-19 |    4 |&lt;br /&gt;| Claws    | 1994-03-17 | 2003-08-19 |    9 |&lt;br /&gt;| Fang     | 1990-08-27 | 2003-08-19 |   12 |&lt;br /&gt;| Fluffy   | 1993-02-04 | 2003-08-19 |   10 |&lt;br /&gt;| Puffball | 1999-03-30 | 2003-08-19 |    4 |&lt;br /&gt;| Slim     | 1996-04-29 | 2003-08-19 |    7 |&lt;br /&gt;| Whistler | 1997-12-09 | 2003-08-19 |    5 |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To sort the output by &lt;code class="literal"&gt;age&lt;/code&gt; rather than           &lt;code class="literal"&gt;name&lt;/code&gt;, just use a different &lt;code class="literal"&gt;ORDER           BY&lt;/code&gt; clause:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth, CURDATE(),&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;(YEAR(CURDATE())-YEAR(birth))&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;- (RIGHT(CURDATE(),5)&lt;right(birth,5))&gt;&lt;/right(birth,5))&gt;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;AS age&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet ORDER BY age;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| name     | birth      | CURDATE()  | age  |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;| Chirpy   | 1998-09-11 | 2003-08-19 |    4 |&lt;br /&gt;| Puffball | 1999-03-30 | 2003-08-19 |    4 |&lt;br /&gt;| Whistler | 1997-12-09 | 2003-08-19 |    5 |&lt;br /&gt;| Slim     | 1996-04-29 | 2003-08-19 |    7 |&lt;br /&gt;| Claws    | 1994-03-17 | 2003-08-19 |    9 |&lt;br /&gt;| Fluffy   | 1993-02-04 | 2003-08-19 |   10 |&lt;br /&gt;| Fang     | 1990-08-27 | 2003-08-19 |   12 |&lt;br /&gt;| Bowser   | 1989-08-31 | 2003-08-19 |   13 |&lt;br /&gt;| Buffy    | 1989-05-13 | 2003-08-19 |   14 |&lt;br /&gt;+----------+------------+------------+------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           A similar query can be used to determine age at death for           animals that have died. You determine which animals these are           by checking whether the &lt;code class="literal"&gt;death&lt;/code&gt; value is           &lt;code class="literal"&gt;NULL&lt;/code&gt;. Then, for those with           non-&lt;code class="literal"&gt;NULL&lt;/code&gt; values, compute the difference           between the &lt;code class="literal"&gt;death&lt;/code&gt; and           &lt;code class="literal"&gt;birth&lt;/code&gt; values:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth, death,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;(YEAR(death)-YEAR(birth)) - (RIGHT(death,5)&lt;right(birth,5))&gt;&lt;/right(birth,5))&gt;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;AS age&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet WHERE death IS NOT NULL ORDER BY age;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+------------+------------+------+&lt;br /&gt;| name   | birth      | death      | age  |&lt;br /&gt;+--------+------------+------------+------+&lt;br /&gt;| Bowser | 1989-08-31 | 1995-07-29 |    5 |&lt;br /&gt;+--------+------------+------------+------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           The query uses &lt;code class="literal"&gt;death IS NOT NULL&lt;/code&gt; rather           than &lt;code class="literal"&gt;death &lt;&gt; NULL&lt;/code&gt; because           &lt;code class="literal"&gt;NULL&lt;/code&gt; is a special value that cannot be           compared using the usual comparison operators. This is           discussed later. See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html" title="3.3.4.6. Working with NULL Values"&gt;Section 3.3.4.6, “Working with &lt;code class="literal"&gt;NULL&lt;/code&gt; Values”&lt;/a&gt;.         &lt;/p&gt; &lt;p&gt;           What if you want to know which animals have birthdays next           month? For this type of calculation, year and day are           irrelevant; you simply want to extract the month part of the           &lt;code class="literal"&gt;birth&lt;/code&gt; column. MySQL provides several           functions for extracting parts of dates, such as           &lt;code class="literal"&gt;YEAR()&lt;/code&gt;, &lt;code class="literal"&gt;MONTH()&lt;/code&gt;, and           &lt;code class="literal"&gt;DAYOFMONTH()&lt;/code&gt;. &lt;code class="literal"&gt;MONTH()&lt;/code&gt; is           the appropriate function here. To see how it works, run a           simple query that displays the value of both           &lt;code class="literal"&gt;birth&lt;/code&gt; and &lt;code class="literal"&gt;MONTH(birth)&lt;/code&gt;:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth, MONTH(birth) FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+------------+--------------+&lt;br /&gt;| name     | birth      | MONTH(birth) |&lt;br /&gt;+----------+------------+--------------+&lt;br /&gt;| Fluffy   | 1993-02-04 |            2 |&lt;br /&gt;| Claws    | 1994-03-17 |            3 |&lt;br /&gt;| Buffy    | 1989-05-13 |            5 |&lt;br /&gt;| Fang     | 1990-08-27 |            8 |&lt;br /&gt;| Bowser   | 1989-08-31 |            8 |&lt;br /&gt;| Chirpy   | 1998-09-11 |            9 |&lt;br /&gt;| Whistler | 1997-12-09 |           12 |&lt;br /&gt;| Slim     | 1996-04-29 |            4 |&lt;br /&gt;| Puffball | 1999-03-30 |            3 |&lt;br /&gt;+----------+------------+--------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Finding animals with birthdays in the upcoming month is also           simple. Suppose that the current month is April. Then the           month value is &lt;code class="literal"&gt;4&lt;/code&gt; and you can look for           animals born in May (month &lt;code class="literal"&gt;5&lt;/code&gt;) like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet WHERE MONTH(birth) = 5;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+------------+&lt;br /&gt;| name  | birth      |&lt;br /&gt;+-------+------------+&lt;br /&gt;| Buffy | 1989-05-13 |&lt;br /&gt;+-------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           There is a small complication if the current month is           December. You cannot merely add one to the month number           (&lt;code class="literal"&gt;12&lt;/code&gt;) and look for animals born in month           &lt;code class="literal"&gt;13&lt;/code&gt;, because there is no such month.           Instead, you look for animals born in January (month           &lt;code class="literal"&gt;1&lt;/code&gt;).         &lt;/p&gt; &lt;p&gt;           You can write the query so that it works no matter what the           current month is, so that you do not have to use the number           for a particular month. &lt;code class="literal"&gt;DATE_ADD()&lt;/code&gt; allows           you to add a time interval to a given date. If you add a month           to the value of &lt;code class="literal"&gt;CURDATE()&lt;/code&gt;, then extract the           month part with &lt;code class="literal"&gt;MONTH()&lt;/code&gt;, the result           produces the month in which to look for birthdays:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;           A different way to accomplish the same task is to add           &lt;code class="literal"&gt;1&lt;/code&gt; to get the next month after the current           one after using the modulo function (&lt;code class="literal"&gt;MOD&lt;/code&gt;)           to wrap the month value to &lt;code class="literal"&gt;0&lt;/code&gt; if it is           currently &lt;code class="literal"&gt;12&lt;/code&gt;:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT name, birth FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;           Note that &lt;code class="literal"&gt;MONTH&lt;/code&gt; returns a number between           &lt;code class="literal"&gt;1&lt;/code&gt; and &lt;code class="literal"&gt;12&lt;/code&gt;. And           &lt;code class="literal"&gt;MOD(something,12)&lt;/code&gt; returns a number between           &lt;code class="literal"&gt;0&lt;/code&gt; and &lt;code class="literal"&gt;11&lt;/code&gt;. So the           addition has to be after the &lt;code class="literal"&gt;MOD()&lt;/code&gt;,           otherwise we would go from November (&lt;code class="literal"&gt;11&lt;/code&gt;) to           January (&lt;code class="literal"&gt;1&lt;/code&gt;).&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.6. Working with &lt;code class="literal"&gt;NULL&lt;/code&gt; Values&lt;/h4&gt; &lt;a class="indexterm" name="id2796463"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2796469"&gt;&lt;/a&gt;&lt;p&gt;           The &lt;code class="literal"&gt;NULL&lt;/code&gt; value can be surprising until you           get used to it. Conceptually, &lt;code class="literal"&gt;NULL&lt;/code&gt; means           “&lt;span class="quote"&gt;a missing unknown value&lt;/span&gt;” and it is treated           somewhat differently from other values. To test for           &lt;code class="literal"&gt;NULL&lt;/code&gt;, you cannot use the arithmetic           comparison operators such as &lt;code class="literal"&gt;=&lt;/code&gt;,           &lt;code class="literal"&gt;&lt;&lt;/code&gt;, or &lt;code class="literal"&gt;&lt;&gt;&lt;/code&gt;. To           demonstrate this for yourself, try the following query:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT 1 = NULL, 1 &lt;&gt; NULL, 1 &lt;&gt; NULL;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+-----------+----------+----------+&lt;br /&gt;| 1 = NULL | 1 &lt;&gt; NULL | 1 &lt;&gt; NULL |&lt;br /&gt;+----------+-----------+----------+----------+&lt;br /&gt;|     NULL |      NULL |     NULL |     NULL |&lt;br /&gt;+----------+-----------+----------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Clearly you get no meaningful results from these comparisons.           Use the &lt;code class="literal"&gt;IS NULL&lt;/code&gt; and &lt;code class="literal"&gt;IS NOT           NULL&lt;/code&gt; operators instead:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT 1 IS NULL, 1 IS NOT NULL;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-----------+---------------+&lt;br /&gt;| 1 IS NULL | 1 IS NOT NULL |&lt;br /&gt;+-----------+---------------+&lt;br /&gt;|         0 |             1 |&lt;br /&gt;+-----------+---------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Note that in MySQL, &lt;code class="literal"&gt;0&lt;/code&gt; or           &lt;code class="literal"&gt;NULL&lt;/code&gt; means false and anything else means           true. The default truth value from a boolean operation is           &lt;code class="literal"&gt;1&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           This special treatment of &lt;code class="literal"&gt;NULL&lt;/code&gt; is why, in           the previous section, it was necessary to determine which           animals are no longer alive using &lt;code class="literal"&gt;death IS NOT           NULL&lt;/code&gt; instead of &lt;code class="literal"&gt;death &lt;&gt;           NULL&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           Two &lt;code class="literal"&gt;NULL&lt;/code&gt; values are regarded as equal in a           &lt;code class="literal"&gt;GROUP BY&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           When doing an &lt;code class="literal"&gt;ORDER BY&lt;/code&gt;,           &lt;code class="literal"&gt;NULL&lt;/code&gt; values are presented first if you do           &lt;code class="literal"&gt;ORDER BY ... ASC&lt;/code&gt; and last if you do           &lt;code class="literal"&gt;ORDER BY ... DESC&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           A common error when working with &lt;code class="literal"&gt;NULL&lt;/code&gt; is to           assume that it is not possible to insert a zero or an empty           string into a column defined as &lt;code class="literal"&gt;NOT NULL&lt;/code&gt;,           but this is not the case. These are in fact values, whereas           &lt;code class="literal"&gt;NULL&lt;/code&gt; means “&lt;span class="quote"&gt;not having a           value.&lt;/span&gt;” You can test this easily enough by using           &lt;code class="literal"&gt;IS &lt;/code&gt;[&lt;code class="literal"&gt;NOT&lt;/code&gt;]&lt;code class="literal"&gt;           NULL&lt;/code&gt; as shown:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-----------+---------------+------------+----------------+&lt;br /&gt;| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |&lt;br /&gt;+-----------+---------------+------------+----------------+&lt;br /&gt;|         0 |             1 |          0 |              1 |&lt;br /&gt;+-----------+---------------+------------+----------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Thus it is entirely possible to insert a zero or empty string           into a &lt;code class="literal"&gt;NOT NULL&lt;/code&gt; column, as these are in           fact &lt;code class="literal"&gt;NOT NULL&lt;/code&gt;. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/problems-with-null.html" title="B.1.5.3. Problems with NULL Values"&gt;Section B.1.5.3, “Problems with &lt;code class="literal"&gt;NULL&lt;/code&gt; Values”&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.7. Pattern Matching&lt;/h4&gt; &lt;a class="indexterm" name="id2796692"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2796699"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2796709"&gt;&lt;/a&gt;&lt;p&gt;           MySQL provides standard SQL pattern matching as well as a form           of pattern matching based on extended regular expressions           similar to those used by Unix utilities such as           &lt;span&gt;&lt;strong class="command"&gt;vi&lt;/strong&gt;&lt;/span&gt;, &lt;span&gt;&lt;strong class="command"&gt;grep&lt;/strong&gt;&lt;/span&gt;, and           &lt;span&gt;&lt;strong class="command"&gt;sed&lt;/strong&gt;&lt;/span&gt;.         &lt;/p&gt; &lt;p&gt;           SQL pattern matching allows you to use           ‘&lt;code class="literal"&gt;_&lt;/code&gt;’ to match any single           character and ‘&lt;code class="literal"&gt;%&lt;/code&gt;’ to match an           arbitrary number of characters (including zero characters). In           MySQL, SQL patterns are case-insensitive by default. Some           examples are shown here. Note that you do not use           &lt;code class="literal"&gt;=&lt;/code&gt; or &lt;code class="literal"&gt;&lt;&gt;&lt;/code&gt; when you           use SQL patterns; use the &lt;code class="literal"&gt;LIKE&lt;/code&gt; or           &lt;code class="literal"&gt;NOT LIKE&lt;/code&gt; comparison operators instead.         &lt;/p&gt; &lt;p&gt;           To find names beginning with           ‘&lt;code class="literal"&gt;b&lt;/code&gt;’:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name LIKE 'b%';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;| name   | owner  | species | sex  | birth      | death      |&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |&lt;br /&gt;| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To find names ending with ‘&lt;code class="literal"&gt;fy&lt;/code&gt;’:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name LIKE '%fy';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;| name   | owner  | species | sex  | birth      | death |&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |&lt;br /&gt;| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To find names containing a ‘&lt;code class="literal"&gt;w&lt;/code&gt;’:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name LIKE '%w%';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;| name     | owner | species | sex  | birth      | death      |&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |&lt;br /&gt;| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |&lt;br /&gt;| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To find names containing exactly five characters, use five           instances of the ‘&lt;code class="literal"&gt;_&lt;/code&gt;’ pattern           character:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name LIKE '_____';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| name  | owner  | species | sex  | birth      | death |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |&lt;br /&gt;| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           The other type of pattern matching provided by MySQL uses           extended regular expressions. When you test for a match for           this type of pattern, use the &lt;code class="literal"&gt;REGEXP&lt;/code&gt; and           &lt;code class="literal"&gt;NOT REGEXP&lt;/code&gt; operators (or           &lt;code class="literal"&gt;RLIKE&lt;/code&gt; and &lt;code class="literal"&gt;NOT RLIKE&lt;/code&gt;,           which are synonyms).         &lt;/p&gt; &lt;p&gt;           Some characteristics of extended regular expressions are:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               ‘&lt;code class="literal"&gt;.&lt;/code&gt;’ matches any single               character.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               A character class ‘&lt;code class="literal"&gt;[...]&lt;/code&gt;’               matches any character within the brackets. For example,               ‘&lt;code class="literal"&gt;[abc]&lt;/code&gt;’ matches               ‘&lt;code class="literal"&gt;a&lt;/code&gt;’,               ‘&lt;code class="literal"&gt;b&lt;/code&gt;’, or               ‘&lt;code class="literal"&gt;c&lt;/code&gt;’. To name a range of               characters, use a dash.               ‘&lt;code class="literal"&gt;[a-z]&lt;/code&gt;’ matches any letter,               whereas ‘&lt;code class="literal"&gt;[0-9]&lt;/code&gt;’ matches any               digit.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               ‘&lt;code class="literal"&gt;*&lt;/code&gt;’ matches zero or more               instances of the thing preceding it. For example,               ‘&lt;code class="literal"&gt;x*&lt;/code&gt;’ matches any number of               ‘&lt;code class="literal"&gt;x&lt;/code&gt;’ characters,               ‘&lt;code class="literal"&gt;[0-9]*&lt;/code&gt;’ matches any number               of digits, and ‘&lt;code class="literal"&gt;.*&lt;/code&gt;’ matches               any number of anything.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               A &lt;code class="literal"&gt;REGEXP&lt;/code&gt; pattern match succeeds if the               pattern matches anywhere in the value being tested. (This               differs from a &lt;code class="literal"&gt;LIKE&lt;/code&gt; pattern match,               which succeeds only if the pattern matches the entire               value.)             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               To anchor a pattern so that it must match the beginning or               end of the value being tested, use               ‘&lt;code class="literal"&gt;^&lt;/code&gt;’ at the beginning or               ‘&lt;code class="literal"&gt;$&lt;/code&gt;’ at the end of the               pattern.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;           To demonstrate how extended regular expressions work, the           &lt;code class="literal"&gt;LIKE&lt;/code&gt; queries shown previously are rewritten           here to use &lt;code class="literal"&gt;REGEXP&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           To find names beginning with           ‘&lt;code class="literal"&gt;b&lt;/code&gt;’, use           ‘&lt;code class="literal"&gt;^&lt;/code&gt;’ to match the beginning of           the name:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP '^b';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;| name   | owner  | species | sex  | birth      | death      |&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |&lt;br /&gt;| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |&lt;br /&gt;+--------+--------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           If you really want to force a &lt;code class="literal"&gt;REGEXP&lt;/code&gt;           comparison to be case sensitive, use the           &lt;code class="literal"&gt;BINARY&lt;/code&gt; keyword to make one of the strings a           binary string. This query matches only lowercase           ‘&lt;code class="literal"&gt;b&lt;/code&gt;’ at the beginning of a name:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP BINARY '^b';&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;           To find names ending with ‘&lt;code class="literal"&gt;fy&lt;/code&gt;’,           use ‘&lt;code class="literal"&gt;$&lt;/code&gt;’ to match the end of the           name:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP 'fy$';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;| name   | owner  | species | sex  | birth      | death |&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |&lt;br /&gt;| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+--------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           To find names containing a ‘&lt;code class="literal"&gt;w&lt;/code&gt;’,           use this query:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP 'w';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;| name     | owner | species | sex  | birth      | death      |&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |&lt;br /&gt;| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |&lt;br /&gt;| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |&lt;br /&gt;+----------+-------+---------+------+------------+------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Because a regular expression pattern matches if it occurs           anywhere in the value, it is not necessary in the previous           query to put a wildcard on either side of the pattern to get           it to match the entire value like it would be if you used an           SQL pattern.         &lt;/p&gt; &lt;p&gt;           To find names containing exactly five characters, use           ‘&lt;code class="literal"&gt;^&lt;/code&gt;’ and           ‘&lt;code class="literal"&gt;$&lt;/code&gt;’ to match the beginning and           end of the name, and five instances of           ‘&lt;code class="literal"&gt;.&lt;/code&gt;’ in between:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP '^.....$';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| name  | owner  | species | sex  | birth      | death |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |&lt;br /&gt;| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           You could also write the previous query using the           &lt;code class="literal"&gt;{&lt;em class="replaceable"&gt;&lt;code&gt;n&lt;/code&gt;&lt;/em&gt;}&lt;/code&gt;           (“&lt;span class="quote"&gt;repeat-&lt;em class="replaceable"&gt;&lt;code&gt;n&lt;/code&gt;&lt;/em&gt;-times&lt;/span&gt;”)           operator:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM pet WHERE name REGEXP '^.{5}$';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| name  | owner  | species | sex  | birth      | death |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |&lt;br /&gt;| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |&lt;br /&gt;+-------+--------+---------+------+------------+-------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/regexp.html" title="12.4.2. Regular Expressions"&gt;Section 12.4.2, “Regular Expressions”&lt;/a&gt;, provides more information about the           syntax for regular expressions.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;3.3.4.8. Counting Rows&lt;/h4&gt; &lt;a class="indexterm" name="id2797218"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2797229"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2797239"&gt;&lt;/a&gt;&lt;p&gt;           Databases are often used to answer the question, “&lt;span class="quote"&gt;How           often does a certain type of data occur in a table?&lt;/span&gt;”           For example, you might want to know how many pets you have, or           how many pets each owner has, or you might want to perform           various kinds of census operations on your animals.         &lt;/p&gt; &lt;p&gt;           Counting the total number of animals you have is the same           question as “&lt;span class="quote"&gt;How many rows are in the           &lt;code class="literal"&gt;pet&lt;/code&gt; table?&lt;/span&gt;” because there is one           record per pet. &lt;code class="literal"&gt;COUNT(*)&lt;/code&gt; counts the number           of rows, so the query to count your animals looks like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT COUNT(*) FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------+&lt;br /&gt;| COUNT(*) |&lt;br /&gt;+----------+&lt;br /&gt;|        9 |&lt;br /&gt;+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Earlier, you retrieved the names of the people who owned pets.           You can use &lt;code class="literal"&gt;COUNT()&lt;/code&gt; if you want to find out           how many pets each owner has:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT owner, COUNT(*) FROM pet GROUP BY owner;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+----------+&lt;br /&gt;| owner  | COUNT(*) |&lt;br /&gt;+--------+----------+&lt;br /&gt;| Benny  |        2 |&lt;br /&gt;| Diane  |        2 |&lt;br /&gt;| Gwen   |        3 |&lt;br /&gt;| Harold |        2 |&lt;br /&gt;+--------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Note the use of &lt;code class="literal"&gt;GROUP BY&lt;/code&gt; to group all           records for each &lt;code class="literal"&gt;owner&lt;/code&gt;. Without it, all you           get is an error message:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT owner, COUNT(*) FROM pet;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...)&lt;br /&gt;with no GROUP columns is illegal if there is no GROUP BY clause&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           &lt;code class="literal"&gt;COUNT()&lt;/code&gt; and &lt;code class="literal"&gt;GROUP BY&lt;/code&gt; are           useful for characterizing your data in various ways. The           following examples show different ways to perform animal           census operations.         &lt;/p&gt; &lt;p&gt;           Number of animals per species:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT species, COUNT(*) FROM pet GROUP BY species;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------+----------+&lt;br /&gt;| species | COUNT(*) |&lt;br /&gt;+---------+----------+&lt;br /&gt;| bird    |        2 |&lt;br /&gt;| cat     |        2 |&lt;br /&gt;| dog     |        3 |&lt;br /&gt;| hamster |        1 |&lt;br /&gt;| snake   |        1 |&lt;br /&gt;+---------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Number of animals per sex:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT sex, COUNT(*) FROM pet GROUP BY sex;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+------+----------+&lt;br /&gt;| sex  | COUNT(*) |&lt;br /&gt;+------+----------+&lt;br /&gt;| NULL |        1 |&lt;br /&gt;| f    |        4 |&lt;br /&gt;| m    |        4 |&lt;br /&gt;+------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           (In this output, &lt;code class="literal"&gt;NULL&lt;/code&gt; indicates that the           sex is unknown.)         &lt;/p&gt; &lt;p&gt;           Number of animals per combination of species and sex:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| species | sex  | COUNT(*) |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| bird    | NULL |        1 |&lt;br /&gt;| bird    | f    |        1 |&lt;br /&gt;| cat     | f    |        1 |&lt;br /&gt;| cat     | m    |        1 |&lt;br /&gt;| dog     | f    |        1 |&lt;br /&gt;| dog     | m    |        2 |&lt;br /&gt;| hamster | f    |        1 |&lt;br /&gt;| snake   | m    |        1 |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           You need not retrieve an entire table when you use           &lt;code class="literal"&gt;COUNT()&lt;/code&gt;. For example, the previous query,           when performed just on dogs and cats, looks like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT species, sex, COUNT(*) FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE species = 'dog' OR species = 'cat'&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;GROUP BY species, sex;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| species | sex  | COUNT(*) |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| cat     | f    |        1 |&lt;br /&gt;| cat     | m    |        1 |&lt;br /&gt;| dog     | f    |        1 |&lt;br /&gt;| dog     | m    |        2 |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           Or, if you wanted the number of animals per sex only for           animals whose sex is known:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT species, sex, COUNT(*) FROM pet&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE sex IS NOT NULL&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt; -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;GROUP BY species, sex;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| species | sex  | COUNT(*) |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;| bird    | f    |        1 |&lt;br /&gt;| cat     | f    |        1 |&lt;br /&gt;| cat     | m    |        1 |&lt;br /&gt;| dog     | f    |        1 |&lt;br /&gt;| dog     | m    |        2 |&lt;br /&gt;| hamster | f    |        1 |&lt;br /&gt;| snake   | m    |        1 |&lt;br /&gt;+---------+------+----------+&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="title"&gt;3.3.4.9. Using More Than one Table&lt;/h4&gt; &lt;a class="indexterm" name="id2797474"&gt;&lt;/a&gt;&lt;p&gt;           The &lt;code class="literal"&gt;pet&lt;/code&gt; table keeps track of which pets you           have. If you want to record other information about them, such           as events in their lives like visits to the vet or when           litters are born, you need another table. What should this           table look like? It needs:&lt;/p&gt;&lt;br /&gt;&lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               To contain the pet name so that you know which animal each               event pertains to.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               A date so that you know when the event occurred.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               A field to describe the event.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               An event type field, if you want to be able to categorize               events.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;           Given these considerations, the &lt;code class="literal"&gt;CREATE           TABLE&lt;/code&gt; statement for the &lt;code class="literal"&gt;event&lt;/code&gt;           table might look like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;CREATE TABLE event (name VARCHAR(20), date DATE,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;type VARCHAR(15), remark VARCHAR(255));&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;           As with the &lt;code class="literal"&gt;pet&lt;/code&gt; table, it's easiest to load           the initial records by creating a tab-delimited text file           containing the information:         &lt;/p&gt; &lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;name&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;date&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;type&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;remark&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fluffy&lt;/td&gt; &lt;td&gt;1995-05-15&lt;/td&gt; &lt;td&gt;litter&lt;/td&gt; &lt;td&gt;4 kittens, 3 female, 1 male&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Buffy&lt;/td&gt; &lt;td&gt;1993-06-23&lt;/td&gt; &lt;td&gt;litter&lt;/td&gt; &lt;td&gt;5 puppies, 2 female, 3 male&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Buffy&lt;/td&gt; &lt;td&gt;1994-06-19&lt;/td&gt; &lt;td&gt;litter&lt;/td&gt; &lt;td&gt;3 puppies, 3 female&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Chirpy&lt;/td&gt; &lt;td&gt;1999-03-21&lt;/td&gt; &lt;td&gt;vet&lt;/td&gt; &lt;td&gt;needed beak straightened&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Slim&lt;/td&gt; &lt;td&gt;1997-08-03&lt;/td&gt; &lt;td&gt;vet&lt;/td&gt; &lt;td&gt;broken rib&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Bowser&lt;/td&gt; &lt;td&gt;1991-10-12&lt;/td&gt; &lt;td&gt;kennel&lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fang&lt;/td&gt; &lt;td&gt;1991-10-12&lt;/td&gt; &lt;td&gt;kennel&lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Fang&lt;/td&gt; &lt;td&gt;1998-08-28&lt;/td&gt; &lt;td&gt;birthday&lt;/td&gt; &lt;td&gt;Gave him a new chew toy&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Claws&lt;/td&gt; &lt;td&gt;1998-03-17&lt;/td&gt; &lt;td&gt;birthday&lt;/td&gt; &lt;td&gt;Gave him a new flea collar&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Whistler&lt;/td&gt; &lt;td&gt;1998-12-09&lt;/td&gt; &lt;td&gt;birthday&lt;/td&gt; &lt;td&gt;First birthday&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;           Load the records like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;LOAD DATA LOCAL INFILE 'event.txt' INTO TABLE event;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;           Based on what you have learned from the queries that you have           run on the &lt;code class="literal"&gt;pet&lt;/code&gt; table, you should be able to           perform retrievals on the records in the           &lt;code class="literal"&gt;event&lt;/code&gt; table; the principles are the same.           But when is the &lt;code class="literal"&gt;event&lt;/code&gt; table by itself           insufficient to answer questions you might ask?         &lt;/p&gt; &lt;p&gt;           Suppose that you want to find out the ages at which each pet           had its litters. We saw earlier how to calculate ages from two           dates. The litter date of the mother is in the           &lt;code class="literal"&gt;event&lt;/code&gt; table, but to calculate her age on           that date you need her birth date, which is stored in the           &lt;code class="literal"&gt;pet&lt;/code&gt; table. This means the query requires           both tables:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT pet.name,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;(YEAR(date)-YEAR(birth)) - (RIGHT(date,5)&lt;right(birth,5))&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;remark&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet INNER JOIN event&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;  ON pet.name = event.name&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;WHERE event.type = 'litter';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+------+-----------------------------+&lt;br /&gt;| name   | age  | remark                      |&lt;br /&gt;+--------+------+-----------------------------+&lt;br /&gt;| Fluffy |    2 | 4 kittens, 3 female, 1 male |&lt;br /&gt;| Buffy  |    4 | 5 puppies, 2 female, 3 male |&lt;br /&gt;| Buffy  |    5 | 3 puppies, 3 female         |&lt;br /&gt;+--------+------+-----------------------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           There are several things to note about this query:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               The &lt;code class="literal"&gt;FROM&lt;/code&gt; clause joins two tables               because the query needs to pull information from both of               them.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;p&gt;               When combining (joining) information from multiple tables,               you need to specify how records in one table can be               matched to records in the other. This is easy because they               both have a &lt;code class="literal"&gt;name&lt;/code&gt; column. The query uses               &lt;code class="literal"&gt;ON&lt;/code&gt; clause to match up records in the               two tables based on the &lt;code class="literal"&gt;name&lt;/code&gt; values.             &lt;/p&gt; &lt;p&gt;               The query uses an &lt;code class="literal"&gt;INNER JOIN&lt;/code&gt; to combine               the tables. An &lt;code class="literal"&gt;INNER JOIN&lt;/code&gt; allows for               rows from either table to appear in the result if and only               if both tables meet the conditions specified in the               &lt;code class="option"&gt;ON&lt;/code&gt; clause. In this example, the               &lt;code class="literal"&gt;ON&lt;/code&gt; clause specifies that the               &lt;code class="literal"&gt;name&lt;/code&gt; column in the               &lt;code class="literal"&gt;pet&lt;/code&gt; table must match the               &lt;code class="literal"&gt;name&lt;/code&gt; column in the               &lt;code class="literal"&gt;event&lt;/code&gt; table. If a name appears in one               table but not the other, the row will not appear in the               result because the condition in the &lt;code class="literal"&gt;ON&lt;/code&gt;               clause fails.             &lt;/p&gt; &lt;/li&gt;&lt;li&gt;&lt;p&gt;               Because the &lt;code class="literal"&gt;name&lt;/code&gt; column occurs in both               tables, you must be specific about which table you mean               when referring to the column. This is done by prepending               the table name to the column name.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;           You need not have two different tables to perform a join.           Sometimes it is useful to join a table to itself, if you want           to compare records in a table to other records in that same           table. For example, to find breeding pairs among your pets,           you can join the &lt;code class="literal"&gt;pet&lt;/code&gt; table with itself to           produce candidate pairs of males and females of like species:         &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;FROM pet AS p1 INNER JOIN pet AS p2&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;  ON p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+--------+------+--------+------+---------+&lt;br /&gt;| name   | sex  | name   | sex  | species |&lt;br /&gt;+--------+------+--------+------+---------+&lt;br /&gt;| Fluffy | f    | Claws  | m    | cat     |&lt;br /&gt;| Buffy  | f    | Fang   | m    | dog     |&lt;br /&gt;| Buffy  | f    | Bowser | m    | dog     |&lt;br /&gt;+--------+------+--------+------+---------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           In this query, we specify aliases for the table name to refer           to the columns and keep straight which instance of the table           each column reference is associated with.         &lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;pre class="programlisting"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre class="programlisting"&gt;&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-4916534054440222903?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4916534054440222903'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/4916534054440222903'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/33-creating-and-using-database.html' title='3.3. Creating and Using a Database'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-8379197131388230096</id><published>2007-04-02T21:06:00.000-07:00</published><updated>2007-04-02T21:22:09.703-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.2. Entering Queries</title><content type='html'>&lt;p&gt;       Make sure that you are connected to the server, as discussed in       the previous section. Doing so does not in itself select any       database to work with, but that's okay. At this point, it's more       important to find out a little about how to issue queries than to       jump right in creating tables, loading data into them, and       retrieving data from them. This section describes the basic       principles of entering commands, using several queries you can try       out to familiarize yourself with how &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       works.     &lt;/p&gt; &lt;p&gt;       Here's a simple command that asks the server to tell you its       version number and the current date. Type it in as shown here       following the &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; prompt and press Enter:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT VERSION(), CURRENT_DATE;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------------+--------------+&lt;br /&gt;| VERSION()      | CURRENT_DATE |&lt;br /&gt;+----------------+--------------+&lt;br /&gt;| 5.0.7-beta-Max | 2005-07-11   |&lt;br /&gt;+----------------+--------------+&lt;br /&gt;1 row in set (0.01 sec)&lt;br /&gt;mysql&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       This query illustrates several things about       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           A command normally consists of an SQL statement followed by a           semicolon. (There are some exceptions where a semicolon may be           omitted. &lt;code class="literal"&gt;QUIT&lt;/code&gt;, mentioned earlier, is one of           them. We'll get to others later.)         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           When you issue a command, &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; sends it to           the server for execution and displays the results, then prints           another &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; prompt to indicate that           it is ready for another command.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; displays query output in tabular form           (rows and columns). The first row contains labels for the           columns. The rows following are the query results. Normally,           column labels are the names of the columns you fetch from           database tables. If you're retrieving the value of an           expression rather than a table column (as in the example just           shown), &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; labels the column using the           expression itself.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; shows how many rows were returned and           how long the query took to execute, which gives you a rough           idea of server performance. These values are imprecise because           they represent wall clock time (not CPU or machine time), and           because they are affected by factors such as server load and           network latency. (For brevity, the “&lt;span class="quote"&gt;rows in set&lt;/span&gt;”           line is sometimes not shown in the remaining examples in this           chapter.)         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       Keywords may be entered in any lettercase. The following queries       are equivalent:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT VERSION(), CURRENT_DATE;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;select version(), current_date;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SeLeCt vErSiOn(), current_DATE;&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       Here's another query. It demonstrates that you can use       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; as a simple calculator:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT SIN(PI()/4), (4+1)*5;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+------------------+---------+&lt;br /&gt;| SIN(PI()/4)      | (4+1)*5 |&lt;br /&gt;+------------------+---------+&lt;br /&gt;| 0.70710678118655 |      25 |&lt;br /&gt;+------------------+---------+&lt;br /&gt;1 row in set (0.02 sec)&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The queries shown thus far have been relatively short, single-line       statements. You can even enter multiple statements on a single       line. Just end each one with a semicolon:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT VERSION(); SELECT NOW();&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+----------------+&lt;br /&gt;| VERSION()      |&lt;br /&gt;+----------------+&lt;br /&gt;| 5.0.7-beta-Max |&lt;br /&gt;+----------------+&lt;br /&gt;1 row in set (0.00 sec)&lt;br /&gt;&lt;br /&gt;+---------------------+&lt;br /&gt;| NOW()               |&lt;br /&gt;+---------------------+&lt;br /&gt;| 2005-07-11 17:59:36 |&lt;br /&gt;+---------------------+ &lt;br /&gt;1 row in set (0.00 sec)&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       A command need not be given all on a single line, so lengthy       commands that require several lines are not a problem.       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; determines where your statement ends by       looking for the terminating semicolon, not by looking for the end       of the input line. (In other words, &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       accepts free-format input: it collects input lines but does not       execute them until it sees the semicolon.)     &lt;/p&gt; &lt;p&gt;       Here's a simple multiple-line statement:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;USER()&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;CURRENT_DATE;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------------+--------------+&lt;br /&gt;| USER()        | CURRENT_DATE |&lt;br /&gt;+---------------+--------------+&lt;br /&gt;| jon@localhost | 2005-07-11   |&lt;br /&gt;+---------------+--------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       In this example, notice how the prompt changes from       &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; to &lt;code class="literal"&gt;-&gt;&lt;/code&gt; after you       enter the first line of a multiple-line query. This is how       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; indicates that it has not yet seen a       complete statement and is waiting for the rest. The prompt is your       friend, because it provides valuable feedback. If you use that       feedback, you can always be aware of what &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       is waiting for.     &lt;/p&gt; &lt;p&gt;       If you decide you do not want to execute a command that you are in       the process of entering, cancel it by typing       &lt;code class="literal"&gt;\c&lt;/code&gt;:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;USER()&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;\c&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       Here, too, notice the prompt. It switches back to       &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; after you type &lt;code class="literal"&gt;\c&lt;/code&gt;,       providing feedback to indicate that &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is       ready for a new command.     &lt;/p&gt; &lt;p&gt;       The following table shows each of the prompts you may see and       summarizes what they mean about the state that       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is in:     &lt;/p&gt; &lt;a class="indexterm" name="id2793594"&gt;&lt;/a&gt;&lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Prompt&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Meaning&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;mysql&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Ready for new command.&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;-&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Waiting for next line of multiple-line command.&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;'&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Waiting for next line, waiting for completion of a string that began               with a single quote (‘&lt;code class="literal"&gt;'&lt;/code&gt;’).&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;"&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Waiting for next line, waiting for completion of a string that began               with a double quote (‘&lt;code class="literal"&gt;"&lt;/code&gt;’).&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;`&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Waiting for next line, waiting for completion of an identifier that               began with a backtick               (‘&lt;code class="literal"&gt;`&lt;/code&gt;’).&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;code class="literal"&gt;/*&gt;&lt;/code&gt;&lt;/td&gt; &lt;td&gt;Waiting for next line, waiting for completion of a comment that began               with &lt;code class="literal"&gt;/*&lt;/code&gt;.&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;       In the MySQL 5.0 series, the &lt;code class="literal"&gt;/*&gt;&lt;/code&gt; prompt was       implemented in MySQL 5.0.6.     &lt;/p&gt; &lt;p&gt;       Multiple-line statements commonly occur by accident when you       intend to issue a command on a single line, but forget the       terminating semicolon. In this case, &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       waits for more input:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT USER()&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       If this happens to you (you think you've entered a statement but       the only response is a &lt;code class="literal"&gt;-&gt;&lt;/code&gt; prompt), most       likely &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is waiting for the semicolon. If       you don't notice what the prompt is telling you, you might sit       there for a while before realizing what you need to do. Enter a       semicolon to complete the statement, and &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;       executes it:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT USER()&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;   -&gt; &lt;strong class="userinput"&gt;&lt;code&gt;;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;+---------------+&lt;br /&gt;| USER()        |&lt;br /&gt;+---------------+&lt;br /&gt;| jon@localhost |&lt;br /&gt;+---------------+&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The &lt;code class="literal"&gt;'&gt;&lt;/code&gt; and &lt;code class="literal"&gt;"&gt;&lt;/code&gt; prompts       occur during string collection (another way of saying that MySQL       is waiting for completion of a string). In MySQL, you can write       strings surrounded by either ‘&lt;code class="literal"&gt;'&lt;/code&gt;’ or       ‘&lt;code class="literal"&gt;"&lt;/code&gt;’ characters (for example,       &lt;code class="literal"&gt;'hello'&lt;/code&gt; or &lt;code class="literal"&gt;"goodbye"&lt;/code&gt;), and       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; lets you enter strings that span multiple       lines. When you see a &lt;code class="literal"&gt;'&gt;&lt;/code&gt; or       &lt;code class="literal"&gt;"&gt;&lt;/code&gt; prompt, it means that you have entered a       line containing a string that begins with a       ‘&lt;code class="literal"&gt;'&lt;/code&gt;’ or       ‘&lt;code class="literal"&gt;"&lt;/code&gt;’ quote character, but have not       yet entered the matching quote that terminates the string. This       often indicates that you have inadvertently left out a quote       character. For example:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM my_table WHERE name = 'Smith AND age &lt;&gt;&lt;/strong&gt;&lt;br /&gt;   '&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       If you enter this &lt;code class="literal"&gt;SELECT&lt;/code&gt; statement, then press       &lt;span&gt;&lt;strong class="keycap"&gt;Enter&lt;/strong&gt;&lt;/span&gt; and wait for the result, nothing happens.       Instead of wondering why this query takes so long, notice the clue       provided by the &lt;code class="literal"&gt;'&gt;&lt;/code&gt; prompt. It tells you that       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; expects to see the rest of an       unterminated string. (Do you see the error in the statement? The       string &lt;code class="literal"&gt;'Smith&lt;/code&gt; is missing the second single       quote mark.)     &lt;/p&gt; &lt;p&gt;       At this point, what do you do? The simplest thing is to cancel the       command. However, you cannot just type &lt;code class="literal"&gt;\c&lt;/code&gt; in       this case, because &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; interprets it as part       of the string that it is collecting. Instead, enter the closing       quote character (so &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; knows you've finished       the string), then type &lt;code class="literal"&gt;\c&lt;/code&gt;:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;SELECT * FROM my_table WHERE name = 'Smith AND age &lt;&gt;&lt;/strong&gt;&lt;br /&gt;   '&gt; &lt;strong class="userinput"&gt;&lt;code&gt;'\c&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;mysql&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The prompt changes back to &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt;,       indicating that &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is ready for a new       command.     &lt;/p&gt; &lt;p&gt;       The &lt;code class="literal"&gt;`&gt;&lt;/code&gt; prompt is similar to the       &lt;code class="literal"&gt;'&gt;&lt;/code&gt; and &lt;code class="literal"&gt;"&gt;&lt;/code&gt; prompts, but       indicates that you have begun but not completed a backtick-quoted       identifier.     &lt;/p&gt; &lt;p&gt;       It is important to know what the &lt;code class="literal"&gt;'&gt;&lt;/code&gt;,       &lt;code class="literal"&gt;"&gt;&lt;/code&gt;, and &lt;code class="literal"&gt;`&gt;&lt;/code&gt; prompts       signify, because if you mistakenly enter an unterminated string,       any further lines you type appear to be ignored by       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; — including a line containing       &lt;code class="literal"&gt;QUIT&lt;/code&gt;. This can be quite confusing, especially       if you do not know that you need to supply the terminating quote       before you can cancel the current command.     &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-8379197131388230096?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/8379197131388230096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=8379197131388230096' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/8379197131388230096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/8379197131388230096'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/32-entering-queries.html' title='3.2. Entering Queries'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-98682433274358330</id><published>2007-04-02T21:01:00.000-07:00</published><updated>2007-04-02T21:05:49.663-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>3.1. Connecting to and Disconnecting from the Server</title><content type='html'>&lt;p&gt;       To connect to the server, you will usually need to provide a MySQL       user name when you invoke &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; and, most       likely, a password. If the server runs on a machine other than the       one where you log in, you will also need to specify a host name.       Contact your administrator to find out what connection parameters       you should use to connect (that is, what host, user name, and       password to use). Once you know the proper parameters, you should       be able to connect like this:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -h &lt;em class="replaceable"&gt;&lt;code&gt;host&lt;/code&gt;&lt;/em&gt; -u &lt;em class="replaceable"&gt;&lt;code&gt;user&lt;/code&gt;&lt;/em&gt; -p&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;********&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       &lt;code class="literal"&gt;host&lt;/code&gt; and &lt;code class="literal"&gt;user&lt;/code&gt; represent the       host name where your MySQL server is running and the user name of       your MySQL account. Substitute appropriate values for your setup.       The &lt;code class="literal"&gt;********&lt;/code&gt; represents your password; enter it       when &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; displays the &lt;code class="literal"&gt;Enter       password:&lt;/code&gt; prompt.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;       If that works, you should see some introductory information       followed by a &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; prompt:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -h &lt;em class="replaceable"&gt;&lt;code&gt;host&lt;/code&gt;&lt;/em&gt; -u &lt;em class="replaceable"&gt;&lt;code&gt;user&lt;/code&gt;&lt;/em&gt; -p&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Enter password: &lt;strong class="userinput"&gt;&lt;code&gt;********&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Welcome to the MySQL monitor.  Commands end with ; or \g.&lt;br /&gt;Your MySQL connection id is 25338 to server version: 5.0.38-standard&lt;br /&gt;&lt;br /&gt;Type 'help;' or '\h' for help. Type '\c' to clear the buffer.&lt;br /&gt;&lt;br /&gt;mysql&gt;&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       The &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; prompt tells you that       &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; is ready for you to enter commands.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;       If you are logging in on the same machine that MySQL is running       on, you can omit the host, and simply use the following:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql -u &lt;em class="replaceable"&gt;&lt;code&gt;user&lt;/code&gt;&lt;/em&gt; -p&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       If, when you attempt to log in, you get an error message such as       &lt;span class="errortext"&gt;ERROR 2002 (HY000): Can't connect to local MySQL server       through socket '/tmp/mysql.sock' (2)&lt;/span&gt;, it means that       that MySQL server daemon (Unix) or service (Windows) is not       running. Consult the administrator or see the section of       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/installing.html" title="Chapter 2. Installing and Upgrading MySQL"&gt;Chapter 2, &lt;i&gt;Installing and Upgrading MySQL&lt;/i&gt;&lt;/a&gt; that is appropriate to your operating       system.     &lt;/p&gt; &lt;p&gt;       For help with other problems often encountered when trying to log       in, see &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/common-errors.html" title="B.1.2. Common Errors When Using MySQL Programs"&gt;Section B.1.2, “Common Errors When Using MySQL Programs”&lt;/a&gt;.     &lt;/p&gt; &lt;p&gt;       Some MySQL installations allow users to connect as the anonymous       (unnamed) user to the server running on the local host. If this is       the case on your machine, you should be able to connect to that       server by invoking &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; without any options:     &lt;/p&gt; &lt;pre class="programlisting"&gt;shell&gt; &lt;strong class="userinput"&gt;&lt;code&gt;mysql&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;       After you have connected successfully, you can disconnect any time       by typing &lt;code class="literal"&gt;QUIT&lt;/code&gt; (or &lt;code class="literal"&gt;\q&lt;/code&gt;) at       the &lt;code class="literal"&gt;mysql&gt;&lt;/code&gt; prompt:     &lt;/p&gt; &lt;pre class="programlisting"&gt;mysql&gt; &lt;strong class="userinput"&gt;&lt;code&gt;QUIT&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;Bye&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;       On Unix, you can also disconnect by pressing Control-D.     &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1888076610204930038-98682433274358330?l=dieselms.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dieselms.blogspot.com/feeds/98682433274358330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1888076610204930038&amp;postID=98682433274358330' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/98682433274358330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1888076610204930038/posts/default/98682433274358330'/><link rel='alternate' type='text/html' href='http://dieselms.blogspot.com/2007/04/31-connecting-to-and-disconnecting-from.html' title='3.1. Connecting to and Disconnecting from the Server'/><author><name>wzy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://photos1.blogger.com/img/15/7277/640/hallow17.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1888076610204930038.post-1429690738753878108</id><published>2007-04-01T20:00:00.000-07:00</published><updated>2007-04-01T20:10:51.555-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dev.mysql'/><title type='text'>2.4.8. Installing MySQL on Windows</title><content type='html'>&lt;p&gt;       A native Windows distribution of MySQL has been available from       MySQL AB since version 3.21 and represents a sizable percentage of       the daily downloads of MySQL. This section describes the process       for installing MySQL on Windows.     &lt;/p&gt; &lt;p&gt;       &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: If you are upgrading MySQL       from an existing installation older than MySQL 4.1.5, you must       first perform the procedure described in       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-upgrading.html" title="2.4.8.14. Upgrading MySQL on Windows"&gt;Section 2.4.8.14, “Upgrading MySQL on Windows”&lt;/a&gt;.     &lt;/p&gt; &lt;p&gt;       To run MySQL on Windows, you need the following:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt; &lt;p&gt;           A 32-bit Windows operating system such as 9x, Me, NT, 2000,           XP, or Windows Server 2003.         &lt;/p&gt; &lt;p&gt;           A Windows NT-based operating system (NT, 2000, XP, 2003)           permits you to run the MySQL server as a service. The use of a           Windows NT-based operating system is strongly recommended. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-start-service.html" title="2.4.8.11. Starting MySQL as a Windows Service"&gt;Section 2.4.8.11, “Starting MySQL as a Windows Service”&lt;/a&gt;.         &lt;/p&gt; &lt;p&gt;           Generally, you should install MySQL on Windows using an           account that has administrator rights. Otherwise, you may           encounter problems with certain operations such as editing the           &lt;code class="literal"&gt;PATH&lt;/code&gt; environment variable or accessing the           &lt;span&gt;&lt;strong class="command"&gt;Service Control Manager&lt;/strong&gt;&lt;/span&gt;.         &lt;/p&gt; &lt;/li&gt;&lt;li&gt;&lt;p&gt;           TCP/IP protocol support.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           Enough space on the hard drive to unpack, install, and create           the databases in accordance with your requirements (generally           a minimum of 200 megabytes is recommended.)         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       There may also be other requirements, depending on how you plan to       use MySQL:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           If you plan to connect to the MySQL server via ODBC, you need           a Connector/ODBC driver. See &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/connectors.html" title="Chapter 23. Connectors"&gt;Chapter 23, &lt;i&gt;Connectors&lt;/i&gt;&lt;/a&gt;.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           If you need tables with a size larger than 4GB, install MySQL           on an NTFS or newer filesystem. Don't forget to use           &lt;code class="literal"&gt;MAX_ROWS&lt;/code&gt; and           &lt;code class="literal"&gt;AVG_ROW_LENGTH&lt;/code&gt; when you create tables. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/create-table.html" title="13.1.5. CREATE TABLE Syntax"&gt;Section 13.1.5, “&lt;code class="literal"&gt;CREATE TABLE&lt;/code&gt; Syntax”&lt;/a&gt;.         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       MySQL for Windows is available in several distribution formats:     &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;           Binary distributions are available that contain a setup           program that installs everything you need so that you can           start the server immediately. Another binary distribution           format contains an archive that you simply unpack in the           installation location and then configure yourself. For           details, see &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-choosing-package.html" title="2.4.8.1. Choosing An Installation Package"&gt;Section 2.4.8.1, “Choosing An Installation Package”&lt;/a&gt;.         &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;           The source distribution contains all the code and support           files for building the executables using the Visual Studio           compiler system.         &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;       Generally speaking, you should use a binary distribution that       includes an installer. It is simpler to use than the others, and       you need no additional tools to get MySQL up and running. The       installer for the Windows version of MySQL, combined with a GUI       Configuration Wizard, automatically installs MySQL, creates an       option file, starts the server, and secures the default user       accounts.     &lt;/p&gt; &lt;p&gt;       The following section describes how to install MySQL on Windows       using a binary distribution. To use an installation package that       does not include an installer, follow the procedure described in       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-install-archive.html" title="2.4.8.5. Installing MySQL from a Noinstall Zip Archive"&gt;Section 2.4.8.5, “Installing MySQL from a Noinstall Zip Archive”&lt;/a&gt;. To install using a       source distribution, see &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-source-build.html" title="2.4.14.6. Installing MySQL from Source on Windows"&gt;Section 2.4.14.6, “Installing MySQL from Source on Windows”&lt;/a&gt;.     &lt;/p&gt; &lt;p&gt;       MySQL distributions for Windows can be downloaded from       &lt;a href="http://dev.mysql.com/downloads/" target="_top"&gt;http://dev.mysql.com/downloads/&lt;/a&gt;. See       &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/getting-mysql.html" title="2.4.4. How to Get MySQL"&gt;Section 2.4.4, “How to Get MySQL”&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.1. Choosing An Installation Package&lt;/h4&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;         For MySQL 5.0, there are three installation         packages to choose from when installing MySQL on Windows:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             &lt;span class="bold"&gt;&lt;strong&gt;The Essentials Package&lt;/strong&gt;&lt;/span&gt;:             This package has a filename similar to             &lt;code class="filename"&gt;mysql-essential-5.0.38-win32.msi&lt;/code&gt;             and contains the minimum set of files needed to install             MySQL on Windows, including the Configuration Wizard. This             package does not include optional components such as the             embedded server and benchmark suite.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             &lt;span class="bold"&gt;&lt;strong&gt;The Complete Package&lt;/strong&gt;&lt;/span&gt;: This             package has a filename similar to             &lt;code class="filename"&gt;mysql-5.0.38-win32.zip&lt;/code&gt; and             contains all files needed for a complete Windows             installation, including the Configuration Wizard. This             package includes optional components such as the embedded             server and benchmark suite.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             &lt;span class="bold"&gt;&lt;strong&gt;The Noinstall Archive&lt;/strong&gt;&lt;/span&gt;: This             package has a filename similar to             &lt;code class="filename"&gt;mysql-noinstall-5.0.38-win32.zip&lt;/code&gt;             and contains all the files found in the Complete install             package, with the exception of the Configuration Wizard.             This package does not include an automated installer, and             must be manually installed and configured.           &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         The Essentials package is recommended for most users. It is         provided as an &lt;code class="filename"&gt;.msi&lt;/code&gt; file for use with the         Windows Installer. The Complete and Noinstall distributions are         packaged as Zip archives. To use them, you must have a tool that         can unpack &lt;code class="filename"&gt;.zip&lt;/code&gt; files.       &lt;/p&gt; &lt;p&gt;         Your choice of install package affects the installation process         you must follow. If you choose to install either the Essentials         or Complete install packages, see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-using-installer.html" title="2.4.8.2. Installing MySQL with the Automated Installer"&gt;Section 2.4.8.2, “Installing MySQL with the Automated Installer”&lt;/a&gt;. If you choose to         install MySQL from the Noinstall archive, see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-install-archive.html" title="2.4.8.5. Installing MySQL from a Noinstall Zip Archive"&gt;Section 2.4.8.5, “Installing MySQL from a Noinstall Zip Archive”&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.2. Installing MySQL with the Automated Installer&lt;/h4&gt; &lt;p&gt;         New MySQL users can use the MySQL Installation Wizard and MySQL         Configuration Wizard to install MySQL on Windows. These are         designed to install and configure MySQL in such a way that new         users can immediately get started using MySQL.       &lt;/p&gt; &lt;p&gt;         The MySQL Installation Wizard and MySQL Configuration Wizard are         available in the Essentials and Complete install packages. They         are recommended for most standard MySQL installations.         Exceptions include users who need to install multiple instances         of MySQL on a single server host and advanced users who want         complete control of server configuration.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.3. Using the MySQL Installation Wizard&lt;/h4&gt;&lt;br /&gt;&lt;h5 class="title"&gt;2.4.8.3.1. Introduction to the Installation Wizard&lt;/h5&gt; &lt;p&gt;           MySQL Installation Wizard is an installer for the MySQL server           that uses the latest installer technologies for Microsoft           Windows. The MySQL Installation Wizard, in combination with           the MySQL Configuration Wizard, allows a user to install and           configure a MySQL server that is ready for use immediately           after installation.         &lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard is the standard installer for           all MySQL server distributions, version 4.1.5 and higher.           Users of previous versions of MySQL need to shut down and           remove their existing MySQL installations manually before           installing MySQL with the MySQL Installation Wizard. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-install-wizard.html#mysql-install-wizard-upgrading" title="2.4.8.3.7. Upgrading MySQL with the Installation Wizard"&gt;Section 2.4.8.3.7, “Upgrading MySQL with the Installation Wizard”&lt;/a&gt;, for more           information on upgrading from a previous version.         &lt;/p&gt; &lt;p&gt;           Microsoft has included an improved version of their Microsoft           Windows Installer (MSI) in the recent versions of Windows. MSI           has become the de-facto standard for application installations           on Windows 2000, Windows XP, and Windows Server 2003. The           MySQL Installation Wizard makes use of this technology to           provide a smoother and more flexible installation process.         &lt;/p&gt; &lt;p&gt;           The Microsoft Windows Installer Engine was updated with the           release of Windows XP; those using a previous version of           Windows can reference           &lt;a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;292539" target="_top"&gt;this           Microsoft Knowledge Base article&lt;/a&gt; for information on           upgrading to the latest version of the Windows Installer           Engine.         &lt;/p&gt; &lt;p&gt;           In addition, Microsoft has introduced the WiX (Windows           Installer XML) toolkit recently. This is the first highly           acknowledged Open Source project from Microsoft. We have           switched to WiX because it is an Open Source project and it           allows us to handle the complete Windows installation process           in a flexible manner using scripts.         &lt;/p&gt; &lt;p&gt;           Improving the MySQL Installation Wizard depends on the support           and feedback of users like you. If you find that the MySQL           Installation Wizard is lacking some feature important to you,           or if you discover a bug, please report it in our bugs           database using the instructions given in           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/bug-reports.html" title="1.8. How to Report Bugs or Problems"&gt;Section 1.8, “How to Report Bugs or Problems”&lt;/a&gt;.         &lt;/p&gt;  &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-starting"&gt;&lt;/a&gt;2.4.8.3.2. Downloading and Starting the MySQL Installation Wizard&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           The MySQL installation packages can be downloaded from           &lt;a href="http://dev.mysql.com/downloads/" target="_top"&gt;http://dev.mysql.com/downloads/&lt;/a&gt;. If the package you           download is contained within a Zip archive, you need to           extract the archive first.         &lt;/p&gt; &lt;p&gt;           The process for starting the wizard depends on the contents of           the installation package you download. If there is a           &lt;code class="filename"&gt;setup.exe&lt;/code&gt; file present, double-click it           to start the installation process. If there is an           &lt;code class="filename"&gt;.msi&lt;/code&gt; file present, double-click it to           start the installation process.         &lt;/p&gt; &lt;/div&gt; &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-install-type"&gt;&lt;/a&gt;2.4.8.3.3. Choosing an Install Type&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           There are three installation types available:           &lt;span class="bold"&gt;&lt;strong&gt;Typical&lt;/strong&gt;&lt;/span&gt;,           &lt;span class="bold"&gt;&lt;strong&gt;Complete&lt;/strong&gt;&lt;/span&gt;, and           &lt;span class="bold"&gt;&lt;strong&gt;Custom&lt;/strong&gt;&lt;/span&gt;.         &lt;/p&gt; &lt;p&gt;           The &lt;span class="bold"&gt;&lt;strong&gt;Typical&lt;/strong&gt;&lt;/span&gt; installation type           installs the MySQL server, the &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt;           command-line client, and the command-line utilities. The           command-line clients and utilities include           &lt;span&gt;&lt;strong class="command"&gt;mysqldump&lt;/strong&gt;&lt;/span&gt;, &lt;span&gt;&lt;strong class="command"&gt;myisamchk&lt;/strong&gt;&lt;/span&gt;,           and several other tools to help you manage the MySQL server.         &lt;/p&gt; &lt;p&gt;           The &lt;span class="bold"&gt;&lt;strong&gt;Complete&lt;/strong&gt;&lt;/span&gt; installation           type installs all components included in the installation           package. The full installation package includes components           such as the embedded server library, the benchmark suite,           support scripts, and documentation.         &lt;/p&gt; &lt;p&gt;           The &lt;span class="bold"&gt;&lt;strong&gt;Custom&lt;/strong&gt;&lt;/span&gt; installation type           gives you complete control over which packages you wish to           install and the installation path that is used. See           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-install-wizard.html#mysql-install-wizard-custom-install" title="2.4.8.3.4. The Custom Install Dialog"&gt;Section 2.4.8.3.4, “The Custom Install Dialog”&lt;/a&gt;, for           more information on performing a custom install.         &lt;/p&gt; &lt;p&gt;           If you choose the &lt;span class="bold"&gt;&lt;strong&gt;Typical&lt;/strong&gt;&lt;/span&gt; or           &lt;span class="bold"&gt;&lt;strong&gt;Complete&lt;/strong&gt;&lt;/span&gt; installation types           and click the &lt;span class="guibutton"&gt;Next&lt;/span&gt; button, you advance           to the confirmation screen to verify your choices and begin           the installation. If you choose the           &lt;span class="bold"&gt;&lt;strong&gt;Custom&lt;/strong&gt;&lt;/span&gt; installation type and           click the &lt;span class="guibutton"&gt;Next&lt;/span&gt; button, you advance to           the custom installation dialog, described in           &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-install-wizard.html#mysql-install-wizard-custom-install" title="2.4.8.3.4. The Custom Install Dialog"&gt;Section 2.4.8.3.4, “The Custom Install Dialog”&lt;/a&gt;.         &lt;/p&gt; &lt;/div&gt; &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-custom-install"&gt;&lt;/a&gt;2.4.8.3.4. The Custom Install Dialog&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           If you wish to change the installation path or the specific           components that are installed by the MySQL Installation           Wizard, choose the &lt;span class="bold"&gt;&lt;strong&gt;Custom&lt;/strong&gt;&lt;/span&gt;           installation type.         &lt;/p&gt; &lt;p&gt;           A tree view on the left side of the custom install dialog           lists all available components. Components that are not           installed have a red &lt;span class="guiicon"&gt;X&lt;/span&gt; icon; components           that are installed have a gray icon. To change whether a           component is installed, click on that component's icon and           choose a new option from the drop-down list that appears.         &lt;/p&gt; &lt;p&gt;           You can change the default installation path by clicking the           &lt;span class="guibutton"&gt;Change...&lt;/span&gt; button to the right of the           displayed installation path.         &lt;/p&gt; &lt;p&gt;           After choosing your installation components and installation           path, click the &lt;span class="guibutton"&gt;Next&lt;/span&gt; button to advance           to the confirmation dialog.         &lt;/p&gt; &lt;/div&gt; &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-confirmation-dialog"&gt;&lt;/a&gt;2.4.8.3.5. The Confirmation Dialog&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           Once you choose an installation type and optionally choose           your installation components, you advance to the confirmation           dialog. Your installation type and installation path are           displayed for you to review.         &lt;/p&gt; &lt;p&gt;           To install MySQL if you are satisfied with your settings,           click the &lt;span class="guibutton"&gt;Install&lt;/span&gt; button. To change           your settings, click the &lt;span class="guibutton"&gt;Back&lt;/span&gt; button.           To exit the MySQL Installation Wizard without installing           MySQL, click the &lt;span class="guibutton"&gt;Cancel&lt;/span&gt; button.         &lt;/p&gt; &lt;p&gt;           After installation is complete, you have the option of           registering with the MySQL web site. Registration gives you           access to post in the MySQL forums at           &lt;a href="http://forums.mysql.com/" target="_top"&gt;forums.mysql.com&lt;/a&gt;,           along with the ability to report bugs at           &lt;a href="http://bugs.mysql.com/" target="_top"&gt;bugs.mysql.com&lt;/a&gt; and           to subscribe to our newsletter. The final screen of the           installer provides a summary of the installation and gives you           the option to launch the MySQL Configuration Wizard, which you           can use to create a configuration file, install the MySQL           service, and configure security settings.         &lt;/p&gt; &lt;/div&gt; &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-changes"&gt;&lt;/a&gt;2.4.8.3.6. Changes Made by MySQL Installation Wizard&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           Once you click the &lt;span class="guibutton"&gt;Install&lt;/span&gt; button, the           MySQL Installation Wizard begins the installation process and           makes certain changes to your system which are described in           the sections that follow.         &lt;/p&gt; &lt;p&gt;           &lt;span class="bold"&gt;&lt;strong&gt;Changes to the Registry&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard creates one Windows registry key           in a typical install situation, located in           &lt;code class="literal"&gt;HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard creates a key named after the           major version of the server that is being installed, such as           &lt;code class="literal"&gt;MySQL Server 5.0&lt;/code&gt;. It contains           two string values, &lt;code class="literal"&gt;Location&lt;/code&gt; and           &lt;code class="literal"&gt;Version&lt;/code&gt;. The &lt;code class="literal"&gt;Location&lt;/code&gt;           string contains the path to the installation directory. In a           default installation it contains &lt;code class="literal"&gt;C:\Program           Files\MySQL\MySQL Server 5.0\&lt;/code&gt;. The           &lt;code class="literal"&gt;Version&lt;/code&gt; string contains the release number.           For example, for an installation of MySQL Server           5.0.38, the key contains a value of           &lt;code class="literal"&gt;5.0.38&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           These registry keys are used to help external tools identify           the installed location of the MySQL server, preventing a           complete scan of the hard-disk to determine the installation           path of the MySQL server. The registry keys are not required           to run the server, and if you install MySQL using the           &lt;code class="literal"&gt;noinstall&lt;/code&gt; Zip archive, the registry keys           are not created.         &lt;/p&gt; &lt;p&gt;           &lt;span class="bold"&gt;&lt;strong&gt;Changes to the Start Menu&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard creates a new entry in the           Windows &lt;span class="guimenu"&gt;Start&lt;/span&gt; menu under a common MySQL           menu heading named after the major version of MySQL that you           have installed. For example, if you install MySQL           5.0, the MySQL Installation Wizard creates a           &lt;span class="guisubmenu"&gt;MySQL Server 5.0&lt;/span&gt; section           in the &lt;span class="guimenu"&gt;Start&lt;/span&gt; menu.         &lt;/p&gt; &lt;p&gt;           The following entries are created within the new           &lt;span class="guimenu"&gt;Start&lt;/span&gt; menu section:         &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;               &lt;span class="guimenuitem"&gt;MySQL Command Line Client&lt;/span&gt;: This               is a shortcut to the &lt;span&gt;&lt;strong class="command"&gt;mysql&lt;/strong&gt;&lt;/span&gt; command-line               client and is configured to connect as the               &lt;code class="literal"&gt;root&lt;/code&gt; user. The shortcut prompts for a               &lt;code class="literal"&gt;root&lt;/code&gt; user password when you connect.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span class="guimenuitem"&gt;MySQL Server Instance Config               Wizard&lt;/span&gt;: This is a shortcut to the MySQL               Configuration Wizard. Use this shortcut to configure a               newly installed server, or to reconfigure an existing               server.             &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;               &lt;span class="guimenuitem"&gt;MySQL Documentation&lt;/span&gt;: This is a               link to the MySQL server documentation that is stored               locally in the MySQL server installation directory. This               option is not available when the MySQL server is installed               using the Essentials installation package.             &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;           &lt;span class="bold"&gt;&lt;strong&gt;Changes to the File System&lt;/strong&gt;&lt;/span&gt;         &lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard by default installs the MySQL           5.0 server to &lt;code class="filename"&gt;C:\&lt;em class="replaceable"&gt;&lt;code&gt;Program           Files&lt;/code&gt;&lt;/em&gt;\MySQL\MySQL Server           &lt;em class="replaceable"&gt;&lt;code&gt;5.0&lt;/code&gt;&lt;/em&gt;&lt;/code&gt;, where           &lt;em class="replaceable"&gt;&lt;code&gt;Program Files&lt;/code&gt;&lt;/em&gt; is the default           location for applications in your system, and           &lt;em class="replaceable"&gt;&lt;code&gt;5.0&lt;/code&gt;&lt;/em&gt; is the major           version of your MySQL server. This is the recommended location           for the MySQL server, replacing the former default location           &lt;code class="filename"&gt;C:\mysql&lt;/code&gt;.         &lt;/p&gt; &lt;p&gt;           By default, all MySQL applications are stored in a common           directory at &lt;code class="filename"&gt;C:\&lt;em class="replaceable"&gt;&lt;code&gt;Program           Files&lt;/code&gt;&lt;/em&gt;\MySQL&lt;/code&gt;, where           &lt;em class="replaceable"&gt;&lt;code&gt;Program Files&lt;/code&gt;&lt;/em&gt; is the default           location for applications in your Windows installation. A           typical MySQL installation on a developer machine might look           like this:         &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\Program Files\MySQL\MySQL Server 5.0&lt;br /&gt;C:\Program Files\MySQL\MySQL Administrator 1.0&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="title"&gt;2.4.8.5. Installing MySQL from a Noinstall Zip Archive&lt;/h4&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;         Users who are installing from the Noinstall package can use the         instructions in this section to manually install MySQL. The         process for installing MySQL from a Zip archive is as follows:       &lt;/p&gt; &lt;div class="orderedlist"&gt;&lt;ol type="1"&gt;&lt;li&gt;&lt;p&gt;             Extract the archive to the desired install directory           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Create an option file           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Choose a MySQL server type           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Start the MySQL server           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Secure the default user accounts           &lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt; &lt;p&gt;         This process is described in the sections that follow.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre class="programlisting"&gt;&lt;br /&gt;C:\Program Files\MySQL\MySQL Query Browser 1.0&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;           This approach makes it easier to manage and maintain all MySQL           applications installed on a particular system.         &lt;/p&gt; &lt;/div&gt; &lt;div class="section" lang="en"&gt; &lt;div class="titlepage"&gt;&lt;div&gt;&lt;div&gt;&lt;h5 class="title"&gt; &lt;a name="mysql-install-wizard-upgrading"&gt;&lt;/a&gt;2.4.8.3.7. Upgrading MySQL with the Installation Wizard&lt;/h5&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;           The MySQL Installation Wizard can perform server upgrades           automatically using the upgrade capabilities of MSI. That           means you do not need to remove a previous installation           manually before installing a new release. The installer           automatically shuts down and removes the previous MySQL           service before installing the new version.         &lt;/p&gt; &lt;p&gt;           Automatic upgrades are available only when upgrading between           installations that have the same major and minor version           numbers. For example, you can upgrade automatically from MySQL           4.1.5 to MySQL 4.1.6, but not from MySQL 4.1 to           MySQL 5.0.         &lt;/p&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;h4 class="title"&gt;2.4.8.6. Extracting the Install Archive&lt;/h4&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;         To install MySQL manually, do the following:       &lt;/p&gt; &lt;div class="orderedlist"&gt;&lt;ol type="1"&gt;&lt;li&gt;&lt;p&gt;             If you are upgrading from a previous version please refer to             &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-upgrading.html" title="2.4.8.14. Upgrading MySQL on Windows"&gt;Section 2.4.8.14, “Upgrading MySQL on Windows”&lt;/a&gt;, before beginning the             upgrade process.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             If you are using a Windows NT-based operating system such as             Windows NT, Windows 2000, Windows XP, or Windows Server             2003, make sure that you are logged in as a user with             administrator privileges.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Choose an installation location. Traditionally, the MySQL             server is installed in &lt;code class="filename"&gt;C:\mysql&lt;/code&gt;. The             MySQL Installation Wizard installs MySQL under             &lt;code class="filename"&gt;C:\Program Files\MySQL&lt;/code&gt;. If you do not             install MySQL at &lt;code class="filename"&gt;C:\mysql&lt;/code&gt;, you must             specify the path to the install directory during startup or             in an option file. See             &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-create-option-file.html" title="2.4.8.7. Creating an Option File"&gt;Section 2.4.8.7, “Creating an Option File”&lt;/a&gt;.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Extract the install archive to the chosen installation             location using your preferred Zip archive tool. Some tools             may extract the archive to a folder within your chosen             installation location. If this occurs, you can move the             contents of the subfolder into the chosen installation             location.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.7. Creating an Option File&lt;/h4&gt; &lt;p&gt;         If you need to specify startup options when you run the server,         you can indicate them on the command line or place them in an         option file. For options that are used every time the server         starts, you may find it most convenient to use an option file to         specify your MySQL configuration. This is particularly true         under the following circumstances:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             The installation or data directory locations are different             from the default locations (&lt;code class="filename"&gt;C:\Program             Files\MySQL\MySQL Server 5.0&lt;/code&gt; and             &lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL Server             5.0\data&lt;/code&gt;).           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             You need to tune the server settings.           &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         When the MySQL server starts on Windows, it looks for options in         two files: the &lt;code class="filename"&gt;my.ini&lt;/code&gt; file in the Windows         directory, and the &lt;code class="filename"&gt;C:\my.cnf&lt;/code&gt; file. The         Windows directory typically is named something like         &lt;code class="filename"&gt;C:\WINDOWS&lt;/code&gt; or         &lt;code class="filename"&gt;C:\WINNT&lt;/code&gt;. You can determine its exact         location from the value of the &lt;code class="literal"&gt;WINDIR&lt;/code&gt;         environment variable using the following command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;echo %WINDIR%&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         MySQL looks for options first in the &lt;code class="filename"&gt;my.ini&lt;/code&gt;         file, and then in the &lt;code class="filename"&gt;my.cnf&lt;/code&gt; file. However,         to avoid confusion, it's best if you use only one file. If your         PC uses a boot loader where &lt;code class="filename"&gt;C:&lt;/code&gt; is not the         boot drive, your only option is to use the         &lt;code class="filename"&gt;my.ini&lt;/code&gt; file. Whichever option file you use,         it must be a plain text file.       &lt;/p&gt; &lt;p&gt;         You can also make use of the example option files included with         your MySQL distribution; see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/option-files-preconfigured.html" title="4.3.2.1. Preconfigured Option Files"&gt;Section 4.3.2.1, “Preconfigured Option Files”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         An option file can be created and modified with any text editor,         such as Notepad. For example, if MySQL is installed in         &lt;code class="filename"&gt;E:\mysql&lt;/code&gt; and the data directory is in         &lt;code class="filename"&gt;E:\mydata\data&lt;/code&gt;, you can create an option         file containing a &lt;code class="literal"&gt;[mysqld]&lt;/code&gt; section to specify         values for the &lt;code class="literal"&gt;basedir&lt;/code&gt; and         &lt;code class="literal"&gt;datadir&lt;/code&gt; parameters:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysqld]&lt;br /&gt;# set basedir to your installation path&lt;br /&gt;basedir=E:/mysql&lt;br /&gt;# set datadir to the location of your data directory&lt;br /&gt;datadir=E:/mydata/data&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         Note that Windows pathnames are specified in option files using         (forward) slashes rather than backslashes. If you do use         backslashes, you must double them:       &lt;/p&gt; &lt;pre class="programlisting"&gt;[mysqld]&lt;br /&gt;# set basedir to your installation path&lt;br /&gt;basedir=E:\\mysql&lt;br /&gt;# set datadir to the location of your data directory&lt;br /&gt;&lt;br /&gt;datadir=E:\\mydata\\data&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         On Windows, the MySQL installer places the data directory         directly under the directory where you install MySQL. If you         would like to use a data directory in a different location, you         should copy the entire contents of the &lt;code class="filename"&gt;data&lt;/code&gt;         directory to the new location. For example, if MySQL is         installed in &lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL Server         5.0&lt;/code&gt;, the data directory is by default in         &lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL Server         5.0\data&lt;/code&gt;. If you want to use         &lt;code class="filename"&gt;E:\mydata&lt;/code&gt; as the data directory instead,         you must do two things:       &lt;/p&gt; &lt;div class="orderedlist"&gt;&lt;ol type="1"&gt;&lt;li&gt;&lt;p&gt;             Move the entire &lt;code class="filename"&gt;data&lt;/code&gt; directory and all             of its contents from &lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL             Server 5.0\data&lt;/code&gt; to             &lt;code class="filename"&gt;E:\mydata&lt;/code&gt;.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Use a &lt;code class="option"&gt;--datadir&lt;/code&gt; option to specify the new             data directory location each time you start the server.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.8. Selecting a MySQL Server Type&lt;/h4&gt; &lt;p class="cs"&gt;This section does not apply to MySQL Enterprise Server users.&lt;/p&gt; &lt;p&gt;         The following table shows the available servers for Windows in         MySQL 5.0.       &lt;/p&gt; &lt;div class="informaltable"&gt;&lt;table border="1"&gt; &lt;colgroup&gt; &lt;col&gt; &lt;col&gt; &lt;/colgroup&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Binary&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;&lt;span class="bold"&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;span&gt;&lt;strong class="command"&gt;mysqld-nt&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;Optimized binary for Windows NT, 2000, and XP with support for named                 pipes&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;Optimized binary without named pipe support (for Windows 95/ME)&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;span&gt;&lt;strong class="command"&gt;mysqld-debug&lt;/strong&gt;&lt;/span&gt;&lt;/td&gt; &lt;td&gt;Like &lt;span&gt;&lt;strong class="command"&gt;mysqld-nt&lt;/strong&gt;&lt;/span&gt;, but compiled with full debugging and                 automatic memory allocation checking&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;&lt;/div&gt; &lt;p&gt;         All of the preceding binaries are optimized for modern Intel         processors, but should work on any Intel i386-class or higher         processor.       &lt;/p&gt; &lt;p&gt;         Each of the servers in a distribution support the same set of         storage engines. The &lt;code class="literal"&gt;SHOW ENGINES&lt;/code&gt; statement         displays which engines a given server supports.       &lt;/p&gt; &lt;p&gt;         All Windows MySQL 5.0 servers have support for         symbolic linking of database directories.       &lt;/p&gt; &lt;a class="indexterm" name="id2773098"&gt;&lt;/a&gt;&lt;a class="indexterm" name="id2773105"&gt;&lt;/a&gt;&lt;p&gt;         MySQL supports TCP/IP on all Windows platforms. The         &lt;span&gt;&lt;strong class="command"&gt;mysqld-nt&lt;/strong&gt;&lt;/span&gt; and &lt;code class="literal"&gt;mysql-debug&lt;/code&gt;         servers support named pipes on Windows NT, 2000, XP, and 2003.         However, the default is to use TCP/IP regardless of platform.         (Named pipes are slower than TCP/IP in many Windows         configurations.)       &lt;/p&gt; &lt;p&gt;         Use of named pipes is subject to these conditions:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             Named pipes are enabled only if you start the server with             the &lt;code class="option"&gt;--enable-named-pipe&lt;/code&gt; option. It is             necessary to use this option explicitly because some users             have experienced problems with shutting down the MySQL             server when named pipes were used.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Named-pipe connections are allowed only by the             &lt;span&gt;&lt;strong class="command"&gt;mysqld-nt&lt;/strong&gt;&lt;/span&gt; and             &lt;span&gt;&lt;strong class="command"&gt;mysqld-debug&lt;/strong&gt;&lt;/span&gt; servers, and only if the             server is run on a version of Windows that supports named             pipes (NT, 2000, XP, 2003).           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             These servers can be run on Windows 98 or Me, but only if             TCP/IP is installed; named-pipe connections cannot be used.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             These servers cannot be run on Windows 95.           &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: Most of the examples in         this manual use &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; as the server name. If         you choose to use a different server, such as         &lt;span&gt;&lt;strong class="command"&gt;mysqld-nt&lt;/strong&gt;&lt;/span&gt;, make the appropriate substitutions         in the commands that are shown in the examples.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.9. Starting the Server for the First Time&lt;/h4&gt; &lt;p&gt;         This section gives a general overview of starting the MySQL         server. The following sections provide more specific information         for starting the MySQL server from the command line or as a         Windows service.       &lt;/p&gt; &lt;p&gt;         The information here applies primarily if you installed MySQL         using the &lt;code class="literal"&gt;Noinstall&lt;/code&gt; version, or if you wish         to configure and test MySQL manually rather than with the GUI         tools.       &lt;/p&gt; &lt;p&gt;         The examples in these sections assume that MySQL is installed         under the default location of &lt;code class="filename"&gt;C:\Program         Files\MySQL\MySQL Server 5.0&lt;/code&gt;. Adjust the         pathnames shown in the examples if you have MySQL installed in a         different location.       &lt;/p&gt; &lt;p&gt;         On NT-based systems such as Windows NT, 2000, XP, or 2003,         clients have two options. They can use TCP/IP, or they can use a         named pipe if the server supports named-pipe connections. For         MySQL to work with TCP/IP on Windows NT 4, you must install         service pack 3 (or newer).       &lt;/p&gt; &lt;p&gt;         On Windows 95, 98, or Me, MySQL clients always connect to the         server using TCP/IP. (This allows any machine on your network to         connect to your MySQL server.) Because of this, you must make         sure that TCP/IP support is installed on your machine before         starting MySQL. You can find TCP/IP on your Windows CD-ROM.       &lt;/p&gt; &lt;p&gt;         Note that if you are using an old Windows 95 release (for         example, OSR2), it is likely that you have an old Winsock         package; MySQL requires Winsock 2. You can get the newest         Winsock from &lt;a href="http://www.microsoft.com/" target="_top"&gt;http://www.microsoft.com/&lt;/a&gt;. Windows         98 has the new Winsock 2 library, so it is unnecessary to update         the library.       &lt;/p&gt; &lt;p&gt;         MySQL for Windows also supports shared-memory connections if the         server is started with the &lt;code class="option"&gt;--shared-memory&lt;/code&gt;         option. Clients can connect through shared memory by using the         &lt;code class="option"&gt;--protocol=memory&lt;/code&gt; option.       &lt;/p&gt; &lt;p&gt;         For information about which server binary to run, see         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/windows-select-server.html" title="2.4.8.8. Selecting a MySQL Server Type"&gt;Section 2.4.8.8, “Selecting a MySQL Server Type”&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         Testing is best done from a command prompt in a console window         (or “&lt;span class="quote"&gt;DOS window&lt;/span&gt;”). In this way you can have the         server display status messages in the window where they are easy         to see. If something is wrong with your configuration, these         messages make it easier for you to identify and fix any         problems.       &lt;/p&gt; &lt;p&gt;         To start the server, enter this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld" --console&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         For a server that includes &lt;code class="literal"&gt;InnoDB&lt;/code&gt; support,         you should see the messages similar to those following as it         starts (the pathnames and sizes may differ):       &lt;/p&gt; &lt;pre class="programlisting"&gt;InnoDB: The first specified datafile c:\ibdata\ibdata1 did not exist:&lt;br /&gt;InnoDB: a new database to be created!&lt;br /&gt;InnoDB: Setting file c:\ibdata\ibdata1 size to 209715200&lt;br /&gt;InnoDB: Database physically writes the file full: wait...&lt;br /&gt;InnoDB: Log file c:\iblogs\ib_logfile0 did not exist: new to be created&lt;br /&gt;InnoDB: Setting log file c:\iblogs\ib_logfile0 size to 31457280&lt;br /&gt;InnoDB: Log file c:\iblogs\ib_logfile1 did not exist: new to be created&lt;br /&gt;InnoDB: Setting log file c:\iblogs\ib_logfile1 size to 31457280&lt;br /&gt;InnoDB: Log file c:\iblogs\ib_logfile2 did not exist: new to be created&lt;br /&gt;InnoDB: Setting log file c:\iblogs\ib_logfile2 size to 31457280&lt;br /&gt;InnoDB: Doublewrite buffer not found: creating new&lt;br /&gt;InnoDB: Doublewrite buffer created&lt;br /&gt;InnoDB: creating foreign key constraint system tables&lt;br /&gt;InnoDB: foreign key constraint system tables created&lt;br /&gt;011024 10:58:25  InnoDB: Started&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         When the server finishes its startup sequence, you should see         something like this, which indicates that the server is ready to         service client connections:       &lt;/p&gt; &lt;pre class="programlisting"&gt;mysqld: ready for connections&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Version: '5.0.38'  socket: ''  port: 3306&lt;br /&gt;&lt;/pre&gt; &lt;p&gt;         The server continues to write to the console any further         diagnostic output it produces. You can open a new console window         in which to run client programs.       &lt;/p&gt; &lt;p&gt;         If you omit the &lt;code class="option"&gt;--console&lt;/code&gt; option, the server         writes diagnostic output to the error log in the data directory         (&lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL Server         5.0\data&lt;/code&gt; by default). The error log is         the file with the &lt;code class="filename"&gt;.err&lt;/code&gt; extension.       &lt;/p&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: The accounts that are         listed in the MySQL grant tables initially have no passwords.         After starting the server, you should set up passwords for them         using the instructions in &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/post-installation.html" title="2.4.15. Post-Installation Setup and Testing"&gt;Section 2.4.15, “Post-Installation Setup and Testing”&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.10. Starting MySQL from the Windows Command Line&lt;/h4&gt; &lt;p&gt;         The MySQL server can be started manually from the command line.         This can be done on any version of Windows.       &lt;/p&gt; &lt;p&gt;         To start the &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; server from the command         line, you should start a console window (or “&lt;span class="quote"&gt;DOS         window&lt;/span&gt;”) and enter this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld"&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         The path to &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; may vary depending on the         install location of MySQL on your system.       &lt;/p&gt; &lt;p&gt;         On non-NT versions of Windows, this command starts         &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; in the background. That is, after the         server starts, you should see another command prompt. If you         start the server this way on Windows NT, 2000, XP, or 2003, the         server runs in the foreground and no command prompt appears         until the server exits. Because of this, you should open another         console window to run client programs while the server is         running.       &lt;/p&gt; &lt;p&gt;         You can stop the MySQL server by executing this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" -u root shutdown&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: If the MySQL         &lt;code class="literal"&gt;root&lt;/code&gt; user account has a password, you need to         invoke &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; with the         &lt;code class="option"&gt;-p&lt;/code&gt; option and supply the password when         prompted.       &lt;/p&gt; &lt;p&gt;         This command invokes the MySQL administrative utility         &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; to connect to the server and tell         it to shut down. The command connects as the MySQL         &lt;code class="literal"&gt;root&lt;/code&gt; user, which is the default         administrative account in the MySQL grant system. Note that         users in the MySQL grant system are wholly independent from any         login users under Windows.       &lt;/p&gt; &lt;p&gt;         If &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; doesn't start, check the error log         to see whether the server wrote any messages there to indicate         the cause of the problem. The error log is located in the         &lt;code class="filename"&gt;C:\Program Files\MySQL\MySQL Server         5.0\data&lt;/code&gt; directory. It is the file with         a suffix of &lt;code class="filename"&gt;.err&lt;/code&gt;. You can also try to start         the server as &lt;span&gt;&lt;strong class="command"&gt;mysqld --console&lt;/strong&gt;&lt;/span&gt;; in this case,         you may get some useful information on the screen that may help         solve the problem.       &lt;/p&gt; &lt;p&gt;         The last option is to start &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; with the         &lt;code class="option"&gt;--standalone&lt;/code&gt; and &lt;code class="option"&gt;--debug&lt;/code&gt;         options. In this case, &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; writes a log         file &lt;code class="filename"&gt;C:\mysqld.trace&lt;/code&gt; that should contain         the reason why &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; doesn't start. See         &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/making-trace-files.html" target="_top"&gt;Creating Trace Files&lt;/a&gt;.       &lt;/p&gt; &lt;p&gt;         Use &lt;span&gt;&lt;strong class="command"&gt;mysqld --verbose --help&lt;/strong&gt;&lt;/span&gt; to display all         the options that &lt;span&gt;&lt;strong class="command"&gt;mysqld&lt;/strong&gt;&lt;/span&gt; understands.&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;h4 class="title"&gt;2.4.8.11. Starting MySQL as a Windows Service&lt;/h4&gt; &lt;p&gt;         On the NT family (Windows NT, 2000, XP, 2003), the recommended         way to run MySQL is to install it as a Windows service, whereby         MySQL starts and stops automatically when Windows starts and         stops. A MySQL server installed as a service can also be         controlled from the command line using &lt;span&gt;&lt;strong class="command"&gt;NET&lt;/strong&gt;&lt;/span&gt;         commands, or with the graphical &lt;span&gt;&lt;strong class="command"&gt;Services&lt;/strong&gt;&lt;/span&gt;         utility.       &lt;/p&gt; &lt;p&gt;         The &lt;span&gt;&lt;strong class="command"&gt;Services&lt;/strong&gt;&lt;/span&gt; utility (the Windows         &lt;span&gt;&lt;strong class="command"&gt;Service Control Manager&lt;/strong&gt;&lt;/span&gt;) can be found in the         Windows Control Panel (under &lt;span class="guimenuitem"&gt;Administrative         Tools&lt;/span&gt; on Windows 2000, XP, and Server 2003). To         avoid conflicts, it is advisable to close the         &lt;span&gt;&lt;strong class="command"&gt;Services&lt;/strong&gt;&lt;/span&gt; utility while performing server         installation or removal operations from the command line.       &lt;/p&gt; &lt;p&gt;         Before installing MySQL as a Windows service, you should first         stop the current server if it is running by using the following         command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqladmin" -u root shutdown&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         &lt;span class="bold"&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/span&gt;: If the MySQL         &lt;code class="literal"&gt;root&lt;/code&gt; user account has a password, you need to         invoke &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; with the         &lt;code class="option"&gt;-p&lt;/code&gt; option and supply the password when         prompted.       &lt;/p&gt; &lt;p&gt;         This command invokes the MySQL administrative utility         &lt;span&gt;&lt;strong class="command"&gt;mysqladmin&lt;/strong&gt;&lt;/span&gt; to connect to the server and tell         it to shut down. The command connects as the MySQL         &lt;code class="literal"&gt;root&lt;/code&gt; user, which is the default         administrative account in the MySQL grant system. Note that         users in the MySQL grant system are wholly independent from any         login users under Windows.       &lt;/p&gt; &lt;p&gt;         Install the server as a service using this command:       &lt;/p&gt; &lt;pre class="programlisting"&gt;C:\&gt; &lt;strong class="userinput"&gt;&lt;code&gt;"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld" --install&lt;/code&gt;&lt;/strong&gt; &lt;/pre&gt; &lt;p&gt;         The service-installation command does not start the server.         Instructions for that are given later in this section.       &lt;/p&gt; &lt;p&gt;         To make it easier to invoke MySQL programs, you can add the         pathname of the MySQL &lt;code class="filename"&gt;bin&lt;/code&gt; directory to your         Windows system &lt;code class="literal"&gt;PATH&lt;/code&gt; environment variable:       &lt;/p&gt; &lt;div class="itemizedlist"&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;             On the Windows desktop, right-click on the &lt;span class="guiicon"&gt;My             Computer&lt;/span&gt; icon, and select             &lt;span class="guimenuitem"&gt;Properties&lt;/span&gt;           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Next select the &lt;span class="guimenuitem"&gt;Advanced&lt;/span&gt; tab from             the &lt;span class="guimenu"&gt;System Properties&lt;/span&gt; menu that appears,             and click the &lt;span class="guibutton"&gt;Environment Variables&lt;/span&gt;             button.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;             Under &lt;span class="guilabel"&gt;System Variables&lt;/span&gt;, select             &lt;span class="guimenuitem"&gt;Path&lt;/span&gt;, and then click the             &lt;span class="guibutton"&gt;Edit&lt;/span&gt; button. The &lt;span class="guimenu"&gt;Edit System             Variable&lt;/span&gt; dialogue should appear.           &lt;/p&gt;&lt;/li&gt;&lt;li&gt; &lt;p&gt;             Place your cursor at the end of the text appearing in the             space marked &lt;span class="guilabel"&gt;Variable Value&lt;/span&gt;. (Use the             &lt;span&gt;&lt;strong class="keycap"&gt;End&lt;/strong&gt;&lt;/span&gt; key to ensure that your cursor is             positioned at the very end of the text in this space.) Then             enter the complete pathname of your MySQL             &lt;code class="filename"&gt;bin&lt;/code&gt; directory (for example,             &lt;code class="literal"&gt;C:\Program Files\MySQL\MyS
