如何在 MySQL 中檢測表是否存在?


要檢測表的存在,請使用 INFORMATION_SCHEMA.TABLES 的概念。以下是語法 −

select table_name from information_schema.tables
where table_schema=database()
and table_name=yourTableName;

為了理解上述語法,讓我們建立一個表 −

mysql> create table DemoTable2032
   -> (
   -> ClientId int,
   -> ClientName varchar(20),
   -> ClientAge int,
   -> ClientCountryName varchar(20)
   -> );
Query OK, 0 rows affected (1.07 sec)

以下是對資料庫中存在的表進行檢測的查詢 −

mysql> select table_name from information_schema.tables
   -> where table_schema=database()
   -> and table_name='DemoTable2032';

將會產生以下輸出 −

+---------------+
| TABLE_NAME    |
+---------------+
| demotable2032 |
+---------------+
1 row in set (0.00 sec)

更新時間: 07-4-2020

250 次瀏覽

開啟你的 職業生涯

完成本課程,獲得認證

開始學習
廣告
© . All rights reserved.