在MySQL中不使用“select from”检查表是否存在?
我们可以通过SHOW命令来实现。首先,我将使用USE命令来使用我的数据库——
mysql> USE business;
已切换到数据库business
我们现在在“business”数据库中。之后,我们可以查看此数据库中有多少可用的表。查询如下所示−
mysql> SHOW tables;
以下是输出结果
+------------------------+
| Tables_in_business |
+------------------------+
| addcolumntable |
| autoincrement |
| autoincrementtable |
| bookindexes |
| chardemo |
| clonestudent |
| columnvaluenulldemo |
| dateadddemo |
| deletedemo |
| deleterecord |
| demo |
| demo1 |
| demoascii |
| demoauto |
| demobcrypt |
| demoemptyandnull |
| demoint |
| demoonreplace |
| demoschema |
| distinctdemo |
| duplicatebookindexes |
| duplicatefound |
| employeetable |
| existsrowdemo |
| findandreplacedemo |
| firsttable |
| foreigntable |
| foreigntabledemo |
| groupdemo |
| groupdemo1 |
| incasesensdemo |
| int1demo |
| intdemo |
| latandlangdemo |
| limitoffsetdemo |
| milliseconddemo |
| modifycolumnnamedemo |
| modifydatatype |
| moneydemo |
| moviecollection |
| mytable |
| nonasciidemo |
| nthrecorddemo |
| nulldemo |
| nullwithselect |
| pasthistory |
| presenthistory |
| primarytable |
| primarytable1 |
| primarytabledemo |
| rownumberdemo |
| rowstranspose |
| rowstransposedemo |
| secondtable |
| sequencedemo |
| smallintdemo |
| spacecolumn |
| student |
| tblfirst |
| tblstudent |
| tbluni |
| textdemo |
| texturl |
| trailingandleadingdemo |
| transcationdemo |
| unsigneddemo |
| updtable |
| varchardemo |
| varchardemo1 |
| varchardemo2 |
| varcharurl |
| whereconditon |
+------------------------+
72行(0.03秒)
以下是在不使用SELECT语句的情况下检查表是否存在的另一种方式。
语法如下−
SHOW tables like ‘yourTableName’;
现在,我正在应用上述查询来检查我的数据库“business”中是否存在表。
查询如下 −
mysql> SHOW tables like 'tblstudent';
以下是输出结果
+---------------------------------+
| Tables_in_business (tblstudent) |
+---------------------------------+
| tblstudent |
+---------------------------------+
1行(0.00秒)
查看上面的输出结果,表’tblstudent’在我的数据库中可见。当表不存在时,情况如下所示−
mysql> SHOW tables like 'sampledemo';
空集(0.00秒)
阅读更多:MySQL 教程