是否有“如果不存在"?在 MySql 中添加列的子句? [英] Is there an "IF NOT EXISTS" clause for adding columns in MySql?

查看:53
本文介绍了是否有“如果不存在"?在 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

这篇关于是否有“如果不存在"?在 MySql 中添加列的子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