如何檢查 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)
廣告