mysql add, modify, delete, query index

First, add an index

1, the general index added / joint index

The first:

format:

ALTER TABLE table name ADD INDEX index name (index field);

ALTER TABLE user ADD INDEX name_index(name);

The second:

format:

Index Name CREATE INDEX ON table (index field);
CREATE INDEX name_index ON user(name);

Note: Add the general index used index, the index field may be separated into a plurality of index for combined comma

2, add a unique index

The first:

format:

ALTER TABLE table name ADD UNIQUE index name (index field);
ALTER TABLE user ADD UNIQUE login_name_index(login_name,user_type);

The second:

format:

Index Name CREATE UNIQUE ON table (index field);
CREATE UNIQUE login_name_index ON user(login_name,user_type);

NOTE: use a unique index UNIQUE, there may be a plurality of index fields separated by commas, that is, when a plurality of combined unique index

Second, remove the index

format:

DROP INDEX index name ON table name;
DROP INDEX login_name_index ON user;

Note: When you delete the index, whether the index is a unique index or ordinary use drop index 

Third, modify the index

mysql does not modify the index in the true sense only after deleting first create a new index can achieve the purpose of modification, because mysql relationship length when creating an index field and so will only create a new index can be created after the deletion new relationships to ensure the accuracy of the index;

Such as: The index modifier login_name_index single unique index;

DROP INDEX login_name_index ON user; 
ALTER TABLE user ADD UNIQUE login_name_index(login_name);

or

DROP INDEX login_name_index ON user;
CREATE INDEX login_name_index ON user(login_name);

Fourth, the query index

The first:

format:

SHOW INDEX FROM 表名;
SHOW INDEX FROM user;

The second:

format:

SHOW KEYS FROM 表名;
SHOW KEYS FROM user;

 

Guess you like

Origin www.cnblogs.com/fatTmonkey/p/11490197.html