May 15, 2021 MySQL
3. Use the ALTER command to add and remove indexes
The establishment of MySQL index is very important for the efficient operation of MySQL, and index can greatly improve the retrieval speed of MySQL.
For example, if a well-designed and indexed MySQL is a Lamborghini, a MySQL without an index is a man-made tricyle.
Take the catalog page (index) of the Chinese dictionary for example, we can quickly find the required words by pinyin, stroke, side-by-side first-class sort of directory (index).
Indexes are column-by-column and composite indexes. A single-column index, that is, an index contains only a single column, and a table can have multiple single-column indexes, but this is not a composite index. A composite index, that is, an index contains more than one column.
When you create an index, you need to make sure that the index is a condition that is applied to the SQL query statement (generally as a condition of the WHERE clause).
In fact, the index is also a table that holds the primary key and index fields and points to the records of the entity table.
The benefits of using indexes are all talked about above, but excessive use of indexes can lead to abuse. I ndexes therefore have their drawbacks: while indexes greatly increase query speed, they also slow down the updating of tables, such as INSERT, UPDATE, and DELETE. Because when you update a table, MySQL saves not only the data, but also the index file.
Indexed index files that take up disk space.
This is the most basic index, and it has no restrictions. It can be created in several ways:
CREATE INDEX indexName ON mytable(username(length));
In the case of CHAR, VARCHAR type, length can be less than the actual length of the field, and in the case of BLOB and TEXT types, length must be specified.
ALTER table tableName ADD INDEX indexName(columnName)
CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, INDEX [indexName] (username(length)) );
DROP INDEX [indexName] ON mytable;
It is similar to the previous normal index, unlike the value of the index column, which must be unique, but allow for empty values. I f the index is combined, the combination of column values must be unique. It can be created in several ways:
CREATE UNIQUE INDEX indexName ON mytable(username(length))
ALTER table mytable ADD UNIQUE [indexName] (username(length))
CREATE TABLE mytable( ID INT NOT NULL, username VARCHAR(16) NOT NULL, UNIQUE [indexName] (username(length)) );
There are four ways to add an index of a data table:
The following example is to add an index to the table.
mysql> ALTER TABLE testalter_tbl ADD INDEX (c);
You can also use the DROP clause in the ALTER command to remove the index. Try removing the index from the following instance:
mysql> ALTER TABLE testalter_tbl DROP INDEX c;
The primary key can only work on one column, and when you add a primary key index, you need to make sure that the primary key is not empty by default (NOT NULL). Here's an example:
mysql> ALTER TABLE testalter_tbl MODIFY i INT NOT NULL; mysql> ALTER TABLE testalter_tbl ADD PRIMARY KEY (i);
You can also use the ALTER command to remove the primary key:
mysql> ALTER TABLE testalter_tbl DROP PRIMARY KEY;
The primary key only needs to be specified when the PRIMARY KEY is deleted, but you must know the index name when you delete the index.
You can use the SHOW INDEX command to list the relevant index information in the table. You can format the output information by adding a .G.
Try the following example:
mysql> SHOW INDEX FROM table_name; \G ........