
- 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 table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
In order to check if a table already exists in the database by using MySQL, you can use INFORMATION_SCHEMA.TABLES. Following is the syntax −
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘yourDatabaseName’ AND TABLE_NAME = ’yourTableName’;
Let us implement the above syntax in order to check if a table already exists in the database.
Case 1: When the table is present −
mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sample' AND TABLE_NAME = 'DemoTable';
This will produce the following output −
+--------------+ | TABLE_NAME | +--------------+ | DemoTable | +--------------+ 1 row in set (0.01 sec)
Case 2: When the table is not present −
mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA= 'sample' AND TABLE_NAME= 'DemoTable2';
This will produce the following output −
Empty set (0.01 sec)
- Related Articles
- How to check an empty table already in a MySQL database?
- How to check if a MySQL database exists?
- How to check if a table exists in MySQL and create if it does not already exist?
- How to check if a user email already exists in Laravel?
- How to check if value exists with MySQL SELECT 1?
- Check if table exists in MySQL and display the warning if it exists?
- How to check if a specific country code exists in a cell with MySQL?
- How to check if a given key already exists in a Python dictionary?
- Check if MongoDB database exists?
- Check if a value exists in a column in a MySQL table?
- Check that a table exists in MySQL?
- Check if a table is empty or not in MySQL using EXISTS
- How to check table status of the tables in a particular MySQL database?
- Check how many rows are in a MySQL database table?
- Create a table if it does not already exist and insert a record in the same query with MySQL

Advertisements