Skip to content

CREATE INDEX

Creates an index on one or more columns of an existing table.

Access SQL. ACE accepts this statement as written here.

CREATE [ UNIQUE ] INDEX index_name
ON table_name ( column_name [ ASC | DESC ] [ , … ] )
[ WITH { PRIMARY | DISALLOW NULL | IGNORE NULL } ]

No two rows may have the same key. A key with a Null in any of its columns is never a duplicate, so (1, Null) can appear twice.

The name of the index. It must be unique among the table’s indexes.

The table to index.

A column in the key, and its sort direction. ASC is the default. A key can have several columns, each with its own direction.

Makes the index the table’s primary key. A primary key is unique, and every key column must have a value. The statement fails if a row already in the table has a Null in a key column.

Every key column must have a value. The statement fails if a row already in the table has a Null in a key column, and so does any later INSERT or UPDATE that would put one there.

Rows whose key is entirely Null are left out of the index. A row with a Null in only some key columns is still indexed.

WITH takes one option.

To create a primary key, unique constraint or foreign key as part of the table definition, use the CONSTRAINT clause of CREATE TABLE or ALTER TABLE instead.

CLUSTERED and NONCLUSTERED are not accepted here, as in ACE.

These run against the Northwind sample database, as in the playground.

Create an index on two columns, the second descending:

CREATE INDEX ixCustomerDate ON Orders (CustomerID, OrderDate DESC)

Create a unique index that leaves out rows with no region:

CREATE TABLE Offices (Id LONG, City TEXT(50), Region TEXT(20))
CREATE UNIQUE INDEX ixRegion ON Offices (Region) WITH IGNORE NULL

Make an existing column the primary key:

CREATE INDEX pkOffices ON Offices (Id) WITH PRIMARY