13.8.4 USE Syntax

USE db_name

The USE db_name statement tells MySQL to use the db_name database as the default (current) database for subsequent statements. The database remains the default until the end of the session or another USE statement is issued:

USE db1;
SELECT COUNT(*) FROM mytable;   # selects from db1.mytable
USE db2;
SELECT COUNT(*) FROM mytable;   # selects from db2.mytable

Making a particular database the default by means of the USE statement does not preclude you from accessing tables in other databases. The following example accesses the author table from the db1 database and the editor table from the db2 database:

USE db1;
SELECT author_name,editor_name FROM author,db2.editor
  WHERE author.editor_id = db2.editor.editor_id;

Download this Manual
User Comments
  Posted by Ryan Bates on July 22, 2005
This query will return the name of the current database and NULL if no database is selected.

  Posted by Robert McCormick on October 19, 2006
To see the list of databases that you can use with the USE command, do

  Posted by Bruce Daniels on May 7, 2008
If you want to get back to the condition of having no default database then do something like the following:

mysql> CREATE DATABASE SomeWeirdName;
mysql> USE SomeWeirdName;
mysql> DROP SomeWeirdName;
Sign Up Login You must be logged in to post a comment.