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'" bookstoreWe’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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access