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发行版本机软件存 ...
随机推荐
- C#编程(十九)----------部分类
部分类 C#中使用关键字partial把类,结构或结构放在多个文件中.一般情况下,一个类全部驻留在单个文件中.但有时候,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以 ...
- Java 数组的 12 个最佳方法
1. 声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...
- yii开发第一部分之执行流程
一 目录文件 |-framework 框架核心库 |--base 底层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集,将提供特定功能给整个应用程序),CC ...
- java List集合记录 ArrayList和LinkedList的区别
一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问 ...
- 自定义PreferenceActivity和PreferenceFragment的样式
感谢:http://blog.csdn.net/luck_apple/article/details/7064004 这篇文章讲的是如何定义fragment的样式,基本布局都是从源码中弄过来的.通过设 ...
- mysql 的indexof函数
LOCATE(substr,str) 返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0. mysql> select LOCATE('bar', 'f ...
- Tornado开发技巧,简单了解tornado
tornado基础入门(一)——简单了解tornado 参考:http://demo.pythoner.com/itt2zh/ch1.html tornado是一个轻量级的web框架,是一个用pyth ...
- [转]php的public、protected、private三种访问控制模式的区别
FROM : http://blog.163.com/weiwenjuan_bj/blog/static/14035033620129304183850/?suggestedreading publi ...
- 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.cpp -- 训练
说实话,具体的训练公式,我没有自己推导,姑且认为他写的代码是对的.总体上看,用bp的方法.特殊之处,在于输入层和输出层是完完全全的“同一层”. void dA::get_corrupted_input ...
- Maximum Submatrix & Largest Rectangle
相关题型 问题一(最大和子矩阵) : 有一个 m x n 的矩阵,矩阵的元素可正可负.请找出该矩阵的一个子矩阵(方块),使得其所有元素之和在所有子矩阵中最大.(问题来源:http://acm.pku. ...