Mysql 常见ALTER TABLE操作

本文详细介绍了SQL中的基本操作,包括删除列、增加列(单列与多列)、修改列的位置、修改字段类型、设置及删除默认值、查看列属性、更改表名以及字段的NULL和NOT NULL约束。这些操作对于数据库管理和维护至关重要。
摘要由CSDN通过智能技术生成

删除列

alter table table_name drop col_name;

增加列(单列)

alter table table_name add col_name col_type comment 'xxx';

增加列(多列)

alter table table_name add col_name col_type comment 'xxx', add col_name col_type(col_length) comment 'xxx';

增加表字段并指明字段放置为第一列

alter table table_name add col_name col_type COMMENT 'sss' FIRST;

增加表字段并指明字段放置为特定列后面

alter table table_name add col_name col_type after col_name_1;

使用MODIFY修改字段类型

alter table table_name modify column col_name col_type;

使用CHANGE修改字段类型

alter table table_name change col_name col_name col_type;

使用CHANGE修改字段名称

alter table table_name change old_col_name new_col_name col_type;

修改列类型、长度

alter table table_name change old_col_name new_col_name new_col_type;

 

查看表中列属性

show columns from table_name;

修改表名

rename table old_table_name to new-table-name;

为字段设置NOT NULL和NULL

alter table table_name modify col_name col_type not null commit '字段注释';

alter table table_name modify col_name col_type default null commit '字段注释';

修改字段的默认值

alter table table_name alter col_name set default 10000;

字段删除默认值 

alter table table_name alter col_name drop default;

 

新增到指定位置语法
alter table app add `name` varchar(64) default '' commit '应用名称' after `app_id`;
修改顺序语法:alter table 表名 change 老字段名 新字段名 字段各种约束 after 字段;
alter table `app` change `title` `title` VARCHAR(64) default '' commit '名称' after `name`;

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BasicLab基础架构实验室

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值