sql给表增加字段及注释以及索引
sql给表增加字段及注释以及索引
增加单个字段及注释:
ALTER TABLE 表名 ADD 字段名 int(11) DEFAULT NULL COMMENT ‘注释;’;
增加多个字段及注释:
alter table 表名 add
(字段名 int(11) DEFAULT NULL COMMENT ‘注释’,
字段名 varchar(255) DEFAULT NULL COMMENT ‘注释’);
给单个字段添加唯一索引:
alter table 表名 add unique index 索引别名 (列名);
给单个字段添加普通索引:
alter table 表名 add index 索引别名 (列名);
给多个字段添加唯一索引:
alter table 表名 add unique index 索引别名 (列名,列名);
给单个表添加多个索引:
alter table 表名 add index 索引别名 (列名), add index 索引别名2 (列名2);
调整字段顺序:
alter table 表名 change 字段名 新字段名 字段类型 默认值 after 字段名(跳到哪个字段之后)
例子:
alter table t1 change z1 rename_z1 varchar(50) default null AFTER z5
mysql增删改查基本语句:
1、修改表名:
rename table 旧表名 to 新表名;
- 1
2、修改字段类型:
alter table 表名 modify column 字段名 字段类型(长度)
- 1
3、修改字段名称和类型:
alter table 表名 change 现有字段名称 修改后字段名称 数据类型
- 1
4、增加字段:
alter table 表名 add 字段名 字段类型(长度)
- 1
//批量增加字段
alter table 表名 add (字段名1 字段类型(长度),字段名2 字段类型(长度),...)
- 1
5、删除字段:
alter table 表名 drop column 字段名
- 1
//批量删除字段
alter table 表名 drop column 字段名1,drop column 字段名2
- 1
6、修改字段默认值:
alter table 表名 alter column 字段 set default 默认值
- 1
7、添加字段备注:
alter table 表名 add 字段名 字段类型(长度)default null comment '备注'
- 1
// 为表添加注释
```vbnet
alter table 表名 comment '注释';
- 1
- 2
- 3
索引:
1.普通索引 添加index
alter table `表名` add index 索引名 ( `字段名` )
- 1
2.主键索引 添加primary key
alter table `表名` add primary key ( `字段名` )
- 1
3.唯一索引 添加unique
alter table `表名` add unique ( `字段名` )
- 1
4.全文索引 添加fulltext
alter table `表名` add fulltext( `字段名`)
- 1
5.如何添加多列索引
alter table `表名` add index 索引名 ( `字段名`, `字段名`, `字段名` )
更多推荐
所有评论(0)