ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序

2022-06-20 19:02:02  阅读:170  来源: 互联网

标签:11 int Mysql alter SQL table day 字段名


//增加一个字段,默认为空
alter table user add COLUMN new1 VARCHAR(20) DEFAULT NULL; 
//增加一个字段,默认不能为空
alter table user add COLUMN new2 VARCHAR(20) NOT NULL;

begin;    //事务开始
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;  //提交事务,事务结束

//删除一个字段
alter table user DROP COLUMN new2;

//修改一个字段的类型
alter table user MODIFY new1 VARCHAR(10);
//修改一个字段的名称,此时一定要重新指定该字段的类型
alter table user CHANGE new1 new4 int;

//批量修改字段名称
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

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));

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

// 可以为表添加注释
ALTER TABLE `table_name` COMMENT'注释'; 
// 为字段添加注释,同样适用于修改
ALTER TABLE `table_name` CHANGE `column_name` `column_name` type(longth) UNSIGNED NULL DEFAULT NULL COMMENT '注释'

// 调整字段顺序
alter table 表名
change 字段名 新字段名 字段类型 默认值 after 字段名(跳到哪个字段之后)
例子:
alter table appstore_souapp_app_androidmarket;
change getPriceCurrency getPriceCurrency varchar(50)   default null AFTER getPrice;

 

标签:11,int,Mysql,alter,SQL,table,day,字段名
来源: https://www.cnblogs.com/dsds/p/16394349.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有