如何检查 MySQL 数据库中是否存在空表?

mysqlmysqli database

要检查数据库中是否存在空表,您需要从表中提取一些记录。如果表不为空,则将返回表记录。

首先我们创建一个表 −

mysql> create table DemoTable(Id int,Name varchar(100),Age int);
Query OK, 0 rows affected (0.80 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable values(1001,'John',23);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(1002,'Chris',21);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(1003,'David',22);
Query OK, 1 row affected (0.19 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+------+-------+------+
| Id   | Name  | Age  |
+------+-------+------+
| 1001 | John  |   23 |
| 1002 | Chris |   21 |
| 1003 | David |   22 |
+------+-------+------+
3 rows in set (0.00 sec)

让我们从表中删除所有记录 −

mysql> delete from DemoTable where Id IN(1001,1002,1003);
Query OK, 3 rows affected (0.19 sec))

现在尝试根据 where 条件 − 从表中获取记录

mysql> select Id from DemoTable where Name="John";
Empty set (0.00 sec)

如上所示,由于表现在是空的,因此返回一个空集。


相关文章