当前位置:网站首页>[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 .
边栏推荐
- Origin2018 installation tutorial "recommended collection"
- Explain ThreadLocal in detail
- 思科--WAN 的概念考试外部工具
- El input text field word limit, beyond which the display turns red and input is prohibited
- Explain JMM in detail
- Single step debugging analysis of rxjs observable of operator
- 阿洛迷茫后的思考
- [daily training] 326 Power of 3
- The principle, testing and Countermeasures of malicious software reverse closing EDR
- Efficiency improvement - encourage personalized container development environment
猜你喜欢

Cut noodles C language

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines

Share some feelings of a programmer who has experienced layoffs twice a year

Preparation of functional test report

ESP自动下载电路设计

转--原来gdb的底层调试原理这么简单

正则系列之量词(Quantifiers)
![[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party](/img/ad/dcb993c3e1e79d2c3663f031dfd1b3.png)
[kotlin third party] coil koltin collaboration picture loading library coil glide like picture loading third party

Quantifiers of regular series

14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
随机推荐
Deep learning -- data operation
Origin2018 installation tutorial "recommended collection"
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
SAP GUI 里的收藏夹事务码管理工具
数字化转型道阻且长,如何迈好关键的第一步
Share some feelings of a programmer who has experienced layoffs twice a year
91. (cesium chapter) cesium rocket launch simulation
Some abilities can't be learned from work. Look at this article, more than 90% of peers
攻防演习防御体系构建之第三篇之建立实战化的安全体系
Explain the volatile keyword
Contents of other parts of the map
使用3DMax制作一个象棋棋子
Turn -- bring it and use it: share a gadget for checking memory leaks
Ffmpeg learning notes
业务可视化-让你的流程图'Run'起来
Flink SQL command line connection yarn
el-input文本域字数限制,超过显示变红并禁止输入
447 Bili Bili noodles warp 1
测试人进阶技能:单元测试报告应用指南