Selecting a Database
Problem
You want to tell mysql which database to use.
Solution
Name the database on the mysql command line or
issue a
USE
statement from within mysql.
Discussion
When you issue a query that refers to a table (as most queries do),
you need to indicate which database the table is part of. One way to
do so is to use a fully qualified table reference that
begins with the database name. (For example,
cookbook.limbs refers to the
limbs table in the cookbook
database.) As a convenience, MySQL also allows you to select a
default (current) database so that you can refer to its tables
without explicitly specifying the database name each time. You can
specify the database on the command line when you start
mysql:
% mysql cookbookIf you provide options on the command line such as connection parameters when you run mysql, they should precede the database name:
%mysql -hhost-p -uusercookbook
If you’ve already started a mysql
session, you can select a database (or switch to a different one) by
issuing a USE statement:
mysql> USE cookbook;
Database changedIf you’ve forgotten or are not sure which database is the current one (which can happen easily if you’re using multiple databases and switching between them several times during the course of a mysql session), use the following statement:
mysql> SELECT DATABASE( );
+------------+
| DATABASE( ) |
+------------+
| cookbook |
+------------+DATABASE( ) is a function that returns the name of the current database. If no database ...