MySQL添加列(如果不存在)

时间:2013-01-17 15:02:27

标签: mysql sql

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_NAME = 'email_subscription' AND COLUMN_NAME = 'subscribe_all')
  THEN 
  ALTER TABLE email_subscription
  ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
  ADD COLUMN subscribe_category varchar(512) DEFAULT NULL;

我看了很多例子。但是这个查询不起作用,我得到错误:

  

错误1064(42000):您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在'IF NOT EXISTS(SELECT * FROM)附近使用的语法   INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='第1行

6 个答案:

答案 0 :(得分:26)

如果你的主机没有授予你创建或运行程序的权限,我想我找到了另一种使用PREPARE / EXECUTE和查询架构的方法:

SET @s = (SELECT IF(
    (SELECT COUNT(*)
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE table_name = 'table_name'
        AND table_schema = DATABASE()
        AND column_name = 'col_name'
    ) > 0,
    "SELECT 1",
    "ALTER TABLE table_name ADD col_name VARCHAR(100)"
));

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

答案 1 :(得分:23)

您可以为查询创建一个程序,

DELIMITER $$
CREATE PROCEDURE Alter_Table()
BEGIN
    DECLARE _count INT;
    SET _count = (  SELECT COUNT(*) 
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE   TABLE_NAME = 'email_subscription' AND 
                            COLUMN_NAME = 'subscribe_all');
    IF _count = 0 THEN
        ALTER TABLE email_subscription
            ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
            ADD COLUMN subscribe_category varchar(512) DEFAULT NULL;
    END IF;
END $$
DELIMITER ;

答案 2 :(得分:4)

您在MySQL中使用MS SQL Server语法。

答案 3 :(得分:3)

在单个MySQL语句中没有相同的语法来实现这一点。

为了得到类似的东西,你可以

1)尝试使用ALTER TABLE添加列,如果表中已存在该名称的列,则让MySQL引发错误,或者

2)查询information_schema.columns视图以检查表中是否存在该名称的列。

请注意,您确实需要检查table_schema以及table_name:

SELECT column_name
  FROM information_schema.columns 
 WHERE table_schema = 'foo'
   AND table_name   = 'email_subscription'
   AND column_name  = 'subscribe_all'

并基于此,决定是否运行ALTER TABLE

答案 4 :(得分:2)

还要添加数据库名称条件以检查列是否存在。

试试这个:

DELIMITER $$
CREATE PROCEDURE sp_AlterTable()
BEGIN
    IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
                   WHERE TABLE_SCHEMA = 'dbName' AND 
                         TABLE_NAME = 'email_subscription' AND 
                         COLUMN_NAME = 'subscribe_all') THEN 
       ALTER TABLE email_subscription
          ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
          ADD COLUMN subscribe_category VARCHAR(512) DEFAULT NULL;
    END IF; 
END $$
DELIMITER ;

答案 5 :(得分:0)

SET @s = (SELECT IF(
    (SELECT COUNT(column_name)
          FROM INFORMATION_SCHEMA.COLUMNS
          WHERE table_name = 'oc_bitcoin_wallets_receive'
          AND table_schema = 'k_opencart2'
          AND column_name = 'test3'
    ) > 0,
    "SELECT 1",
    "ALTER TABLE `oc_bitcoin_wallets_receive` ADD COLUMN `test3` INT NOT NULL AFTER `test2`;"
));
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

编辑值:     oc_bitcoin_wallets_receive - 表名,     k_opencart2 - 数据库名称,     test3 - 新列的名称,     oc_bitcoin_wallets_receive - 第二个位置表     test3 - 第二个位置列,     test2 - 新列之前的列名称。