添加列
alter table 表名 add column 字段名 varchar(20) default null comment '字段注释' ;
添加索引
# 主键索引
alter table 表名 add PRIMARY KEY (`列名`);
# 唯一索引
alter table 表名 add UNIQUE (`列名`);
# 普通索引
alter table 表名 add INDEX idx_索引名 (`列名`);
# 全文索引
alter table 表名 add FULLTEXT (`列名`);
# 添加多列索引
alter table 表名 add INDEX idx_索引名 (`列名1`,`列名2`,`列名3`);
修改表名
alter table 表名 rename 新表名;
修改表列名
#字段不为空
alter table 表名 change column 列名 新列名 varchar(3) not null default comment '字段注释';
#字段为空
alter table 表名 change column 列名 新列名 varchar(3) default null comment '字段注释';
修改列字段长度
alter table 表名 modify column 列名 varchar(255) default null comment '字段注释' ;
更新数据
update 表名 set 字段名='新数据' where 条件 ;
创建表
create table `表名` (
`主键` bigint(20) NOT NULL AUTO_INCREMENT comment '主键注释',
`字段1` varchar(40) NOT NULL comment '字段1注释', #该字段不为空
`字段2` varchar(40) default null comment '字段2注释', #该字段允许为空
PRIMARY KEY ( `主键`)
) ENGINE=InnoDB AUTO_INCREMENT=95223 DEFAULT CHAREST=utf8 COMMENT='表名注释';
# 主键使用 AUTO_INCREMENT 声明后,插入数据时主键为空也会自动添加