Skip to main content

The mysql Client


The mysql Client

          This section discusses mysql, a general-purpose client program for issuing queries and retrieving their
results. It can be used interactively or in batch mode to read queries from a file.

Using mysql Interactively

     The mysql client program enables the sending of queries to the MySQL server with any results being
displayed in the same interface. It can be used interactively or it can read query input from a file in batch
mode:

• Interactive mode is useful for day-to-day usage, for quick one-time queries, and for testing how
queries work.

• Batch mode is useful for running queries that have been prewritten and stored in a file. It's especially valuable for issuing a complex series of queries that's difficult to enter manually, or queries that need to be run automatically by a job scheduler without user intervention. MySQL statements such as the version query, shown below as executed within the mysql client, can also be run from the shell command prompt as part of the mysql client startup;

mysql> SELECT VERSION();


+-----------------+
| VERSION()       |
+-----------------+
| 5.0.41-log           |
+-----------------+

You can execute a statement directly from the command line by using the -e or --execute option:

shell> mysql -u user_name -ppassword -e "SELECT VERSION()"


+-----------------+
| VERSION()       |
+-----------------+
| 5.0.41-log           |
+-----------------+

No statement terminator is necessary unless the string following -e consists of multiple statements. In that case, separate the statements by semicolon characters.





Statement Terminators


There are numerous terminators that can be used in the mysql client to end a statement. Two terminators
are the semicolon character (';') and the \g sequence. They're equivalent and may be used
interchangeably:

mysql> SELECT VERSION(), DATABASE();


+-----------------+--------------------+
| VERSION()       | DATABASE()        |
+-----------------+--------------------+
| 5.0.40-log | INFORMATION_SCHEMA |
+-----------------+--------------------+

The \G sequence also terminates queries, but causes mysql to display query results in a vertical style that
shows each output row with each column value on a separate line:

mysql> SELECT VERSION(), DATABASE()\G

************************** 1. row **************************

VERSION(): 5.0.40-log
DATABASE(): INFORMATION_SCHEMA

The \G terminator is especially useful if a query produces very wide output lines because vertical format
can make the result much easier to read.

The mysql client allows a single query to be entered using multiple input lines. This makes it easier to
issue a long query because you can enter it over the course of several lines. mysql will wait until it sees
the statement terminator before sending the query to the server to be executed. For example:

mysql> SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
-> FROM INFORMATION_SCHEMA.TABLES
-> WHERE TABLE_NAME LIKE 'E%';
 +--------------------+-----------------+-----------------+
| TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE   |
+--------------------+-----------------+------------------+
| information_schema | ENGINES          | SYSTEM VIEW |
| information_schema | EVENTS            | SYSTEM VIEW |
| mysql                      | event                   | BASE TABLE    |
+--------------------+-----------------+------------------+
3 rows in set (#.## sec)

In the preceding example, mysql changes the prompt from mysql> to -> to provide feedback that it's still waiting to see the end of the statement.

If a statement results in an error, mysql displays the following error message:

mysql> This is an invalid statement;
ERROR 1064 (42000): You have an error in your SQL syntax.

If it is necessary to cancel a statement once mysql has already received some of the input, enter \c and

mysql will cancel the statement and return the mysql> prompt:

mysql> SELECT * FROM INFORMATION_SCHEMA > WHERE \c
mysql>

To quit mysql, use \q, QUIT, or EXIT: mysql> \q

Comments

Popular posts from this blog

PHP INTRODUCTION

                     PHP  (recursive acronym for  PHP: Hypertext Preprocessor ) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. PHP stands for  P HP:  H ypertext  P reprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software PHP is free to download and use Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource:  www.php.net PHP is easy to learn and runs efficiently on the server side What can PHP do? Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynam

MySQL General Architecture

        MySQL operates in a networked environment using a client/server architecture. In other words, a central  program acts as a server, and various client programs connect to the server to make requests. A MySQL  installation has the following major components: MySQL Server, Client programs and MySQL non client  utilities.  MySQL Server MySQL Server, or mysqld, is the database server program. The server manages access to the actual  database (schema) on disk and in memory. MySQL Server is multi-threaded and supports many  simultaneous client connections. Clients can connect via several connection protocols. For managing  database contents, the MySQL server features a modular architecture that supports multiple storage engines  that handle different types of tables (for example, it supports both transactional and non-transactional  tables). Keep in mind the difference between a server and a host. The server is software (the MySQL server  program mysqld). Server characteristi