0

Is it possible to determine if a table was created or already existed when using create table if not exists...?

There seem to be two variables returned:

(query ok, affected_rows)

The query always returns (1, 0) regardless of if the table was created or already existed.

But it seems a create schema if not exists does return affected_rows=1.

Thanks.

2
  • you should specify what library or interface you are using to execute mysql queries to get better help Commented Jun 16, 2021 at 21:01
  • @ysth it is more about the MySQL protocol, and how it is inconsistent with affected_rows when creating schema vs creating tables. Commented Jun 16, 2021 at 21:22

1 Answer 1

3

I get 1 warning if the table exists (mysql 8.0.25):

mysql> CREATE TABLE IF NOT EXISTS `test` (`id` INT NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE IF NOT EXISTS `test` (`id` INT NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected, 1 warning (0.00 sec)

The warning is 1050 Table 'test' already exists

But you can just check if the table exists before creating it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.