加入收藏 | 设为首页 | 会员中心 | 我要投稿 武陵站长网 (https://www.50888.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql表索引_MySQL表及索引相关知识

发布时间:2022-10-22 17:32:20 所属栏目:MySql教程 来源:未知
导读: 1.表
1.1)建表
create table student(
id int(4) not null,
name char(20) not null,
age tinyint(2) not null default '0',
dept varchar(16) default null);
show create table

1.表

1.1)建表

create table student(

id int(4) not null,

name char(20) not null,

age tinyint(2) not null default '0',

dept varchar(16) default null);

show create table student\G

1.2)查看建表的结构

desc student;

ab6f5d9c70976182c961f451c31fc052.png

show columns from student;

e5c1601603d334c3b1be309c3635d281.png

1.3)查看已建表的语句

show create table student\G

aabe2c42ca02fc7037af46e3d9554ccf.png

2.索引

2.1 索引类型

1)主键索引:每个表只能有一个主键列

create table student(

id int(4) not null AUTO_INCREMENT,

name char(20) not null,

age tinyint(2) not null default '0',

dept varchar(16) default NULL,

primary key(id),

KEY index_name(name)

);

828ea8c675e4b61cef934bd12eacb799.png

也可后来再添加主键:

alter table student change id id int primary key auto_increment;

6d4fc01873817eda5f289f95a977661e.png

2)普通索引

alter table student drop index index_name; #或者用drop index index_name on student;

alter table student add index index_name(name);

create index index_dept on student(dept(8)); #对dept列的前八个字符创建索引(指定对前n个字符创建索引)

show index from student\G #显示某表中有的索引,mysql默认的索引一般都是BTREE索引

b444ec5937dfc4b8ef6fd8c0223c3a44.png

17332323fe0f4bcc9d3770653550abd7.png

3)联合索引

create index index_name_dept on student(name,dept); #也可限定name的前n个字符和dept的前m个字符

4)唯一索引(非主键索引)

create unique index index_name on student(name);

7d6ccf13b554c3e55381903eff83b30f.png

2.2 索引的创建条件

索引是要占空间的,而且需要维护,因此创建索引要遵循一定条件:

要在表的列上创建索引;

索引会加快查询速度mysql表索引,但是会影响更新速度;

select user,host from mysql.user where host=....索引一定要创建在where后的条件列上,而不是select后的选择数据的列;

尽量选择在唯一值多(比如这个表就男或女两个选项)的大表上的列建立索引。

2018年10月30日

祝好!

(编辑:武陵站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!