
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if a table exists in MySQL and create if it does not already exist?
If you try to create a table and the table name already exist then MySQL will give a warning message. Let us verify the concept.
Here, we are creating a table that already exist −
mysql> CREATE TABLE IF NOT EXISTS DemoTable ( CustomerId int, CustomerName varchar(30), CustomerAge int ); Query OK, 0 rows affected, 1 warning (0.05 sec)
The table name DemoTable is already present. Let us check the warning message.
Following is the query −
mysql> show warnings;
This will produce the following output i.e. the warning message −
+-------+------+------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------+ | Note | 1050 | Table 'demotable' already exists | +-------+------+------------------------------------+ 1 row in set (0.00 sec)
Let us change the table name and create a table that does not already exist −
mysql> CREATE TABLE IF NOT EXISTS DemoTable2 ( CustomerId int, CustomerName varchar(20), CustomerAge int ); Query OK, 0 rows affected (0.56 sec)
Table created successfully above since it does not already exist.
Following is the query to insert records in the table using insert command −
mysql> insert into DemoTable2 values(101,'Chris',23); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2 values(102,'Robert',24); Query OK, 1 row affected (0.12 sec)
Following is the query to display records from the table using select command −
mysql> select *from DemoTable2;
This will produce the following output
+------------+--------------+-------------+ | CustomerId | CustomerName | CustomerAge | +------------+--------------+-------------+ | 101 | Chris | 23 | | 102 | Robert | 24 | +------------+--------------+-------------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- Create view in MySQL only if it does not already exist?
- MySQL create user if it does not exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- Check if table exists in MySQL and display the warning if it exists?
- How to create a folder if it does not exist in C#?
- Check if MySQL entry exists and if it does, how to overwrite other columns?
- How can I create a python directory if it does not exist?
- How can I create a directory if it does not exist using Python?
- Add object to array in JavaScript if name does not already exist?
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- Different methods to check if a MySQL table exist?
- How to check if a column exist in a MySQL table?
- Check if a user exists in MySQL and drop it?
- Insert records in MongoDB collection if it does not exist?
Advertisements