mysql数据库中怎么创建索引

39次阅读
没有评论

共计 3437 个字符,预计需要花费 9 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章主要介绍 mysql 数据库中怎么创建索引,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

案例:创建数据库 index_test,按照下表的结构在 index_test 数据库中创建两个数据表 test_table1 和 test_table2,并按照操作过程完成对数据表的基本操作。

(1)登录 MySQL 数据库
(2) 创建数据库 index_test
(3)创建表 test_table1
(4)创建表 test_table2,存储引擎为 MyISAM
(5)使用 alter table 语句在表 test_table2 的 birth 字段上建立名称为 ComDateIdx 的普通索引
(6) 使用 alter table 语句在表 test_table2 的 id 字段上添加名称为 UniqIdx2 的唯一索引,并以降序排列
(7) 使用 create index 在 firstname、middlename 和 lastname 三个字段上建立名称为 MultiColidx2 的组合索引
(8) 使用 create index 在 title 字段上建立名称为 FTidx 的全文索引
(9) 使用 alter table 语句删除表 test_table1 中名称为 Uniqidx 的唯一索引
(10) 使用 drop index 语句删除表 test_table2 中名称为 MultiColidx2 的组合索引
几个注意点

(1)登录 MySQL 数据库

C:\Users\Hudie mysql -h localhost -u root -p
Enter password: *******

(2)创建数据库 index_test

mysql  create database index_test;Query OK, 1 row affected (0.06 sec)mysql  use index_test;Database changed

(3)创建表 test_table1

mysql  create table test_table1 -  (
 -  id int not null primary key auto_increment,
 -  name char(100) not null,
 -  address char(100) not null,
 -  description char(100) not null,
 -  unique index uniqidx(id),
 -  index MultiColidx(name(20),address(30) ),
 -  index Comidx(description(30))
 -  );Query OK, 0 rows affected (0.11 sec)mysql  show create table test_table1 \G*************************** 1. row ***************************
 Table: test_table1Create Table: CREATE TABLE `test_table1` ( `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` char(100) NOT NULL,
 `address` char(100) NOT NULL,
 `description` char(100) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `uniqidx` (`id`),
 KEY `MultiColidx` (`name`(20),`address`(30)),
 KEY `Comidx` (`description`(30))) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci1 row in set (0.06 sec)

可以看到在 test_table 表中成功创建了 3 个索引,分别是在 id 字段上名称为 uniqidx 的唯一索引;在 name 和 address 字段上的组合索引;在 description 字段上长度为 30 的普通索引。

(4)创建表 test_table2,存储引擎为 MyISAM

mysql  create table test_table2 -  (
 -  id int not null primary key auto_increment,
 -  firstname char(100) not null,
 -  middlename char(100) not null,
 -  lastname char(100) not null,
 -  birth date not null,
 -  title char(100) null
 -  )ENGINE=MyISAM;Query OK, 0 rows affected (0.07 sec)

(5)使用 alter table 语句在表 test_table2 的 birth 字段上建立名称为 ComDateIdx 的普通索引

mysql  alter table test_table2 add index ComDateidx(birth);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0

(6)使用 alter table 语句在表 test_table2 的 id 字段上添加名称为 Uniqidx2 的唯一索引

mysql  alter table test_table2 add unique index Uniqidx(id);Query OK, 0 rows affected (0.11 sec)Records: 0 Duplicates: 0 Warnings: 0

(7)使用 create index 在 firstname 和 middlename 两个字段上建立名称为 MultiColidx2 的组合索引

mysql  create index MultiColidx2 on test_table2(firstname,middlename);Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0

(8)使用 create index 在 title 字段上建立名称为 FTidx 的全文索引

mysql  create fulltext index ftidx on test_table2(title);Query OK, 0 rows affected (0.13 sec)Records: 0 Duplicates: 0 Warnings: 0

(9)使用 alter table 语句删除表 test_table1 中名称为 Uniqidx 的唯一索引

mysql  alter table test_table1 drop index uniqidx;Query OK, 0 rows affected (0.09 sec)Records: 0 Duplicates: 0 Warnings: 0

(10)使用 drop index 语句删除表 test_table2 中名称为 MultiColidx2 的组合索引

mysql  drop index MultiColidx2 on test_table2;Query OK, 0 rows affected (0.12 sec)Records: 0 Duplicates: 0 Warnings: 0

几个注意点:

1. 索引对数据库的性能如此重要,如何使用它?

如果索引列较少,则需要的磁盘空间和维护开销都较少。

如果在一个大表上创建了多种组合索引,索引文件也会膨胀很快。另外索引较多,可覆盖更多的查询。

尝试添加、删除、修改索引,不影响数据库架构或应用程序设计。

2. 尽量使用短索引

对字符串类型的字段进行索引,如果可能应该指定一个前缀长度。例如,有一个 char(255)的列,如果在前 10 或 30 个字符内多数值是唯一的,就不需要对整个列进行索引。

短索引不仅可以提高查询速度,也能节省磁盘空间、减少 I / O 操作。

以上是“mysql 数据库中怎么创建索引”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注丸趣 TV 行业资讯频道!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计3437字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)