MySQL 修改表命令

修改数据表的存储引擎

describe `Reports`;
show table status like 'reports';
select * from information_schema.`TABLE_CONSTRAINTS` where table_schema='northwind';

alter table `reports` engine='myisam';
alter table `reports` engine='InnoDB';

删除表

drop table `Reports`; #删除数据表,不能删除有外键约束的表

修改表名

alter table `Reports` rename `Reports2`;

修改表字段

alter table `Reports` add `column1` nchar(5) null; #新增列
alter table `Reports` modify `column1` nvarchar(10); #修改列属性
alter table `Reports` change `column1` `column2` nvarchar(10); #修改列名
alter table `Reports` change `column2` `column3` nchar(5); #修改列名与类型
alter table `Reports` drop column `column3`; #删除列

修改表和字段注释

alter table `Reports` modify column `Name` nvarchar(30) comment '修改后的字段注释'; #修改字段的注释
alter table `Reports` comment '修改后的表的注释'; #修改表的注释

增删约束

alter table `Reports` add constraint `uniq_1` unique(`Name`); #新增约束
alter table `Reports` drop index `uniq_1`; #删除约束

增删索引

create index `CategoryNameIndex` on `Categories`(`CategoryName`(10)); #创建一个普通索引,使用列名前10个字符
show index from `Categories`; #查看指定表的索引
drop index `CategoryNameIndex` on `Categories`;

create unique index `NameIndex` on `Reports`(`Name`); #创建唯一索引
show index from `Reports`;
drop index `NameIndex` on `Reports`;

create unique index `NameFileIndex` on `Categories`(`CategoryName`,`PictureFile`); #创建组合索引
drop index `NameFileIndex` on `Categories`;

普通索引,非聚集索引。InnoDB 默认主键是聚集索引。一个表只能有一个聚集索引(Clustered Index)。

  • 前缀包括每列值的前 length 个字符。
  • blog和text列也可以编制索引,但是必须给出前缀长度。
  • 多数名称的前10个字符不同,所以这个索引不会比使用全名创建的索引慢很多。
  • 使用列部分创建索引可使索引文件大大减小,节省大量磁盘空间,还可能提速 insert 操作。

猜你喜欢

转载自blog.csdn.net/petezh/article/details/81511361