如何检查 MySQL 数据库是否存在?
mysqlmysqli database更新于 2024/8/23 3:56:17
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/
打印
下一节:MySQL error 1452 - Cannot add or a child row: a foreign key constraint fails ❯❮ 上一节:如何在执行 SQL 脚本时回显打印语句?

