Mysql index types and basic usage examples

Mysql index types and basic usage examples

This article uses examples to illustrate the types and basic usage of MySQL indexes. Share with you for your reference, the details are as follows:

index

MySQL currently has the following main index types:

  • Normal index
  • Unique Index
  • Primary key index
  • Composite Index
  • Full-text index

- General index

Is the most basic index, it has no restrictions.

CREATE INDEX IndexName ON `TableName`(`field name`(length))

- Unique index

Similar to the previous ordinary index, the difference is that the value of the index column must be unique, but null values ​​are allowed. If it is a composite index, the combination of column values ​​must be unique.

CREATE UNIQUE INDEX indexName ON table(column(length))

- Primary key index

It is a special unique index. A table can only have one primary key and no null values ​​are allowed. Generally, the primary key index is created at the same time as the table is created.

CREATE TABLE `table` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  `title` char(255) NOT NULL ,
  PRIMARY KEY (`id`)
);

- Combined index

Refers to an index created on multiple fields. The index will be used only if the first field when the index is created is used in the query condition. Follow the leftmost prefix set when using a composite index

ALTER TABLE `table` ADD INDEX name_city_age (name,city,age);

- Full text index

It is mainly used to find keywords in text rather than directly comparing with values ​​in the index. The fulltext index is very different from other indexes. It is more like a search engine rather than a simple parameter match of a where statement. The fulltext index is used in conjunction with the match against operation, rather than the general where statement plus like. It can be used in create table, alter table, and create index, but currently only char, varchar, and text columns can be used to create full-text indexes. It is worth mentioning that when the amount of data is large, it is much faster to put the data into a table without a global index and then create a fulltext index with CREATE index than to create a fulltext index for a table first and then write the data.

CREATE TABLE `table` (
  `id` int(11) NOT NULL AUTO_INCREMENT ,
  `title` char(255) CHARACTER NOT NULL ,
  `content` text CHARACTER NULL ,
  `time` int(10) NULL DEFAULT NULL ,
  PRIMARY KEY (`id`),
  FULLTEXT (content)
);

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Summary of knowledge points about covering index in MySQL
  • Detailed explanation of MySQL InnoDB index extension
  • MySQL performance optimization: how to use indexes efficiently and correctly
  • Basic syntax of MySQL index
  • Detailed explanation of MySQL combined index method
  • A quick guide to MySQL indexes
  • An article to master MySQL index query optimization skills
  • Detailed explanation of the underlying implementation of descending index, a new feature of MySQL 8
  • Which is faster among MySQL full-text index, joint index, like query, and json query?
  • How to use MySQL covering index and table return
  • Various types of MySQL indexes

<<:  js to realize the rotation of web page pictures

>>:  Solve the problem that Alibaba Cloud SSH remote connection will be disconnected after a short time

Recommend

Detailed explanation of VueRouter routing

Table of contents vue router 1. Understand the co...

This article helps you understand PReact10.5.13 source code

Table of contents render.js part create-context.j...

ElementUI component el-dropdown (pitfall)

Select and change: click to display the current v...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

el-table in vue realizes automatic ceiling effect (supports fixed)

Table of contents Preface Implementation ideas Ef...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

How to create a Pod in Kubernetes

Table of contents How to create a Pod? kubectl to...

Vue implements calling PC camera to take photos in real time

Vue calls the PC camera to take pictures in real ...

How to use CSS styles and selectors

Three ways to use CSS in HTML: 1. Inline style: s...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

React+axios implements github search user function (sample code)

load Request Success Request failed Click cmd and...

A brief discussion on the lock range of MySQL next-key lock

Preface One day, I was suddenly asked about MySQL...

MySQL 8.0.15 installation and configuration graphic tutorial

This article records the installation and configu...

MySQL 5.6.28 installation and configuration tutorial under Linux (Ubuntu)

mysql5.6.28 installation and configuration method...