Mysql数据库的索引和视图具体是怎么样的
admin
2023-03-26 01:41:15
0

下文给大家带来关于Mysql数据库的索引和视图具体是怎么样的,感兴趣的话就一起来看看这篇文章吧,相信看完Mysql数据库的索引和视图具体是怎么样的对大家多少有点帮助吧。

索引的概念

数据库的索引与书籍中的目录类似
在一本书中,无需阅读整本书,利用目录就可以快速查找所需信息
书中的目录是一个词语列表,其中注明了包含各个词的页码
数据库索引
在数据库中,索引数据库程序无需对整个表进行扫描,就可以在其中找到所需数据
数据库中的索引是某个表中一列或若干列的集合,以及物理标识这些值的数据页的逻辑指针清单

索引的作用

设置了合适的索引之后,数据库利用葛总快速的定位技术,能够大大加快查询速率
特别是当表很大时,或者查询涉及到多个表时,使用索引可使查询加快成千倍
可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本
通过创建唯一索引保证数据表数据的唯一性
可以加快表与表之间的连接
在使用分组和排序时,可大大减少分组和排序时间

索引分类

普通索引
这是最基本的索引类型,而且它没有唯一性的限制
唯一性索引
索引的列的所有值都只能出现一次,即必须唯一
主键
主键是一种唯一性索引,但它必须指定为“PRIMARY KEY”
全文索引
全文索引可以在VARCHAR或者TEXT类型的列上创建

创建索引的原则依据

表的主键,外键必须有索引
数据量超过300行的表应该有索引
经常与其他表进行连接的表,在连接字段上应该建立索引
唯一性太差的字段不适合建立索引
更新太频繁的字段不适合创建索引
经常出现在Where字句中的字段,特别是大表的字段,应该建立索引
索引应该建在选择性高的字段上
索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引

索引原理图

Mysql数据库的索引和视图具体是怎么样的

普通索引

普通索引结构语句

create index 索引名字 on tablename(列的列表

create index 索引名字 on tablename(列的列表)
[root@localhost ~]# mysql -u root -p #进入mysql数据库
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;  #查看所有数据库

mysql> use school; #创建school数据库
Database changed
mysql> create table info (    #创建数据表
id int(4) not null primary key auto_increment, #int类型整型为4,不能为空,主键索引,数值自然增长
name varchar(10) not null,  #varchar字符串不能为空
address varchar(50) default 'nanjing',  #字符串默认是nanjing
age int(3) not null); #int类型

Query OK, 0 rows affected (0.05 sec)

mysql> insert into info (name,address,age) values ('zhangsan','beijing',20),('lisi','shanghai',22);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * form info; #查看数据表
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form info' at line 1
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> desc info; #查看表结构
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> create index index_age on info (age); #创建索引固定搭配,index_age索引作用在info表中的age列
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info; #查看数据表中的索引
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY   |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info; #删除数据表中的index_age的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

唯一索引

唯一索引结构语句

create unique index 索引的名字 on tablename (列的列表)

mysql> create unique index unique_name on info (name); #创建唯一索引,create unique index固定搭配起个名字,作用在info的name列中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY     |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> alter table info add unique index index_name (name); #另一种方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

定义索引的三种方式

1.创建表的时候 直接定义
2.create index 索引名称 on 表名 (列名1,列名2);列名可以是多个
3.alter table 表名 add index 索引名称 (列名);

mysql> alter table info add unique index index_name (name); #另一种方法alter table info add 唯一索引 索引名字,作用在name列名中
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> create table user (
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal not null,
    -> hobby int(2) not null default '1',
    -> index index_score (score));  #在创建表的时候可以直接定义索引
Query OK, 0 rows affected (0.05 sec)

mysql> desc user;
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(4)        | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)   | NO   |     | NULL    |                |
| score | decimal(10,0) | NO   | MUL | NULL    |                |
| hobby | int(2)        | NO   |     | 1       |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

做两张表做索引查询,两张表合在一起查看

#要对应着列名填写数据
mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     1 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

再创建一个表做与上一个表做相连查询

