How to check if a MySQL database exists?

MySQLMySQLi Database

The schema_name command is used to check if a MySQL database exists or not. The syntax of this command is as follows −

select schema_name from information_schema.schemata where schema_name = 'database
name';

Now, the above command is used to check whether the database exists or not. The query for that is as follows −

Case 1 − The database exists.

mysql> select schema_name from information_schema.schemata where schema_name = 'business';

The output obtained is as follows −

+-------------+
| SCHEMA_NAME |
+-------------+
| business    |
+-------------+
1 row in set (0.00 sec)

Case 2 − The database does not exist.

mysql> select schema_name from information_schema.schemata where schema_name = 'sample2';
Empty set (0.00 sec)
Note: We can check how many databases are present in MySQL with the help of the show
command.

The syntax for the show command is as follows −

show databases;

The query using the above syntax is as follows −

mysql> show databases;

The following is the output

+--------------------+
| Database           |
+--------------------+
| business           |
| hello              |
| information_schema |
| mybusiness         |
| mysql              |
| performance_schema |
| sample             |
| sys                |
| test               |
+--------------------+
9 rows in set (0.00 sec)

Now, we can choose the name of a particular database with the help of the use command. The query is given as follows −

mysql> use business;
Database changed

We can also check the number of tables that are present in a particular database. This can be done using the show command. The query for this is as follows −

mysql> show tables;

After executing the above query, the following output is obtained −

+----------------------+
| Tables_in_business   |
+----------------------+
| addcolumntable       |
| bookindexes          |
| chardemo             |
| demo                 |
| demoascii            |
| demobcrypt           |
| demoint              |
| demoschema           |
| duplicatebookindexes |
| existsrowdemo        |
| foreigntable         |
| groupdemo            |
| int1demo             |
| intdemo              |
| latandlangdemo       |
| modifycolumnnamedemo |
| modifydatatype       |
| moviecollection      |
| mytable              |
| nthrecorddemo        |
| nulldemo             |
| primarytable         |
| primarytable1        |
| smallintdemo         |
| student              |
| tblstudent           |
| tbluni               |
| textdemo             |
| texturl              |
| varchardemo          |
| varcharurl           |
+----------------------+
31 rows in set (0.00 sec)

A particular table can be described with the help of the desc command. The syntax for that is as follows −

desc yourTableName;

Now, the above syntax is used to describe the table. The query for that is −

mysql> desc modifydatatype;

The following is the output obtained −

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES   |    | NULL    |       |
| YourName | varchar(100) | YES   |    | NULL    |       |
+----------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
raja
Published on 18-Oct-2018 09:14:22
Advertisements