Add a Column to a table if not exists


MySQL allows you to create a table if it does not exist, but does not provide a native way of a adding a column (i.e. a field) to an existing table with a test of whether the column already exists - so as to avoid an error if the column already exists. The ability to add a column if it does not exist can be useful for upgrade scripts and the like.

The following script creates a stored procedure that allows a column to be added to a table but only if it does not already exist:

drop procedure if exists AddColumnUnlessExists;
create procedure AddColumnUnlessExists(
IN dbName tinytext,
IN tableName tinytext,
IN fieldName tinytext,
IN fieldDef text)
begin
IF NOT EXISTS (
SELECT * FROM information_schema.COLUMNS
WHERE column_name=fieldName
and table_name=tableName
and table_schema=dbName
)
THEN
set @ddl=CONCAT('ALTER TABLE ',dbName,'.',tableName,
' ADD COLUMN ',fieldName,' ',fieldDef);
prepare stmt from @ddl;
execute stmt;
END IF;
end;

  

This stored procedure provides the functionality for "add column if not exists". To use the script call it with the name of the database, name of the table, name of the field and the field definition to use if the field is to be created. For example:

call AddColumnUnlessExists(Database(), 'accounts', 'dob', 'varchar(32) null');

which will add the field "dob" to the table "accounts" in the current database, unless it already exists, or

call AddColumnUnlessExists('GIS', 'boundaries', 'fillColour', 'int unsigned not null default 1');

which will add the field "fillColour" to the table "boundaries" in the database "GIS" if it does not already exist.

If you want to drop the stored procedure after use then use:

drop procedure AddColumnUnlessExists;

Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists的更多相关文章

  1. 从官方文档中探索MySQL分页的几种方式及分页优化

    概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...

  2. 手把手教你看MySQL官方文档

    前言: 在学习和使用MySQL的过程中,难免会遇到各种问题.不知道当你遇到相关问题时会怎么做,我在工作或写文章的过程中,遇到不懂或需要求证的问题时通常会去查阅官方文档.慢慢的,阅读文档也有了一些经验, ...

  3. MySQL8.0.28安装教程全程参考MySQL官方文档

    前言 为了MySQL8.0.28安装教程我竟然在MySQL官方文档逛了一天,至此献给想入门MySQL8.0的初学者.以目前最新版本的MySQL8.0.28为示例进行安装与初步使用的详细讲解,面向初学者 ...

  4. swift官方文档中的函数闭包是怎么理解的?

    官方文档中的16页: numbers.map({ (number: Int) -> Int in let result = * number return result }) 不知道这个怎么用, ...

  5. swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?

    在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...

  6. 看MySQL官方文档的示例SQL有感

    [背景] 周末比较闲,我这个人又没有什么爱好,当然了读书除外:前一些天我一个同事说:“你一个dba想去写一本“django”书,合适吗?” 我想也是,一个人不能忘了本,所以MySQL还是要好好的搞一搞 ...

  7. tensorflow官方文档中的sub 和mul中的函数已经在API中改名了

    在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...

  8. 【采坑小计】thanos receiver的官方文档中,并未说明tsdb落盘的配置方式

    官方文档的地址在:https://thanos.io/tip/components/receive.md/ 一开始以为落盘的时间间隔是:--tsdb.retention=15d 实际测试中发现,tha ...

  9. 使用MySQL Yum存储库的快速指南【mysql官方文档】

    使用MySQL Yum存储库的快速指南 抽象 MySQL Yum存储库提供用于在Linux平台上安装MySQL服务器,客户端和其他组件的RPM包.这些软件包还可以升级和替换从Linux发行版本机软件存 ...

随机推荐

  1. Android控件之ImageSwticher

    Android控件之ImageSwticher 1. ImageSwticher介绍 ImageSwitcher是图片切换的控件,它能实现图片切换时的动画效果,包括图片导入效果.图片消失效果等等.An ...

  2. 由pushViewController说起可能出线的各种死法

    做苹果开发或者果粉对导航条这个东西应该都不陌生,这咚咚在小小的屏幕上通过一个简单的View的队列管理来做到手机界面的有条理管理,但是开发过程程序员可能碰到各种死法,下面分享一二.            ...

  3. C#编程(六十)----------LINQ的概述

    LINQ的概述 LINQ的全名为语言继承查询,是VS2008个.NET3.5版中一款突破性的创新,他再对象领域和数据领域之间架起了一座桥梁.使用LINQ能大大加快对于对象数据等等的查询,加快效率. 由 ...

  4. 应收事物处理删除 SQL 语句

    /* Formatted on 2018/3/15 10:07:48 (QP5 v5.256.13226.35538) */ --组织表 SELECT * FROM hr_organization_u ...

  5. 架构:The Clean Architecture(整洁的架构)(转载)

    地址:http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html. Over the last several ...

  6. java过滤特殊字符的正则表达式

    // 过滤特殊字符 public staticString StringFilter(String str) throws PatternSyntaxException { // 只允许字母和数字 / ...

  7. UITableViewCell图片视差效果

    UITableViewCell图片视差效果 效果 源码 https://github.com/YouXianMing/Animations 细节 OffsetImageCell.m OffsetCel ...

  8. 血族第四季/全集The Strain迅雷下载

    当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...

  9. Android之Android apk动态加载机制的研究

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/22597587 (来自singwhatiwanna的csdn博客) 背景 问题 ...

  10. 详细解读Android中的搜索框(二)—— Search Dialog

    Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...