Home > Database > Mysql Tutorial > How to add index in mysql

How to add index in mysql

青灯夜游
Release: 2020-09-16 14:17:38
Original
31407 people have browsed it

Mysql method of adding an index: You can add it through the [create table] statement, such as [CONSTRAINT PRIMARY KEY | INDEX [] []

How to add index in mysql

In mysql, you can create an index while creating the table (CREATE TABLE); you can also create it after creating the table Index, use CREATE INDEX statement or ALTER TABLE statement.

(Video tutorial recommendation: mysql video tutorial)

1. Use the CREATE INDEX statement

You can use the The CREATE INDEX statement creates an index on an existing table, but this statement cannot create a primary key.

CREATE INDEX [<索引名>] ON <表名> (<列名> [<长度>] [ ASC | DESC])
Copy after login

The syntax is as follows:

: Specify the index name. A table can create multiple indexes, but each index has a unique name in the table.

● 

: Specify the name of the table to create an index.

● : Specify the column name to create an index. You can usually consider using columns that frequently appear in the JOIN clause and WHERE clause in the query statement as index columns.

● : Optional. Specifies that the length characters preceding the column are used to create the index. Creating an index using part of a column can help reduce the size of the index file and save the space occupied by the index columns. In some cases, only the prefix of a column can be indexed. The length of an index column has a maximum limit of 255 bytes (1000 bytes for MyISAM and InnoDB tables). If the length of an index column exceeds this limit, it can only be indexed using the column's prefix. In addition, columns of type BLOB or TEXT must also use prefix indexes.

● ASC|DESC: Optional. ASC specifies that the index is sorted in ascending order, DESC specifies that the index is sorted in descending order, and the default is ASC.

2. Use the CREATE TABLE statement

The index can be created at the same time as creating the table (CREATE TABLE). Syntax format:

1. Create a primary key index

CONSTRAINT PRIMARY KEY [索引类型] (<列名>,…)
Copy after login

When using the CREATE TABLE statement to define column options, you can create a primary key by adding PRIMARY KEY directly after a column definition. When the primary key is a multi-column index composed of multiple columns, this method cannot be used. It can only be implemented by adding a PRIMARY KRY (,...) clause at the end of the statement. .

2. Create a general index

 KEY | INDEX [<索引名>] [<索引类型>] (<列名>,…)
Copy after login

3. Create a unique index

UNIQUE [ INDEX | KEY] [<索引名>] [<索引类型>] (<列名>,…)
Copy after login

4. Create a foreign key index

FOREIGN KEY <索引名> <列名>
Copy after login

Example 1: Create a table tb_stu_info, and create a general index on the height field of the table.

mysql> CREATE TABLE tb_stu_info
    -> (
    -> id INT NOT NULL,
    -> name CHAR(45) DEFAULT NULL,
    -> dept_id INT DEFAULT NULL,
    -> age INT DEFAULT NULL,
    -> height INT DEFAULT NULL,
    -> INDEX(height)
    -> );
Copy after login

Example 2: Create a table tb_stu_info2 and use the UNIQUE keyword to create a unique index on the id field of the table.

mysql> CREATE TABLE tb_stu_info2
    -> (
    -> id INT NOT NULL,
    -> name CHAR(45) DEFAULT NULL,
    -> dept_id INT DEFAULT NULL,
    -> age INT DEFAULT NULL,
    -> height INT DEFAULT NULL,
    -> UNIQUE INDEX(id)
    -> );
Copy after login

3. Use the ALTER TABLE statement

While using the ALTER TABLE statement to modify the table, you can also add changes to the existing table. Add index. The specific method is to add one or more of the following syntax components to the ALTER TABLE statement.

1. Create a primary key index

ADD PRIMARY KEY [<索引类型>] (<列名>,…)
Copy after login

2. Create a general index

ADD INDEX [<索引名>] [<索引类型>] (<列名>,…)
Copy after login

3. Create a unique index

ADD UNIQUE [ INDEX | KEY] [<索引名>] [<索引类型>] (<列名>,…)
Copy after login

4. Create a foreign key index

ADD FOREIGN KEY [<索引名>] (<列名>,…)
Copy after login

Example 1:After creating a table tb_stu_info3, use the UNIQUE keyword to create a unique index on the id field of the table.

mysql> CREATE TABLE tb_stu_info3
    -> (
    -> id INT NOT NULL,
    -> name CHAR(45) DEFAULT NULL,
    -> dept_id INT DEFAULT NULL,
    -> age INT DEFAULT NULL,
    -> height INT DEFAULT NULL,
    -> );
Query OK,0 rows affected (0.40 sec)
mysql>ALTER TABLE tb_stu_info3 ADD UNIQUE (id) ;
Copy after login

4. Display index information

Use the SHOW INDEX command to list the relevant index information in the table. Output information can be formatted by adding \G.

Example:

mysql> SHOW CREATE TABLE tb_stu_info\G
*************************** 1. row ***************************
       Table: tb_stu_info
Create Table: CREATE TABLE `tb_stu_info` (
  `id` int(11) NOT NULL,
  `name` char(45) DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `height` int(11) DEFAULT NULL,
  KEY `height` (`height`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312
1 row in set (0.01 sec)
Copy after login

The above is the detailed content of How to add index in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template