当前位置:网站首页>[MySQL] index creation, viewing and deletion
[MySQL] index creation, viewing and deletion
2022-07-01 23:03:00 【Zhang Yanwei_ Laura】
List of articles
One 、 Create index
1、 Create indexes while creating tables
Use create table Create table time , In addition to defining the data type of the column , You can also define primary key constraints Foreign key constraints Or uniqueness constraint , No matter what kind of constraint you create , While defining the constraint, it is equivalent to creating an index on the specified column .
① Create a normal index
CREATE TABLE book(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
INDEX idx_bname(book_name)
);
② Create a unique index
Declare a field with a unique index , When adding data , Make sure it's unique , But you can add null
Keywords of unique index :unique
CREATE TABLE book1(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
UNIQUE INDEX uk_idx_cmt(COMMENT)
);
③ Create primary key index
After setting it as the primary key, the database will automatically create an index ,innodb Index for clustering , grammar :
keyword :PRIMARY KEY
# Define the primary key index by defining the primary key constraint
CREATE TABLE book2(
book_id INT PRIMARY KEY ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
Be careful : Delete primary key index : Delete the primary key index by deleting the primary key constraint
ALTER TABLE book2
DROP PRIMARY KEY;
Modify the primary key index : You must delete (drop) The original index , New again (add) Indexes
④ Create a single column index
CREATE TABLE book3(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
UNIQUE INDEX idx_bname(book_name)
);
⑤ Create a federated index
CREATE TABLE book4(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR,
# Declaration index
INDEX mul_bid_bname_info(book_id,book_name,info)
);
⑥ Create full text index
fulltext Full text index can be used for full-text search , And only for char varchar and text Column creation index .
The index is always on the entire column , Local is not supported ( Prefix ) Indexes .
CREATE TABLE test4(
id INT NOT NULL,
NAME CHAR(30) NOT NULL,
age INT NOT NULL,
info VARCHAR(255),
FULLTEXT INDEX futxt_idx_info(info(50))
# info(50) Belong to local ( Prefix ) Indexes , Currently does not support , Only the current column will be searched
)
Full text indexes can create multiple columns
CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR (200),
body TEXT,
FULLTEXT index (title, body)
) ENGINE = INNODB ;
tips: For fuzzy query
like How to query :
SELECT * FROM papers WHERE content LIKE ‘% Query string %’;
Full text citation match+against Mode query :
SELECT * FROM papers WHERE MATCH(title,content) AGAINST (‘ Query string ’);
Be careful
1. Before using full-text indexing , Find out the version support ;
2. Full text index ratio like + % fast N times , But there may be precision problems ;
3. If you need a large amount of data for full-text indexing , It is recommended to add data first , Then create the index .
⑦ Create spatial index
The spatial index is being created , The required field of space type must be Non empty .
give an example : Create table test5, The space type is GEOMETRY Create a spatial index on the field of ,SQL The statement is as follows :
CREATE TABLE test5(
geo GEOMETRY NOT NULL,
SPATIAL INDEX spa_idx_geo(geo)
) ENGINE=MyISAM;
2、 Create an index on the created table
To create an index in an existing table, you can use ALTER TABLE Statements or CREATE INDEX sentence .
① Use alter table Statement to create an index
Basic grammar :alter table …add…
# Create data table
CREATE TABLE book5(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
# Create index alter table ...add...
# General index
ALTER TABLE book5 ADD INDEX idx_cmt(COMMENT);
# Uniqueness index
ALTER TABLE book5 ADD UNIQUE uk_idx_bname(book_name);
# Joint index
ALTER TABLE book5 ADD INDEX mul_bid_bname_info(book_id,book_name,info);
② Use create index Create index
Basic grammar :create index…on…
CREATE INDEX Statement can add an index to an existing table , stay MySQL in , CREATE INDEX Mapped to a ALTER TABLE On statement , The basic grammatical structure is :
# Create data table
CREATE TABLE book6(
book_id INT ,
book_name VARCHAR(100),
AUTHORS VARCHAR(100),
info VARCHAR(100) ,
COMMENT VARCHAR(100),
year_publication YEAR
);
# Create index create index...on....
# General index
CREATE INDEX idx_cmt ON book6(COMMENT);
# Uniqueness index
CREATE UNIQUE INDEX uk_idx_bname ON book6(book_name);
# Joint index
CREATE INDEX mul_bid_bname_info ON book6(book_id,book_name,info);
Two 、 Look at the index
View the index through the command
Mode one :
SHOW CREATE TABLE book;
Mode two :
SHOW INDEX FROM book;
3、 ... and 、 Delete index
1、 Use ALTER TABLE Delete index
ALTER TABLE The basic syntax for deleting an index is as follows : ALTER TABLE … DROP INDEX …
ALTER TABLE book5
DROP INDEX idx_cmt;
Tips : add to Auto_increment( Self growth ) The unique index of the constraint field cannot be deleted
2、 Use DROP INDEX Statement to delete the index
DROP INDEX The basic syntax for deleting an index is as follows : DROP INDEX … ON …
DROP INDEX uk_idx_bname ON book5;
tips: Delete related fields in the federated index ( Delete the fields in the table )
Delete related fields in the federated index
ALTER TABLE book5
DROP COLUMN book_name;
Tips : When deleting columns in a table , If the column to be deleted is part of the index , The column is also removed from the index . If all the columns that make up the index are deleted , Then the entire index will be deleted .
边栏推荐
- Flynk SQL client uses comparison and is familiar with official documents
- Quantifiers of regular series
- 正则系列之量词(Quantifiers)
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- Use of locust
- Map container
- Where can the courses purchased by CSDN be accessed
- 今日睡眠质量记录71分
- SAP intelligent robot process automation (IRPA) solution sharing
- [literacy] deep / shallow, local / global features in machine learning image processing
猜你喜欢

Cutefishos system~

Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle

Explain the use of locksupport in detail

软件测试之「 性能测试」总结,新手上路必会知识点

leetcode - 287. Find duplicates

Turn -- use setjmp and longjmp in C language to realize exception capture and collaboration

有些能力,是工作中学不来的,看看这篇超过90%同行

Turn -- bring it and use it: share a gadget for checking memory leaks

正则系列之组和范围(Groups and Ranges)

I graduated from college in 14 years and changed to software testing in 3 months. My monthly salary was 13.5k. At the age of 32, I finally found the right direction
随机推荐
Appium automation test foundation - appium installation (I)
Explain ThreadLocal in detail
Quantifiers of regular series
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
[image segmentation] 2021 segformer neurips
使用3DMax制作一个象棋棋子
力扣 710. 黑名单中的随机数
Multiple smart pointers
通过Go语言创建CA与签发证书
Pytorch's code for visualizing feature maps after training its own network
cvpr2022 human pose estiamtion
毕业季,既是告别,也是新的开始
【Kotlin 第三方 】coil koltin协程图片加载库Coil类似Glide的图片加载第三方
Unable to climb hill sort, directly insert sort
Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine
Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
Using emqx cloud to realize one machine one secret verification of IOT devices
rxjs Observable of 操作符的单步调试分析
Talk about what parameters ZABBIX monitors
Turn -- bring it and use it: share a gadget for checking memory leaks