是否有“如果不存在"?在 MySql 中添加列的子句?

问题描述:

我需要在一些数据库上运行这个 MySql 代码,我怀疑其中一些已经有了这个列.下面的代码是否有类似于 if not exists 的内容?

I need to run this MySql code on some databases and I suspect that some of them to have already this column. Is there anything similar to if not exists for the below code?

ALTER TABLE 
    `comments` 
ADD COLUMN 
    `active` int(1) NOT NULL DEFAULT '0' AFTER `sid`

我正在考虑这样做以防止出现重复列错误.

I'm considering this to prevent getting the duplicated column error.

它不是内置的,但可以使用 information_schema 数据库来完成:

It's not built-in, but it can be accomplished using the information_schema database:

http://www.cryer.co.uk/brian/mysql/howto_add_column_unless_exists.htm