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 ...

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.