当前位置:Gxlcms > 数据库问题 > mysql 索引

mysql 索引

时间:2021-07-01 10:21:17 帮助过:16人阅读

,     -> name varchar(20) not null,     -> age tinyint(2) not null default ‘0‘,     -> key index_name(name)     -> ); Query OK, 0 rows affected (0.02 sec)   这样我们就为这个表创建了一个主键索引,primary key 普通索引 key  index_name(这个代表索引名)   查看表结构就可以看到KEY字段建立索引了 MUL代表普通索引   建立表之后再通过alter 命令建立索引或者修改索引,但是不建议这样做。比如我们刚才创建的表的主键索引是id,我们想要修改使用其他键作为索引,就必须先将id的索引去掉   删除主键索引: mysql>alter table student drop primary key; 技术分享   给已经创建好的表添加主键 mysql> alter table student change id id int primary key auto_increment; Query OK, 0 rows affected (0.09 sec) Records: 0  Duplicates: 0  Warnings: 0   这个是为我们已经创建好的student表的id字段添加主键   技术分享 修改普通索引,刚才我们的表为字段name 建立了普通索引。如果想要改到其他的字段上我们需要将普通索引删除,然后再为其他字段创建普通索引。 删除普通索引 mysql> alter table student drop index index_name; Query OK, 0 rows affected (0.16 sec) Records: 0  Duplicates: 0  Warnings: 0 技术分享   添加索引 mysql> alter table student add index index_name(name); Query OK, 0 rows affected (0.39 sec) Records: 0  Duplicates: 0  Warnings: 0 如果一个表的某个列比较长,比如是一个博文的话,在上面创建索引,索引也会消耗较大的磁盘空间,因此如果确定了前几个字符是唯一的,就可以为这个列的几个字符创建索引   mysql>create index index_name on student(name(8));   这个语气表示为student表的name字段前8个字符创建索引   查看索引: mysql>show index from student\G 技术分享   为表的字段创建联合索引: 联合索引是什么意思?就是创建索引时候,同时指定几个字段创建索引,这样做是有时候我们可能同时列一起查询的,联合索引允许列上面已经存在索引的情况下再创建联合索引 mysql>create index index_name on student(age,name);   这个表示在age,name上创建普通索引   创建唯一索引: 唯一索引就是比如你在163邮箱申请一个账号时候,每个账号都是唯一 的,已经被使用的用户名,我们就无法使用 mysql> create unique index uni_name on student(name); Query OK, 0 rows affected (0.10 sec) Records: 0  Duplicates: 0  Warnings: 0   技术分享    

mysql 索引

标签:完全   not   查询   prim   file   主键   如何   创建   query   

人气教程排行