如何检查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)
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短