首頁  >  文章  >  資料庫  >  mysql怎麼查詢資料庫有多少表

mysql怎麼查詢資料庫有多少表

青灯夜游
青灯夜游原創
2021-12-02 11:13:5628167瀏覽

mysql查詢資料庫有多少表格的方法:1、使用MySQL客戶端登入MySQL資料庫伺服器;2、使用「USE 資料庫名稱」語句切換到指定的資料庫;3、使用「SHOW TABLES; ”語句列出指定資料庫中的所有表格即可。

mysql怎麼查詢資料庫有多少表

本教學操作環境:windows7系統、mysql8版本、Dell G3電腦。

在mysql中,可以利用SHOW TABLES語句來查詢資料庫有多少表,該語句可以列出資料庫的所有表。

要在MySQL資料庫中列出所有資料表,請依照下列步驟操作:

  • 使用MySQL客戶端(如mysql )登入MySQL資料庫伺服器

  • 使用 USE 資料庫名稱 語句切換到特定的資料庫。

  • 使用SHOW TABLES指令。

下面說明了MySQL SHOW TABLES指令的語法:

SHOW TABLES;

MySQL SHOW TABLES範例

以下範例說明如何列出yiibaidb資料庫中的所有表格。

步驟1 - 連接到MySQL資料庫伺服器:

C:\Users\Administrator>mysql -u root -p

步驟2 -切換到yiibaidb資料庫:

mysql> USE yiibaidb;
Database changed
mysql>

步驟3 - 顯示yiibaidb資料庫中的所有資料表:

mysql> show tables;
+--------------------+
| Tables_in_yiibaidb |
+--------------------+
| aboveavgproducts   |
| article_tags       |
| bigsalesorder      |
| contacts           |
| customerorders     |
| customers          |
| departments        |
| employees          |
| employees_audit    |
| officeinfo         |
| offices            |
| offices_bk         |
| offices_usa        |
| orderdetails       |
| orders             |
| organization       |
| payments           |
| price_logs         |
| productlines       |
| products           |
| saleperorder       |
| user_change_logs   |
| v_contacts         |
| vps                |
+--------------------+
24 rows in set

SHOW TABLES指令可顯示表格是基底表還是視圖。要在結果中包含表格類型,請使用SHOW TABLES語句,如下所示-

SHOW FULL TABLES;

執行上面語句,如下所示-

mysql> SHOW FULL TABLES;
+--------------------+------------+
| Tables_in_yiibaidb | Table_type |
+--------------------+------------+
| aboveavgproducts   | VIEW       |
| article_tags       | BASE TABLE |
| bigsalesorder      | VIEW       |
| contacts           | BASE TABLE |
| customerorders     | VIEW       |
| customers          | BASE TABLE |
| departments        | BASE TABLE |
| employees          | BASE TABLE |
| employees_audit    | BASE TABLE |
| officeinfo         | VIEW       |
| offices            | BASE TABLE |
| offices_bk         | BASE TABLE |
| offices_usa        | BASE TABLE |
| orderdetails       | BASE TABLE |
| orders             | BASE TABLE |
| organization       | VIEW       |
| payments           | BASE TABLE |
| price_logs         | BASE TABLE |
| productlines       | BASE TABLE |
| products           | BASE TABLE |
| saleperorder       | VIEW       |
| user_change_logs   | BASE TABLE |
| v_contacts         | VIEW       |
| vps                | VIEW       |
+--------------------+------------+
24 rows in set

我們在yiibaidb資料庫中建立一個名為view_contacts的視圖,其中包含來自employeescustomers表格的名字,姓氏和電話。

CREATE VIEW view_contacts 
AS 
SELECT lastName, firstName, extension as phone 
FROM employees 
UNION
SELECT contactFirstName, contactLastName, phone 
FROM customers;

現在,執行查詢SHOW FULL TABLES指令:

mysql> SHOW FULL TABLES;
+--------------------+------------+
| Tables_in_yiibaidb | Table_type |
+--------------------+------------+
| aboveavgproducts   | VIEW       |
| article_tags       | BASE TABLE |
| bigsalesorder      | VIEW       |
| contacts           | BASE TABLE |
| customerorders     | VIEW       |
| customers          | BASE TABLE |
| departments        | BASE TABLE |
| employees          | BASE TABLE |
| employees_audit    | BASE TABLE |
| officeinfo         | VIEW       |
| offices            | BASE TABLE |
| offices_bk         | BASE TABLE |
| offices_usa        | BASE TABLE |
| orderdetails       | BASE TABLE |
| orders             | BASE TABLE |
| organization       | VIEW       |
| payments           | BASE TABLE |
| price_logs         | BASE TABLE |
| productlines       | BASE TABLE |
| products           | BASE TABLE |
| saleperorder       | VIEW       |
| user_change_logs   | BASE TABLE |
| v_contacts         | VIEW       |
| view_contacts      | VIEW       |
| vps                | VIEW       |
+--------------------+------------+
25 rows in set

您可以看到,v_contacts,view_contacts, vps等是視圖(VIEW),而其它表則都是基底表(BASE TABLE)。

對於具有許多表格的資料庫,一次顯示所有表格可能不免直覺。

幸運的是,SHOW TABLES指令提供了一個選項,允許使用LIKE運算子或WHERE子句中的表達式對傳回的表進行過濾,如下所示:

SHOW TABLES LIKE pattern;

SHOW TABLES WHERE expression;

例如,要顯示yiibaidb資料庫中以字母p開頭的所有表,請使用以下語句:

mysql> SHOW TABLES LIKE 'p%';
+-------------------------+
| Tables_in_yiibaidb (p%) |
+-------------------------+
| payments                |
| price_logs              |
| productlines            |
| products                |
+-------------------------+
4 rows in set

或顯示以'es'字串結尾的表,可使用以下語句:

mysql> SHOW TABLES LIKE '%es';
+--------------------------+
| Tables_in_yiibaidb (%es) |
+--------------------------+
| employees                |
| offices                  |
| productlines             |
+--------------------------+
3 rows in set

以下語句說明如何在SHOW TABLES#語句中使用WHERE子句列出yiibai資料庫中的所有視圖-

mysql> SHOW FULL TABLES WHERE table_type = 'VIEW';
+--------------------+------------+
| Tables_in_yiibaidb | Table_type |
+--------------------+------------+
| aboveavgproducts   | VIEW       |
| bigsalesorder      | VIEW       |
| customerorders     | VIEW       |
| officeinfo         | VIEW       |
| organization       | VIEW       |
| saleperorder       | VIEW       |
| v_contacts         | VIEW       |
| view_contacts      | VIEW       |
| vps                | VIEW       |
+--------------------+------------+
9 rows in set

有時,希望看到非目前使用的資料庫中的表。可以使用SHOW TABLES語句的FROM子句來指定要顯示表格的資料庫。

以下範例示範如何顯示以'time'開頭的表格;

mysql> SHOW TABLES FROM mysql LIKE 'time%';
+---------------------------+
| Tables_in_mysql (time%)   |
+---------------------------+
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
+---------------------------+
5 rows in set

以下語句相當於上面的語句,但它使用IN而不是FROM子句,如下所示-

mysql> SHOW TABLES IN mysql LIKE 'time%';
+---------------------------+
| Tables_in_mysql (time%)   |
+---------------------------+
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
+---------------------------+
5 rows in set

請注意,如果您沒有基底表或檢視的權限,則它不會顯示在SHOW TABLES命令的結果集中。

【相關推薦:mysql影片教學

#

以上是mysql怎麼查詢資料庫有多少表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn