Sunday, October 25, 2009

Section 3.12. Command-Line Interface







3.12. Command-Line Interface

It's not necessary to open the mysql interface
to enter SQL statements into the MySQL server. In fact, sometimes you may
have only a quick query to make in MySQL, and you'd rather just do it from
the shell or command line. For instance, suppose we have a table called
vendors in our database, and we want to get a quick
list of vendors in Louisiana and their telephone numbers. We could enter
the following from the command line in Linux (or an equivalent operating
system) to get this list:

mysql --user='paola' --password='caporale1017' \
-e "SELECT vendor, telephone FROM vendors \
WHERE state='LA'" bookstore


We're still using the mysql client, but we're
not entering the interface. As shown earlier, we provide the username
paola and her password
caporale1017 as arguments to the command. This line
ends with a backslash to let the Unix shell know that there are more parameters to come.
Otherwise, we would need to put all of the information shown on one
line.

On the second line, we use the -e option to
indicate that what follows it in double quotes is to be executed by the
mysql client. Notice that what's in double quotes is
the same SQL statement with the same syntax as what we would enter if we
were logged in to the interface. The syntax doesn't change because we're
entering the SQL statement from the command line. We don't need a
terminating semicolon, though, because the mysql
client knows where the SQL statement ends.

Finally, after the SQL statement, we provide the name of the
database to be used. We could eliminate this argument by adding the
database name before the table name, separated by a dot (i.e.,
bookstore.vendors).

There are other command-line options with the
mysql client. There are also other command-line
utilities available for accessing and manipulating data in MySQL. You can
use some of these utilities for backing up the database or for performing
server maintenance and tuning. They are covered in Chapters Chapter 15 and Chapter 16.








No comments:

Post a Comment