Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结
2024-10-02 23:45:11
转自:http://www.111cn.net/database/mysql/71648.htm
1.增加一个字段
代码如下 | 复制代码 |
//增加一个字段,默认为空 alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; //增加一个字段,默认不能为空 alter table user add COLUMN new2 VARCHAR(20) NOT NULL; |
2.批量怎加字段
方法一
这里可以使用事务
代码如下 | 复制代码 |
bagin; //事务开始 alter table em_day_data add f_day_house7 int(11); alter table em_day_data add f_day_house8 int(11); alter table em_day_data add f_day_house9 int(11); alter table em_day_data add f_day_house10 int(11); commit; //提交事务,事务结束 |
事务(transaction)是由一系列操作序列构成的程序执行单元,这些操作要么都做,要么都不做,是一个不可分割的工作单位。
方法二
mysql 批量为表添加多个字段
alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));
代码如下 | 复制代码 |
alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11)); |
3.删除一个字段
代码如下 | 复制代码 |
//删除一个字段 alter table user DROP COLUMN new2; |
4.修改一个字段
代码如下 | 复制代码 |
//修改一个字段的类型 alter table user MODIFY new1 VARCHAR(10); //修改一个字段的名称,此时一定要重新指定该字段的类型 alter table user CHANGE new1 new4 int; |
5.批量修改字段名称
代码如下 | 复制代码 |
alter table 表 change 修改前字段名 修改后字段名称 int(11) not null, change 修改前字段名 修改后字段名称 int(11) not null, change 修改前字段名 修改后字段名称 int(11) not null, change 修改前字段名 修改后字段名称 int(11) not null, change 修改前字段名 修改后字段名称 int(11) not null |
例子:
代码如下 | 复制代码 |
alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null, change f_day_house12 f_day_hour12 int(11) not null, change f_day_house13 f_day_hour13 int(11) not null, change f_day_house14 f_day_hour14 int(11) not null, change f_day_house15 f_day_hour15 int(11) not null, change f_day_house16 f_day_hour16 int(11) not null, change f_day_house17 f_day_hour17 int(11) not null |
6,添加注释
代码如下 | 复制代码 |
// 可以为表添加注释 ALTER TABLE `table_name` COMMENT'注释'; // 为字段添加注释,同样适用于修改 ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注释' |
7,调整字段顺序:
alter table 表名
change 字段名 新字段名 字段类型 默认值 after 字段名(跳到哪个字段之后)
例子:
代码如下 | 复制代码 |
alter table appstore_souapp_app_androidmarket; change getPriceCurrency getPriceCurrency varchar(50) default null AFTER getPrice; |
最新文章
- 【转】MySQL索引背后的数据结构及算法原理
- tomcat源码剖析系列
- spring mvc中使用freemark的一点心得
- Charm Bracelet
- Spring MVC 通过@Value注解读取.properties配置内容
- mybatis Mapper XML 文件
- FCFS
- 【Stack Overflow -- 原创加工、原创整理、生产实战】-- 深度复制
- angularjs自定义指令实现分页插件
- 安装memcache及php的memcached模块
- 深入理解Java虚拟机-第1章-走进Java-读书笔记
- Matlab高级绘图
- IE push方法,最后一个参数后面不能跟";,";,否则报语法错误
- c#中的 virtual override 和abstract 以及sealed
- Quest for sane signals in Qt - step 1 (hand coding a Q_OBJECT)
- 混合开发 Hybird Ionic Angular Cordova web 跨平台 MD
- hadoop完全分布式安装部署-笔记
- anu - children
- Ubuntu14.04安装Torch7笔记
- 【Linux】Linux 文件中^M字符处理
热门文章
- 在工作有时候centos6.5系统使用rpm包安装mysql5.7出现的问题
- 基于Netty的私有协议栈的开发
- SVN设置钩子
- 新版Retrofit 2可运行例子(解决Could not locate ResponseBody converter for问题)
- AOP的基本概念
- Sql Server中Float格式转换字符串varchar方法(转)
- [ActionScript 3.0] 对代码加密的有效方法
- nyoj 82 迷宫寻宝(二)
- C# 多线程线程池( 一 )
- Android工作学习第5天之Activity的传值问题