INSERT INTO table IF table exists otherwise CREATE TABLE

15,174

Solution 1

I'd create 2 statements. Try this:

CREATE TABLE IF NOT EXISTS `table` (
id INT(10),
word VARCHAR(500)
);
INSERT INTO `table` VALUES (NULL,"1234");

Solution 2

You can first check if the table exists, if it doesn't then you can create it. Do the insert statement after this..

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

Something like

CREATE TABLE IF NOT EXISTS table (...)

INSERT INTO table VALUES (...)

Note:

However, there is no verification that the existing table has a structure identical to that indicated by the CREATE TABLE statement.

Share:
15,174
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    Do mysql support something like this?

    INSERT INTO `table` VALUES (NULL,"1234") IF TABLE EXISTS `table` ELSE CREATE TABLE `table` (id INT(10), word VARCHAR(500));