如何检查MySQL数据库是否存在?
schema_name 命令用于检查 MySQL 数据库是否存在。此命令的语法如下 -
select schema_name from information_schema.schemata where schema_name = 'databasename';
现在,上面的命令用于检查数据库是否存在。查询如下 -
案例 1 - 数据库存在。
mysql> select schema_name from information_schema.schemata where schema_name = 'business';
获得的输出如下 -
+-------------+| SCHEMA_NAME |
+-------------+
| business |
+-------------+
1 row in set (0.00 sec)
案例 2 - 数据库不存在。
mysql> select schema_name from information_schema.schemata where schema_name = 'sample2';Empty set (0.00 sec)
Note: We can check how many databases are present in MySQL with the help of the showcommand.
show 命令的语法如下 -
show databases;
使用上述语法的查询如下 -
mysql> show databases;
以下是输出
+--------------------+| Database |
+--------------------+
| business |
| hello |
| information_schema |
| mybusiness |
| mysql |
| performance_schema |
| sample |
| sys |
| test |
+--------------------+
9 rows in set (0.00 sec)
现在,我们可以在 use 命令的帮助下选择特定数据库的名称。查询如下 -
mysql> use business;Database changed
我们还可以检查特定数据库中存在的表数。这可以使用 show 命令来完成。对此的查询如下 -
mysql> show tables;
执行上述查询后,获得以下输出 -
+----------------------+| Tables_in_business |
+----------------------+
| addcolumntable |
| bookindexes |
| chardemo |
| demo |
| demoascii |
| demobcrypt |
| demoint |
| demoschema |
| duplicatebookindexes |
| existsrowdemo |
| foreigntable |
| groupdemo |
| int1demo |
| intdemo |
| latandlangdemo |
| modifycolumnnamedemo |
| modifydatatype |
| moviecollection |
| mytable |
| nthrecorddemo |
| nulldemo |
| primarytable |
| primarytable1 |
| smallintdemo |
| student |
| tblstudent |
| tbluni |
| textdemo |
| texturl |
| varchardemo |
| varcharurl |
+----------------------+
31 rows in set (0.00 sec)
可以在 desc 命令的帮助下描述特定的表。其语法如下 -
desc yourTableName;
现在,上面的语法用于描述表。对此的查询是 -
mysql> desc modifydatatype;
以下是获得的输出 -
+----------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| YourName | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
以上是 如何检查MySQL数据库是否存在? 的全部内容, 来源链接: utcz.com/z/356457.html