
- 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
INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
Here’s the syntax to check whether the table already exists using the CREATE TABLE IF NOT EXISTS −
create table if not exists yourTableName ( yourColumnName1 dataType, . . . . N ); insert into yourTableName values(yourValue1,.......N);
Let us first create a table −
mysql> create table if not exists DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentSubject varchar(100) ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentSubject) values('Java'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(StudentSubject) values('MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentSubject) values('C'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------------+ | StudentId | StudentSubject | +-----------+----------------+ | 1 | Java | | 2 | MySQL | | 3 | C | +-----------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Create a table in MySQL and implement TIMESTAMPDIFF()?
- Create a table in MySQL that matches another table?
- Insert JSON into a MySQL table?
- Create MySQL query to create a table from an existing table?
- Insert the data into Table C IF The data is not in Table B while Comparing to Table A in MySQL?
- Implement MySQL trigger in the first table to insert records in the second table?
- How to create a MySQL table with InnoDB engine table?
- How to create a MySQL table with MyISAM engine table?
- Create index on create table in MySQL?
- How can I create a table and insert values in that table using prepare statements?
- MySQL statement to copy data from one table and insert into another table
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- How to check if a table exists in MySQL and create if it does not already exist?
- How to create a temporary MySQL table in a SELECT statement without a separate CREATE TABLE?
- Create a MySQL table from already created table selecting specific rows?

Advertisements