JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Check if a database exists in MySQL

Author:JIYIK Last Updated:2025/04/23 Views:

In this article, we will introduce many ways to check if a database exists in MySQL.


Check if the database exists in MySQL

The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL server.

The MySQL schema is broadly divided into system tables for general operational purposes and data dictionary tables for storing metadata about database items.

Use the use command to check whether the MySQL database exists

Use the use command to determine whether the MySQL database exists. The syntax of the command is as follows:

use `database_name`

The database you want to use is identified here by the name database_name. If the database exists, this command will exit with status code 0; otherwise, it will display the error Unknown database "database_name".

Use the schema_name command to check if the MySQL database exists

You can determine if a MySQL database exists using the schema_name command. The syntax of the command is as follows:

SELECT SCHEMA_NAME
  FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'database_name';

Here, database_name is the name of the database that you want to check for existence.

Use the SHOW command to check if the MySQL database exists

Another option is to use the MySQL SHOW command to see how many databases are currently available. The syntax of the command is as follows:

SHOW databases;

Use the mysqlshow command to check if the MySQL database exists

You can use the mysqlshow command to determine if the MySQL database exists. The syntax of the command is as follows:

mysqlshow `database_name`

Here, database_name represents the name of the database for which you want detailed information. If the database exists, this command will exit with status code 0 and print the database and table details; otherwise, it will display the error unknown database 'database_name'.

To further understand the previous concepts, consider the following example:

SELECT SCHEMA_NAME
  FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'Employees';
SHOW databases;
use Employees;
mysqlshow `Employees`;

In the previous example, we first determined whether the Employees schema existed. We attempted to check each database on the current MySQL server using the second operation.

The third operation changes the given database from the default database, in this case Employees. Information about the Employees database and its tables is shown in the last operation.

Run the above lines of code in any browser compatible with MySQL. It will display the following result.

Output:

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

+--------------------+
| Database           |
+--------------------+
| Employees          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


Database: Employees
+--------------------------------+----------+
|             Tables             | Columns  |
+--------------------------------+----------+
| employee                       |       13 |
| employee_audit                 |       10 |
| employee_salary                |        5 |
+--------------------------------+----------+

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:77 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

MySQL sorts data alphabetically

Publish Date:2025/04/23 Views:129 Category:MySQL

In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t

Display the current database in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query

Converting from datetime type to date-only in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:193 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:196 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial