MySQL, Comprobar si existe una columna en una tabla con SQL
Estoy tratando de escribir una consulta que compruebe si una tabla específica en MySQL tiene una columna específica, y si no - crearla. De lo contrario no hacer nada. Esto es realmente un procedimiento fácil en cualquier base de datos de clase empresarial, sin embargo MySQL parece ser una excepción.
Pensé algo como
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='prefix_topic' AND column_name='topic_last_update')
BEGIN
ALTER TABLE `prefix_topic` ADD `topic_last_update` DATETIME NOT NULL;
UPDATE `prefix_topic` SET `topic_last_update` = `topic_date_add`;
END;
Funcionaría, pero falla mal. ¿Hay alguna manera?
10 answers
Esto funciona bien para mí.
SHOW COLUMNS FROM `table` LIKE 'fieldname';
Con PHP sería algo así como...
$result = mysql_query("SHOW COLUMNS FROM `table` LIKE 'fieldname'");
$exists = (mysql_num_rows($result))?TRUE:FALSE;
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
2011-09-01 00:09:15
@julio
Gracias por el ejemplo SQL. Probé la consulta y creo que necesita una pequeña alteración para que funcione correctamente.
SELECT *
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = 'db_name'
AND TABLE_NAME = 'table_name'
AND COLUMN_NAME = 'column_name'
Eso funcionó para mí.
Gracias!
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
2011-05-10 13:53:17
Lo siguiente es otra forma de hacerlo usando PHP sin la base de datos information_schema:
$chkcol = mysql_query("SELECT * FROM `my_table_name` LIMIT 1");
$mycol = mysql_fetch_array($chkcol);
if(!isset($mycol['my_new_column']))
mysql_query("ALTER TABLE `my_table_name` ADD `my_new_column` BOOL NOT NULL DEFAULT '0'");
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
2012-04-06 16:06:08
Seleccione solo column_name del esquema de información y coloque el resultado de esta consulta en variable. Luego pruebe la variable para decidir si la tabla necesita alteración o no.
P.d. No se olvide de especificar TABLE_SCHEMA para la tabla COLUMNAS también.
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
2010-08-03 10:55:55
Solo para ayudar a cualquiera que esté buscando un ejemplo concreto de lo que @Mchi estaba describiendo, intente algo como
SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_table' AND COLUMN_NAME = 'my_column'
Si devuelve false (cero resultados) entonces sabes que la columna no existe.
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
2011-04-25 22:33:16
Lancé este procedimiento almacenado junto con un comienzo de los comentarios de @lain anteriores, algo bueno si necesita llamarlo más de unas cuantas veces (y no necesita php):
delimiter //
-- ------------------------------------------------------------
-- Use the inforamtion_schema to tell if a field exists.
-- Optional param dbName, defaults to current database
-- ------------------------------------------------------------
CREATE PROCEDURE fieldExists (
OUT _exists BOOLEAN, -- return value
IN tableName CHAR(255), -- name of table to look for
IN columnName CHAR(255), -- name of column to look for
IN dbName CHAR(255) -- optional specific db
) BEGIN
-- try to lookup db if none provided
SET @_dbName := IF(dbName IS NULL, database(), dbName);
IF CHAR_LENGTH(@_dbName) = 0
THEN -- no specific or current db to check against
SELECT FALSE INTO _exists;
ELSE -- we have a db to work with
SELECT IF(count(*) > 0, TRUE, FALSE) INTO _exists
FROM information_schema.COLUMNS c
WHERE
c.TABLE_SCHEMA = @_dbName
AND c.TABLE_NAME = tableName
AND c.COLUMN_NAME = columnName;
END IF;
END //
delimiter ;
Trabajando con fieldExists
mysql> call fieldExists(@_exists, 'jos_vm_product', 'child_option', NULL) //
Query OK, 0 rows affected (0.01 sec)
mysql> select @_exists //
+----------+
| @_exists |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> call fieldExists(@_exists, 'jos_vm_product', 'child_options', 'etrophies') //
Query OK, 0 rows affected (0.01 sec)
mysql> select @_exists //
+----------+
| @_exists |
+----------+
| 1 |
+----------+
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
2012-05-28 22:10:32
Estoy usando este sencillo script:
mysql_query("select $column from $table") or mysql_query("alter table $table add $column varchar (20)");
Funciona si ya está conectado a la base de datos.
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-03-01 15:55:09
NO coloque ALTER TABLE/MODIFY COLS ni ninguna otra operación mod de tabla dentro de una TRANSACCIÓN. Las transacciones son para poder revertir un fallo de CONSULTA no para ALTERations...it se producirá un error cada vez en una transacción.
Simplemente ejecute una consulta SELECT * en la tabla y verifique si la columna está allí...
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-06-21 16:58:12
Muchas gracias a Mfoo que ha puesto el script realmente bonito para añadir columnas dinámicamente si no existe en la tabla. He mejorado su respuesta con PHP. El script además le ayuda a encontrar cuántas tablas realmente se necesitan 'Add column' mysql comand. Solo prueba la receta. Funciona como el encanto.
<?php
ini_set('max_execution_time', 0);
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'books';
$con = mysqli_connect($host, $username, $password);
if(!$con) { echo "Cannot connect to the database ";die();}
mysqli_select_db($con, $database);
$result=mysqli_query($con, 'show tables');
$tableArray = array();
while($tables = mysqli_fetch_row($result))
{
$tableArray[] = $tables[0];
}
$already = 0;
$new = 0;
for($rs = 0; $rs < count($tableArray); $rs++)
{
$exists = FALSE;
$result = mysqli_query($con, "SHOW COLUMNS FROM ".$tableArray[$rs]." LIKE 'tags'");
$exists = (mysqli_num_rows($result))?TRUE:FALSE;
if($exists == FALSE)
{
mysqli_query($con, "ALTER TABLE ".$tableArray[$rs]." ADD COLUMN tags VARCHAR(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL");
++$new;
echo '#'.$new.' Table DONE!<br/>';
}
else
{
++$already;
echo '#'.$already.' Field defined alrady!<br/>';
}
echo '<br/>';
}
?>
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
2014-02-14 18:04:41
Este trabajo para mí con la muestra DOP:
public function GetTableColumn() {
$query = $this->db->prepare("SHOW COLUMNS FROM `what_table` LIKE 'what_column'");
try{
$query->execute();
if($query->fetchColumn()) { return 1; }else{ return 0; }
}catch(PDOException $e){die($e->getMessage());}
}
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-17 14:26:36