当前位置:网站首页>[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 .
边栏推荐
- window安装wsl(二)
- SAP intelligent robot process automation (IRPA) solution sharing
- Electron学习(三)之简单交互操作
- 正则系列之组和范围(Groups and Ranges)
- 每日刷题记录 (十)
- Understanding of inverted residuals
- Detailed explanation of twenty common software testing methods (the most complete in History)
- Origin2018 installation tutorial "recommended collection"
- internal field separator
- Today's sleep quality record 71 points
猜你喜欢
Stimulate new kinetic energy and promote digital economy in multiple places
internal field separator
Sogou wechat app reverse (II) so layer
今日睡眠质量记录71分
You probably haven't noticed the very important testing strategy in your work
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁
Preparation of functional test report
常见的二十种软件测试方法详解(史上最全)
Detailed explanation of twenty common software testing methods (the most complete in History)
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
随机推荐
Cloud Vulnerability Global Database
ESP自动下载电路设计
若干互联网暴露面的收敛及处置建议
Understanding of indexes in MySQL
思科--WAN 的概念考试外部工具
447 Bili Bili noodles warp 1
Flink SQL command line connection yarn
Convergence and disposal suggestions of some Internet exposure surfaces
Cisco test -- the concept and configuration test of routing
Map container
cvpr2022 human pose estiamtion
tcpdump命令使用详解
Deep learning -- data operation
Pytorch's code for visualizing feature maps after training its own network
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
Data enhancement of semi supervised learning
[daily training] 66 add one-tenth
下班前几分钟,我弄清了v-model与.sync的区别
毕业季,既是告别,也是新的开始
[image segmentation] 2021 segformer neurips