How to check an empty table already in a MySQL database?

MySQLMySQLi Database

To check an empty table is in a database, you need to extract some records from the table. If the table is not empty then the table records would be returned.

Let us first create a table −

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

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

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

Let us delete all records from the table −

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

Now try to get records from the table on the basis of where condition −

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

You can see above, since the table is now empty, therefore an empty set is returned.

raja
Published on 22-Aug-2019 10:48:25
Advertisements