• 技术文章 >数据库 >mysql教程

    详解MySQL如何创建索引(案例)

    coldplay.xixicoldplay.xixi2021-03-24 10:48:06转载1817

    案例:创建数据库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 -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.尽量使用短索引

    相关免费学习推荐:mysql数据库(视频)

    以上就是详解MySQL如何创建索引(案例)的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:CSDN,如有侵犯,请联系admin@php.cn删除
    专题推荐:创建索引
    上一篇:一分钟带你了解mysql DDL 下一篇:三分钟带你了解mysql数据类型
    PHP编程就业班

    相关文章推荐

    • 深入理解MySQL索引原理及实现,快速检索数据库• 国内5大搜索引擎是什么• 熟悉MySQL索引• 为什么要使用MySQL索引?

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网