Search code examples
mysqlcreate-tablesql-drop

MySql - DROP table 'whatever' if exist ELSE create table 'whatever'?


I simply want to drop the table 'whatever' if it exist and then recreate table 'whatever' in a single query if possible.

DROP TABLE IF EXISTS `whatever` ELSE
    CREATE TABLE `whatever`

Any idea ?


Solution

  • CREATE TABLE `whatever` IF NOT EXISTS ELSE TRUNCATE `whatever`
    

    Use TRUNCATE to empty the table and reset cardinality instead of deleting the table and recreating it.