Check if table exists in MySQL
This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence.
What is a table in MySQL?
A table is a database object that holds all the database information. A table is similar to a spreadsheet in that the data is logically structured in rows and columns.
Each column shows a field in the record and each row represents a different record. We can create
create a table in MySQL by simply calling the statement.
CREATE TABLE table_name (table_id INT);
We can execute drop
the statement to permanently delete a table.
DROP TABLE table_name;
Sometimes, when we want to deploy a table to a database, we need to confirm that the table has a unique name, otherwise, CREATE TABLE
the statement will fail.
In the other case, if we drop a table that does not exist, we will get the error again. The solution is simple; we need to check if a table exists and perform the operation we want.
Check if a table exists in MySQL using information mode
One way to check if a table exists is to query INFORMATION_SCHEMA.TABLES
the . To do this, we can INFORMATION_SCHEMA.TABLES
get the number of tables with the same name as ours from .
CREATE TABLE table_name (table_id INT);
SELECT count(*)
FROM information_schema.TABLES
WHERE TABLE_NAME = 'table_name';
Output:
+----------+
| count(*) |
+----------+
| 1 |
+----------+
If we get zero, then there is no such table in our database, if we get a non-zero number, then the table exists. If you have multiple databases, you can WHERE
check this in the INSERT statement TABLE_SCHEMA = 'database_name'
.
If you don't know the database, you can SELECT DATABASE()
query it with . A revised version of the above code with the database is below.
CREATE TABLE EMPLOYEE (table_id INT);
SELECT count(*) FROM information_schema.TABLES WHERE TABLE_NAME = 'EMPLOYEE'
AND TABLE_SCHEMA in (SELECT DATABASE());
Output:
+----------+
| count(*) |
+----------+
| 1 |
+----------+
Use SHOW TABLES
the command to check whether the table exists in MySQL
The second method is also very simple and is to use SHOW TABLES
. Let's create a table in the database sampletable
and check if it exists.
CREATE TABLE sampletable (myId INT);
SHOW TABLES LIKE 'sampletable';
Output:
Tables_in_db_3xs4qrcrf (sampletable)
sampletable
If there aren't any tables, you may get zero rows. You need to know that this method does not work with temporary tables.
A temporary table in MySQL is a special type of table that allows you to save a temporary result set that you can reuse multiple times in a single session.
Temporary tables are useful when it is difficult or costly
to query the data that you need in JOIN
a single statement with a INSERT clause .SELECT
In this case, you can save the immediate results in a temporary table and process them with another query. We will discuss the method that works with temporary tables in the next method.
Use table_exists()
the procedure to check if the table exists in MySQL
MySQL 5.7 and later added a new method to determine whether a table or view exists, including temporary tables. Before talking about this procedure, we must first master this procedure.
Procedures are database stored subroutines (similar to subprograms) in traditional scripting languages. In the case of MySQL, procedures are written in MySQL and are stored in the MySQL database/server.
A MySQL procedure consists of three parts: its name, its parameter list, and its SQL query. Now we can move on to our particular procedure table_exists
.
According to the MySQL documentation: 该过程在 OUT 参数中返回表类型。如果具有给定名称的临时表和永久表都存在,则返回 TEMPORARY
.
Let's check it out.
CREATE TABLE mytable (id INT PRIMARY KEY);
CALL sys.table_exists('mydatabase', 'mytable', @exists); SELECT @exists;
Output:
+------------+
| @exists |
+------------+
| BASE TABLE |
+------------+
1 row in set (0.00 sec)
If there is no such table in the database, you will get an empty result.
+---------+
| @exists |
+---------+
| |
+---------+
1 row in set (0.00 sec)
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.
Related Articles
How to select from multiple tables in MySQL
Publish Date:2025/04/25 Views:57 Category:MySQL
-
This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO
Creating a table from CSV in MySQL
Publish Date:2025/04/25 Views:114 Category:MySQL
-
In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou
Creating a Temporary Table in MySQL
Publish Date:2025/04/25 Views:183 Category:MySQL
-
In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These
Truncate all tables in Mysql
Publish Date:2025/04/25 Views:89 Category:MySQL
-
Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two
Different ways to check if a row exists in a MySQL table
Publish Date:2025/04/25 Views:163 Category:MySQL
-
This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is
Get column names in MySQL
Publish Date:2025/04/25 Views:109 Category:MySQL
-
In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif
Get the version in MySQL
Publish Date:2025/04/24 Views:170 Category:MySQL
-
In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T
Full Join in MySQL
Publish Date:2025/04/24 Views:71 Category:MySQL
-
This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_
Grouping by month in MySQL
Publish Date:2025/04/24 Views:166 Category:MySQL
-
In this article, we will learn how to group values by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its