Si la tabla existe drop table entonces crealo, si no existe solo crealo


Estoy perplejo, no se como hacer esto.

Básicamente solo quiero crear una tabla, pero si existe necesita ser eliminada y re-creada, no truncada, pero si no existe simplemente crearla.

¿Alguien podría ayudar?

Gracias, George

 98
Author: George, 2013-11-23

2 answers

Simplemente ponga DROP TABLE IF EXISTS `tablename`; antes de su declaración CREATE TABLE.

Esa instrucción elimina la tabla si existe, pero no lanzará un error si no lo hace.

 214
Author: G-Nugget,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-10 16:26:03

Solo use DROP TABLE IF EXISTS:

DROP TABLE IF EXISTS `foo`;
CREATE TABLE `foo` ( ... );

Intente buscar en la documentación de MySQL primero si tiene algún otro problema.

 31
Author: squeamish ossifrage,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-22 22:55:09