在ALTER TABLE - mysql之前检查列是否存在

jul*_*lio 20 mysql alter-table

有没有办法在ALTER TABLE ADD coumn_name语句运行之前(或作为)运行时检查mySQL数据库中是否存在列?一IF column DOES NOT EXIST ALTER TABLE件事.

我已经尝试ALTER IGNORE TABLE my_table ADD my_column但如果我添加的列已经存在,这仍然会引发错误.

编辑:用例是升级已安装的Web应用程序中的表 - 为了简单起见,我想确保我需要的列存在,如果不存在,请使用 ALTER TABLE

ger*_*rdw 9

由于mysql控制语句(例如"IF")仅在存储过程中起作用,因此可以创建并执行临时控制语句:

DROP PROCEDURE IF EXISTS add_version_to_actor;

DELIMITER $$

CREATE DEFINER=CURRENT_USER PROCEDURE add_version_to_actor ( ) 
BEGIN
DECLARE colName TEXT;
SELECT column_name INTO colName
FROM information_schema.columns 
WHERE table_schema = 'connjur'
    AND table_name = 'actor'
AND column_name = 'version';

IF colName is null THEN 
    ALTER TABLE  actor ADD  version TINYINT NOT NULL DEFAULT  '1' COMMENT  'code version of actor when stored';
END IF; 
END$$

DELIMITER ;

CALL add_version_to_actor;

DROP PROCEDURE add_version_to_actor;
Run Code Online (Sandbox Code Playgroud)


Non*_*nym 8

你认为你可以尝试这个吗?:

SELECT IFNULL(column_name, '') INTO @colName
FROM information_schema.columns 
WHERE table_name = 'my_table'
AND column_name = 'my_column';

IF @colName = '' THEN 
    -- ALTER COMMAND GOES HERE --
END IF;
Run Code Online (Sandbox Code Playgroud)

这不是单行,但你至少可以看看它是否适合你?至少在等待更好的解决方案时......


Wil*_*ler 6

实用功能和程序

首先,我有一组实用程序函数和过程,用于执行诸如删除外键,普通键和列之类的操作.我只是将它们留在数据库中,以便我可以根据需要使用它们.

他们来了.

delimiter $$

create function column_exists(ptable text, pcolumn text)
  returns bool
  reads sql data
begin
  declare result bool;
  select
    count(*)
  into
    result
  from
    information_schema.columns
  where
    `table_schema` = 'my_database' and
    `table_name` = ptable and
    `column_name` = pcolumn;
  return result;
end $$

create function constraint_exists(ptable text, pconstraint text)
  returns bool
  reads sql data
begin
  declare result bool;
  select
    count(*)
  into
    result
  from
    information_schema.table_constraints
  where
    `constraint_schema` = 'my_database' and
    `table_schema` = 'my_database' and
    `table_name` = ptable and
    `constraint_name` = pconstraint;
  return result;
end $$

create procedure drop_fk_if_exists(ptable text, pconstraint text)
begin
  if constraint_exists(ptable, pconstraint) then
    set @stat = concat('alter table ', ptable, ' drop foreign key ', pconstraint);
    prepare pstat from @stat;
    execute pstat;
  end if;
end $$

create procedure drop_key_if_exists(ptable text, pconstraint text)
begin
  if constraint_exists(ptable, pconstraint) then
    set @stat = concat('alter table ', ptable, ' drop key ', pconstraint);
    prepare pstat from @stat;
    execute pstat;
  end if;
end $$

create procedure drop_column_if_exists(ptable text, pcolumn text)
begin
  if column_exists(ptable, pcolumn) then
    set @stat = concat('alter table ', ptable, ' drop column ', pcolumn);
    prepare pstat from @stat;
    execute pstat;
  end if;
end $$

delimiter ;
Run Code Online (Sandbox Code Playgroud)

使用上面的实用程序删除约束和列

有了这些,使用它们来检查存在的列和约束是非常容易的:

-- Drop service.component_id
call drop_fk_if_exists('service', 'fk_service_1');
call drop_key_if_exists('service', 'component_id');
call drop_column_if_exists('service', 'component_id');

-- Drop commit.component_id
call drop_fk_if_exists('commit', 'commit_ibfk_1');
call drop_key_if_exists('commit', 'commit_idx1');
call drop_column_if_exists('commit', 'component_id');

-- Drop component.application_id
call drop_fk_if_exists('component', 'fk_component_1');
call drop_key_if_exists('component', 'application_id');
call drop_column_if_exists('component', 'application_id');
Run Code Online (Sandbox Code Playgroud)


小智 5

用下面约翰·沃森 (John Watson) 的例子来造一个计数句子。

 SELECT count(*) FROM information_schema.COLUMNS
     WHERE COLUMN_NAME = '...'
     and TABLE_NAME = '...'
     and TABLE_SCHEMA = '...'
Run Code Online (Sandbox Code Playgroud)

将该结果保存为整数,然后将其作为应用该ADD COLUMN句子的条件。

  • 您能否详细说明如何保存结果并将其作为应用 ADD COLUMN 的条件? (2认同)