If table exists drop table then create it, if it does not exist just create it

George picture George · Nov 22, 2013 · Viewed 233.7k times · Source

I'm stumped, I don't know how to go about doing this.

Basically I just want to create a table, but if it exists it needs to be dropped and re-created, not truncated, but if it doesn't exist just create it.

Would anyone be able to help?

Thanks, George

Answer

G-Nugget picture G-Nugget · Nov 22, 2013

Just put DROP TABLE IF EXISTS `tablename`; before your CREATE TABLE statement.

That statement drops the table if it exists but will not throw an error if it does not.