检查表是否存在而不在MySQL中使用“ select from”?
我们可以借助SHOW命令来实现。首先,我将在USE命令的帮助下使用数据库-
mysql> USE business; Database changed
我们现在在“业务”数据库中。之后,我们可以检查该数据库有多少个表可用。查询如下-
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 rows in set (0.03 sec)
下面显示了无需使用SELECT语句即可检查表是否存在的其他方法。
语法如下-
SHOW tables like ‘yourTableName’;
现在,我将应用上面的查询来检查数据库“business”的表是否存在。
查询如下-
mysql> SHOW tables like 'tblstudent';
以下是输出
+---------------------------------+ | Tables_in_business (tblstudent) | +---------------------------------+ | tblstudent | +---------------------------------+ 1 row in set (0.00 sec)
查看上面的输出,表“tblstudent”在我的数据库中可见。表不存在的情况如下-
mysql> SHOW tables like 'sampledemo'; Empty set (0.00 sec)