mysql> create table hob (  
    -> id int (2) not null primary key,
    -> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.04 sec)

mysql> desc hob;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(2)      | NO   | PRI | NULL    |       |
| hob_name | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into hob (id,hob_name) values (1,'看书'),(2,'运动'),(3,'跑步');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hob;
+----+----------+
| id | hob_name |
+----+----------+
|  1 | 看书     |
|  2 | 运动     |
|  3 | 跑步     |
+----+----------+
3 rows in set (0.00 sec)

mysql> insert into user (name,score,hobby) values ('zhaoliu',66,2); #在user表中再插一行数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from user inner join hob on user.hobby=hob.id; #把user这种表加入到hob表中,user的hobby对应hob里面的id
+----+---------+-------+-------+----+----------+
| id | name    | score | hobby | id | hob_name |
+----+---------+-------+-------+----+----------+
|  1 | test01  |    88 |     1 |  1 | 看书     |
|  2 | stu01   |    99 |     2 |  2 | 运动     |
|  3 | wangwu  |    77 |     3 |  3 | 游     |
|  4 | zhaoliu |    66 |     2 |  2 | 运动     |
+----+---------+-------+-------+----+----------+
4 rows in set (0.00 sec)

看这个表数据觉得不合理,现在需求只要名字和爱好

mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

表名别名关联查询

mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

创捷视图

创建视图结构语句

create view 视图名 as
视图建立一个映射,把结果呈现出来,真实的数据还在原有表中

mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;

Query OK, 0 rows affected (0.00 sec)
mysql> select * from view_user;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 跑步    |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

全文索引

给长的字段,文段做索引

mysql> create fulltext index full_addr on info (address);
Query OK, 0 rows affected, 1 warning (0.21 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | full_addr  |            1 | address     | NULL      |           2 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

组合索引

mysql> create index index_name_score on user (name,score);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from user;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY          |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score      |            1 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            1 | name        | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

4 rows in set (0.00 sec)

看了以上关于Mysql数据库的索引和视图具体是怎么样的详细内容,是否有所收获。如果想要了解更多相关,可以继续关注我们的行业资讯板块。

相关内容

热门资讯

美媒证实一艘美国商船遭伊朗无人... 新华社华盛顿5月10日电 (记者徐剑梅 黄强)据美国福克斯新闻数字网报道,一艘美国商船10日在波斯湾...
特朗普声称伊朗47年来一直在“... 美国总统特朗普于当地时间5月10日在社交媒体发文,“猛烈抨击”伊朗长期“玩弄”美国和世界,同时还痛批...
学生放学回家后又返回学校坠亡,... 学生符某放学后回到家中,后又从家中返回学校,并于当晚从学校教学楼楼顶坠亡。符某父母随后将学校告上法庭...
泽连斯基称乌已向俄方提交100... 当地时间10日,乌克兰总统泽连斯基表示,乌俄双方将以“千人换千人”的方式交换战俘,乌方已向俄方提交了...
国网上海市电力公司举办“明灯引... 5月7日至9日,在第十个“中国品牌日”来临之际,国网上海市电力公司(以下简称“国网上海电力”)以“明...
字跳申请会话信息的发送方法专利... 国家知识产权局信息显示,北京字跳网络技术有限公司申请一项名为“会话信息的发送方法、装置、电子设备、存...
非开挖定向钻机厂家选择指南:郑... 导语:非开挖定向钻机作为市政管道铺设、能源管线穿越等场景的核心设备,其性能稳定性与厂家服务能力直接影...
你昂贵的DDR5内存可能是假货... 快科技5月10日消息,内存价格近期持续走高,亚洲市场出现大量假冒DDR5内存模块,且外观极具迷惑性。...
6G,迎利好!工信部批复 工信部批复6G技术试验频率。 为进一步推动我国6G技术研发、标准研制与产业化进程,工业和信息化部近日...
涉疫邮轮5名法国公民回国,一人... △“洪迪厄斯”号邮轮(资料图)法国总理勒科尔尼10日在社交媒体说,涉汉坦病毒疫情邮轮“洪迪厄斯”号上...