Operation of MYSQL Index

Source: Internet
Author: User
Tags dname mysql index

A: why

Because the data is stored in a database table, the index is created on a database table object, consisting of keys generated by one field or multiple fields in the table, stored in a data structure (b-tree or hash table) that can quickly and efficiently find fields associated with a key value through MySQL. Depending on the storage type of the index, you can divide the index into

B-Tree index (BTREE)

Hashed index (hash)

Note: The InnoDB and MyISAM storage engines support the Btree type index, and the memory storage engine supports hash type indexes, which are indexed by default.


II: what

MySQL supports 6 kinds of indexes: Normal index unique index full-text index single index multi-column index spatial index


Three: how

Creating and viewing Indexes


Create indexes when creating tables

CREATE TABLE table_name (

Property name Data type,

...

Property name Data type,

Index|key "Index Name" (Property name "(length)" "asc| DESC ") #普通索引

Unique Index|key "Index name" (Property name 1 "(length)" "(asc| DESC) "#唯一索引

Fulltext Index|key "index name" (Property name 1 "(length)" "(asc| DESC) "#全文索引


);


CREATE TABLE T_dept (

Deptno int,

Dname varchar (20),

...

Index Index_deptno (DEPTNO)

Unique index Index_deptno (deptno)

Fulltext Index Index_loc (Loc)

);


Show CREATE table T_dept \g;

Explain

SELECT * from t_dept where deptno=1\g;


Create a normal index on a table that already exists

CREATE Index Index name

Create unique index index name

On table name (property name "(length)" "asc| DESC ")

///...

CREATE INDEX Index_deptno

Create UNIQUE index Index_deptno

On t_dept (DEPTNO);

///...

To create a normal index from an SQL statement ALTER TABLE

ALTER TABLE table_name

ADD index| KEY Index Name (property name "(length)" "(asc| DESC) ";

ADD UNIQUE index| KEY Index Name (property name "(length)" "(asc| DESC) "

///...

ALTER TABLE t_dept

Add index Index_deptno (DEPTNO);

Add unique index Index_deptno (DEPTNO);

///...


Create and view multi-column Indexes

#1:

CREATE TABLE T_dept (

Deptno int,

Dname varchar (20),

Loc varchar (40),

Key Index_dname_loc (Dname,loc)

);


#2:

CREATE INDEX Index_dname_loc

On t_dept (DNAME,LOC);


#3:

ALTER TABLE t_dept

Add index Index_dname_loc (dname,loc);


Delete Index

Drop INDEX Index_name

On table_name



































Operation of MYSQL Index

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.