- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements