一、alter 修改表

1. 修改表名 

alter table student rename to test_student;

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| student          |
| test_field_type  |
| test_null_zero   |
+------------------+
3 rows in set (0.00 sec)

mysql> alter table student rename to test_student;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| test_field_type  |
| test_null_zero   |
| test_student     |
+------------------+
3 rows in set (0.00 sec)

 

 2. 修改表注释   

alter table test_student comment '设计学生表';

mysql> alter table test_student comment '设计学生表';
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| test_field_type  |
| test_null_zero   |
| test_student     |
+------------------+
3 rows in set (0.00 sec)

mysql> show create table test_student;
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table        | Create Table                                                                                                                                                                                                                         |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_student | CREATE TABLE `test_student` (
  `name` varchar(50) DEFAULT NULL,
  `nickname` varchar(50) DEFAULT NULL,
  `age` tinyint(4) DEFAULT NULL,
  `score` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设计学生表'      |
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 

 

二、alter 修改字段

1. 修改字段类型和注释

alter table sys_application modify column app_name varchar(20) COMMENT '应用的名称';

 

2. 修改字段类型

alter table sys_application modify column app_name text;

 

3. 单独修改字段注释 

目前没发现有单独修改字段注释的命令语句。

 

4. 设置字段允许为空

alter table sys_application modify column description varchar(255) null COMMENT '应用描述';

 

5. 增加一个字段,设好数据类型,且不为空,添加注释

alert table sys_application add `url` varchar(255) not null comment '应用访问地址';

 

6. 增加主键 

alter table t_app add aid int(5) not null ,add primary key (aid);

 

7. 增加自增主键

alter table t_app add aid int(5) not null auto_increment ,add primary key (aid);

 

8. 修改为自增主键

alter table t_app modify column aid int(5) auto_increment ;

 

9. 修改字段名字(要重新指定该字段的类型)

alter table t_app change name app_name varchar(20) not null;

 

10. 删除字段

alter table t_app drop aid;

 

11. 在某个字段后增加字段

alter table `t_app` add column gateway_id int not null default 0 AFTER `aid`; #(在哪个字段后面添加)

 

12. 调整字段顺序 

alter table t_app change gateway_id gateway_id int not null after aid ; #(注意gateway_id出现了2次)

 

 

参考推荐:

MySQL 数据类型

MySQL 中的 NULL 和 空字符(”) 区别

MySQL 查询语句取整数或小数

MySQL 正则表达式查询

MySQL 时间函数加减计算

MySQL 日期和时间函数详解

MySQL 连接查询join详解

MySQL 联合查询union详解

MySQL 函数 group_concat

MySQL 中 case when 语句用法

MySQL 中 distinct 和 group by 性能比较

MySQL中distinct和group by过滤删除重复行

MySQL 数据库 User表权限以及用户授权详解

MySQL 字段中区分字符串大小写的解决方法

MySQL 字段的添加前缀、去掉前缀等字符串操作

MySQL中大于小于,IN,OR,BETWEEN性能比较

PHP MySQL中 uft-8中文编码乱码的解决办法

MySQL 中 insert ignore into, replace into 用法总结

Python创建MySQL数据库/表/存储过程

MySQL 存储引擎 TokuDB

单机开启多个 MySQL 实例

MySQL 内存调优

MySQL 执行 SQL 及慢查询监控

MySQL: 一个参数引起的宕机血案

淘宝分享:跳出MySQL的10个大坑

MySQL 删除数据后物理空间未释放

MySQL 删除100G大表中的绝大部分数据

Python连接MySQL、MongoDB、Redis、memcache