Sql 简明教程
SQL - Show Databases
很多时候,我们都会遇到需要列出所有可用数据库的情况。MySQL 提供了一种使用 SHOW DATABASES 命令列出所有数据库的便捷方式,而 MS SQL Server 中没有用于显示或列出数据库的特定命令,但你可以把 SELECT…FROM 命令用作列出可用数据库的变通方法。
Many a times you face a situation where you need to list down all the available databases. MySQL provides a convenient way to list down all the databases using SHOW DATABASES command where as there is no particular command in MS SQL Server to show or list the databases but, you can use the SELECT…FROM command as a work around list down available databases.
List Databases using SQL
SQL SHOW DATABASES 语句用于列出 MySQL 数据库中的所有可用数据库。你可以使用 SHOW SCHEMAS 作为 SHOW DATABASES 的备用命令。
The SQL SHOW DATABASES statement is used to list down all the available databases in MySQL database. You can use SHOW SCHEMAS as an alternate command for SHOW DATABASES.
Syntax
以下是列出 MySQL 中所有可用数据库的 SQL SHOW DATABASES 语法:
Following is the syntax of SQL SHOW DATABASES to list down all the available databases in MySQL −
SHOW DATABASES [LIKE 'pattern' | WHERE expr] ;
我们可以使用 LIKE 或 WHERE 子句,配合 SHOW DATABASES 来过滤数据库列表。
We can use LIKE or WHERE clause along with SHOW DATABASES to filter out a list of databases.
Example
以下是列出所有可用数据库的示例。
Following is an example to list down all the available databases.
SHOW DATABASES;
输出将如下显示。此输出取决于系统中可用的数据库数量 −
The output will be displayed as follows. This output depends on the number of databases available in the system −
Database |
performance_schema |
information_schema |
mysql |
testDB |
以下是列出所有名称以 test 开头的数据库的示例。
Following is an example to list down all the databases whose name starts with test.
SHOW DATABASES LIKE 'test%';
输出将如下显示 −
The output will be displayed as follows −
Database (test%) |
testDB |
The SHOW SCHEMAS Statement
你可以使用 SHOW SCHEMAS 语句作为 SHOW DATABASES 语句的备选。
You can use the SHOW SCHEMAS statement as an alternate for the SHOW DATABASES statement.
Syntax
以下是 SQL SHOW SCHEMAS 语句的语法,用于列出 MySQL 中所有可用数据库 −
Following is the syntax of the SQL SHOW SCHEMAS statement to list down all the available databases in MySQL −
SHOW SCHEMAS [LIKE 'pattern' | WHERE expr] ;
我们可以使用 LIKE 或 WHERE 子句,配合 SHOW SCHEMAS 来过滤数据库列表。
We can use LIKE or WHERE clause along with SHOW SCHEMAS to filter out a list of databases.
Example
以下是列出所有可用数据库的示例。
Following is an example to list down all the available databases.
SHOW SCHEMAS;
输出将如下显示。此输出取决于系统中可用的数据库数量 −
The output will be displayed as follows. This output depends on the number of databases available in the system −
Database |
performance_schema |
information_schema |
mysql |
testDB |
以下是列出所有名称以 test 开头的数据库的示例。
Following is an example to list down all the databases whose name starts with test.
SHOW SCHEMAS LIKE 'test%';
输出将如下显示 −
The output will be displayed as follows −
Database (test%) |
testDB |
The SELECT…FROM Statement
如果你使用的是 MS SQL Server,则可以使用 SELECT…FROM 语句列出所有可用数据库,如下所示。
If you are working with MS SQL Server then you can use the SELECT…FROM statement to list down all the available databases as shown below.
SQL> SELECT * FROM sys.databases;
Output
如果执行上述查询,它会返回一张表,其中列出所有数据库以及与数据库相关的信息。
If we execute the above query, it returns a table that lists down all the databases and associated information about the databases.
name |
database_id |
source_database_id |
owner_sid |
master |
1 |
NULL |
001 |
tempdb |
2 |
NULL |
001 |
model |
3 |
NULL |
001 |
msdb |
4 |
NULL |
001 |
testDB |
5 |
NULL |
001000 |