MySQL 修改表命令

本文介绍了如何在MySQL中修改数据表,包括更改存储引擎、删除表、改表名、改字段、添加删除约束和索引。特别是详细讨论了如何创建前缀索引以优化存储和查询性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改数据表的存储引擎
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 操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值