How to create an index

The syntax of the CREATE INDEX statement

       CREATE [UNIQUE] INDEX|KEY indexName ON tableName (columnName1 [ASC|DESC] [, columnName2 [ASC|DESC]]...)
      

A statement that creates an index based on a single column>

      CREATE INDEX customerID ON orders (customerID);
      

A statement that creates a unique index

      CREATE UNIQUE INDEX emailAddress ON customers (emailAddress);
      

A statement that creates an index based on two columns

      CREATE UNIQUE INDEX customerIDorderNumber ON orders (customerID, orderNumber);
      

A statement that creates an index that’s sorted in descending order

      CREATE INDEX orderTotal ON orders (orderTotal DESC);
      

A CREATE TABLE statement that also creates indexes

      CREATE TABLE customers (
        customerID INT NOT NULL AUTO_INCREMENT,
        emailAddress VARCHAR(255) NOT NULL,
        firstName VARCHAR(60)NOT NULL,
        
        PRIMARY KEY (customerID), 
        UNIQUE INDEX emailAddress (emailAddress), 
        INDEX firstName (firstName) 
      );
      

A DROP INDEX statement that drops an index

      DROP INDEX firstName ON customers;
      

Description

Back