知識積累 MYSQL之索引

2021-09-10 04:23:48 字數 2170 閱讀 4124

#檢視索引

show index from tablename;

#建立復合索引

create unique index indexname on tablename(column1, column2, column3);

#建立復合唯一索引

alter table tablename add unique index indexname on (column1, column2, column3);

#建立主鍵索引

alter table tablename add primary key (id);

#建立唯一索引

alter table tablename add unique index indexname(id);

#建立普通索引

alter table tablename add index indexname(id);

#刪除索引

drop index indexname on tablename;

drop table if exists index_a;

create table index_a(

id bigint(10) not null auto_increment,

username varchar(50) default null comment '使用者名稱',

password varchar(50) default null comment '密碼',

age varchar(2) default null comment '年齡',

mobile varchar(20) default null comment '**', 

create_time timestamp null default current_timestamp comment '建立時間',

update_time timestamp null default null on update current_timestamp comment '修改時間',

primary key (id), #建立主鍵索引

key (username) using btree #建立普通索引

) engine=innodb auto_increment=1 default charset=utf8 comment '使用者表';

insert into index_a(username, password, age, mobile) values ('張三', '123456', '1', '13838380431');

insert into index_a(username, password, age, mobile) values ('李四', '123456', '2', '13838380432');

insert into index_a(username, password, age, mobile) values ('王五', '123456', '3', '13838380433');

insert into index_a(username, password, age, mobile) values ('馬六', '123456', '4', '13838380434');

show index from index_a; 

create index username_password on index_a(username, password); 

alter table index_a add unique index mobile(mobile); 

alter table index_a add index create_time(create_time); 

drop index mobile on index_a; 

select * from index_a;

update index_a set password = '' where id = 1;

delete from index_a where id = 1;

truncate index_a; #清空表

alter table index_a auto_increment = 2; #修改自增下標

mysql積累 索引

參考 索引其實是一種排序的資料結構,能夠幫助我們快速的檢索資料庫中的資料。索引建立經常使用的字段上 不僅僅是經常作為where條件的字段 3.1 hash索引 3.2 b 樹索引 參考 聚簇索引和非聚簇索引,在查詢資料的區別 聚簇索引,就是將索引和資料放到一起,找到索引也就找到了資料,我們剛才看到的...

mysql知識積累

c表的部分資料如下 o表的資料如下 通過mysql語句 update orderpritb o,china jw c set o.longitude c.longitude,o.latitude c.latitude where o.city like c.city and o.province l...

mysql索引知識 mysql索引必會知識點

聚簇索引和非聚簇索引 主要區別在於組織索引的結構是否和資料儲存的結構一樣。一般再innodb中主鍵索引就是聚簇索引,沒有主鍵會預設生成隱藏主鍵字段。理論上也可以指定其他欄位為聚簇索引,聚簇索引也不必唯一。主鍵索引和普通索引 我們都知道,innodb引擎下的儲存結構為b 樹,也就是說首先根據key值一...