MySQL, Check if a column exists in a table with SQL

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I thought something like

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; 

would work, but it fails badly. Is there a way?

回答1:

@julio

Thanks for the SQL example. I tried the query and I think it needs a small alteration to get it working properly.

SELECT *  FROM information_schema.COLUMNS  WHERE      TABLE_SCHEMA = 'db_name'  AND TABLE_NAME = 'table_name'  AND COLUMN_NAME = 'column_name' 

That worked for me.

Thanks!



回答2:

This works well for me.

SHOW COLUMNS FROM `table` LIKE 'fieldname'; 

With PHP it would be something like...

$result = mysql_query("SHOW COLUMNS FROM `table` LIKE 'fieldname'"); $exists = (mysql_num_rows($result))?TRUE:FALSE; 


回答3:

Following is another way of doing it using plain PHP without the information_schema database:

$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'"); 


回答4:

Select just column_name from information schema and put the result of this query into variable. Then test the variable to decide if table needs alteration or not.

P.S. Don't foget to specify TABLE_SCHEMA for COLUMNS table as well.



回答5:

Just to help anyone who is looking for a concrete example of what @Mchi was describing, try something like

SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_table' AND COLUMN_NAME = 'my_column'

If it returns false (zero results) then you know the column doesn't exist.



回答6:

I threw this stored procedure together with a start from @lain's comments above, kind of nice if you need to call it more than a few times (and not needing 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 ; 

Working with 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 | +----------+ 


回答7:

I am using this simple script:

mysql_query("select $column from $table") or mysql_query("alter table $table add $column varchar (20)"); 

It works if you are already connected to the database.



回答8:

DO NOT put ALTER TABLE/MODIFY COLS or any other such table mod operations inside a TRANSACTION. Transactions are for being able to roll back a QUERY failure not for ALTERations...it will error out every time in a transaction.

Just run a SELECT * query on the table and check if the column is there...



回答9:

Many thanks to Mfoo who has put the really nice script for adding columns dynamically if not exists in the table. I have improved his answer with PHP. The script additionally helps you find how many tables actually needed 'Add column' mysql comand. Just taste the recipe. Works like charm.

';     }     else     {         ++$already;         echo '#'.$already.' Field defined alrady!
'; } echo '
'; } ?>


回答10:

This work for me with sample PDO :

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());}      } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!