Mysql官方文档中争对安全添加列的处理方法。Mysql Add a Column to a table if not exists
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的更多相关文章
- 从官方文档中探索MySQL分页的几种方式及分页优化
概览 相比于Oracle,SQL Server 等数据库,MySQL分页的方式简单得多了,官方自带了分页语法 limit 语句: select * from test_t LIMIT {[offset ...
- 手把手教你看MySQL官方文档
前言: 在学习和使用MySQL的过程中,难免会遇到各种问题.不知道当你遇到相关问题时会怎么做,我在工作或写文章的过程中,遇到不懂或需要求证的问题时通常会去查阅官方文档.慢慢的,阅读文档也有了一些经验, ...
- MySQL8.0.28安装教程全程参考MySQL官方文档
前言 为了MySQL8.0.28安装教程我竟然在MySQL官方文档逛了一天,至此献给想入门MySQL8.0的初学者.以目前最新版本的MySQL8.0.28为示例进行安装与初步使用的详细讲解,面向初学者 ...
- swift官方文档中的函数闭包是怎么理解的?
官方文档中的16页: numbers.map({ (number: Int) -> Int in let result = * number return result }) 不知道这个怎么用, ...
- swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?
在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...
- 看MySQL官方文档的示例SQL有感
[背景] 周末比较闲,我这个人又没有什么爱好,当然了读书除外:前一些天我一个同事说:“你一个dba想去写一本“django”书,合适吗?” 我想也是,一个人不能忘了本,所以MySQL还是要好好的搞一搞 ...
- tensorflow官方文档中的sub 和mul中的函数已经在API中改名了
在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题: 1)AttributeError: module 'tensorflow' ...
- 【采坑小计】thanos receiver的官方文档中,并未说明tsdb落盘的配置方式
官方文档的地址在:https://thanos.io/tip/components/receive.md/ 一开始以为落盘的时间间隔是:--tsdb.retention=15d 实际测试中发现,tha ...
- 使用MySQL Yum存储库的快速指南【mysql官方文档】
使用MySQL Yum存储库的快速指南 抽象 MySQL Yum存储库提供用于在Linux平台上安装MySQL服务器,客户端和其他组件的RPM包.这些软件包还可以升级和替换从Linux发行版本机软件存 ...
随机推荐
- android多设备界面适配的利器:属性weight的妙用
1.按比例显示控件元素 <EditText android:id="@+id/edit_message" android:layout_weight="2" ...
- C#编程(二十六)----------泛型
泛型 有了泛型,就可以创建独立于被包含类型的类和方法了.我们不必给不同的类型编写功能相同的许多方法或类,只创建一个方法或类即可. 另一个减少代码的选项是使用object类,但object类不是类型安全 ...
- 【Devops】【docker】【CI/CD】关于jenkins构建成功后一步,执行的shell命令详解+jenkins容器运行宿主机shell命令的实现方法
1.展示这段shell命令 +详解 #================================================================================= ...
- WordPress主题开发:WP_Query常用参数
常用参数 用途 调用文章或页面 s 查询和某个关键词相关的所有的文章/页面信息 p 文章或页面id post__in 多篇id post__not_in 多篇id以外 post_type 查询的信息类 ...
- python测试开发django-34.xadmin管理后台
前言 django自带的admin后台管理页面有点丑陋,于是网上的大神优化了一版后台管理xadmin,并且开源了,在github开源下载到源码. 注意环境搭配: django 2.1.2 xadmin ...
- Windows 下安装NPM
第一步: 下载node.js的windows版 当前最新版本是https://nodejs.org/dist/ 第二步:设置环境变量 把node.exe所在目录加入到PATH环境变量中. 配置成功 ...
- 移动环境下DNS解析失败后的优化方案
我们手机游戏中,通过上报收集到的数据来分析,发现相当多的一部分用户,在请求一些配置时会遇到无法解析的情况,或者域名的解析直接被拦截了. 特别是游戏的补丁包文件(放在CDN上),遇到的域名解析失败是最多 ...
- MySql和相关驱动的安装方式
下载mySql for java驱动的地址:http://www.mysql.com/products/connector/ (可下可不下,因为安装mySql的时候就会包含了各种驱动) MySQL下载 ...
- django的权限认证:登录和退出。auth模块和@login_required装饰器
在settings.py中配置LOGIN_URL参数: # 用户访问带有(@login_required)标签的页面(view)时,如果没有登录,就会跳转到LOGIN_URL(即登陆url). LOG ...
- 未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
"/"应用程序中的服务器错误. 未能加载文件或程序集"Newtonsoft.Json"或它的某一个依赖项.找到的程序集清单定义与程序集引用不匹配. (异常来自 ...