
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to check if a MySQL database exists?
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)
- Related Articles
- Check if MongoDB database exists?
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- How to check if value exists with MySQL SELECT 1?
- How to check if a specific country code exists in a cell with MySQL?
- Check if table exists in MySQL and display the warning if it exists?
- Check if MySQL entry exists and if it does, how to overwrite other columns?
- How to check if a variable exists in JavaScript?
- How to check if a column exists in Pandas?
- How to check if a file exists in Golang?
- How to check if a file exists in Perl?
- Check if a user exists in MySQL and drop it?
- How to check if a table exists in MySQL and create if it does not already exist?
- Check if a value exists in a column in a MySQL table?
- Check if value exists in a comma separated list in MySQL?
- How to check if a key exists in a Python dictionary?
