13.8.4 USE Statement语句

USE db_name

The USE statement tells MySQL to use the named database as the default (current) database for subsequent statements. USE语句告诉MySQL使用命名数据库作为后续语句的默认(当前)数据库。This statement requires some privilege for the database or some object within it.此语句要求数据库或其中的某个对象具有某些权限。

The named database remains the default until the end of the session or another USE statement is issued:在会话结束或发出另一个USE语句之前,命名数据库将保持默认状态:

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

The database name must be specified on a single line. 必须在单行中指定数据库名称。Newlines in database names are not supported.不支持数据库名称中的换行符。

Making a particular database the default by means of the USE statement does not preclude accessing tables in other databases. 通过USE语句将特定数据库设为默认数据库并不排除访问其他数据库中的表。The following example accesses the author table from the db1 database and the editor table from the db2 database:以下示例从db1数据库访问author表,从db2数据库访问editor表:

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