案例:创建数据库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的组合索引
几个注意点
(免费学习推荐:mysql视频教程)
(1)登录MysqL数据库
C:\Users\HudIE>MysqL -h localhost -u root -pEnter 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,存储引擎为MyISAMMysqL> 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数据库(视频)
以上是内存溢出为你收集整理的详解MySQL如何创建索引(案例)全部内容,希望文章能够帮你解决详解MySQL如何创建索引(案例)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)