如何检查 MySQL 数据库是否存在?
schema_name 命令用于检查 MySQL 数据库是否存在。该命令的语法如下所示:
select schema_name from information_schema.schemata where schema_name = 'database name';
现在,以上命令用于检查数据库是否存在。查询如下:
案例 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 show command.
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)
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP