3

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));
0

3 Answers 3

9

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");
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

MySQL doesn't support creating a table in the SELECT statement, but you can easily preface with a quick check to create your table if it doesn't exist:

CREATE TABLE IF NOT EXISTS `table` (
    id INT(10),
    word VARCHAR(500)
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.