Small. Fast. Reliable.
Choose any three.
CREATE INDEX

1. Syntax

create-index-stmt:

CREATE UNIQUE INDEX IF NOT EXISTS schema-name . index-name ON table-name ( indexed-column ) , WHERE expr

expr:

indexed-column:

R-57025-62168:[The CREATE INDEX command consists of the keywords "CREATE INDEX" followed by the name of the new index, the keyword "ON", the name of a previously created table that is to be indexed, and a parenthesized list of table column names and/or expressions that are used for the index key. ] If the optional WHERE clause is included, then the index is a "partial index".

R-16085-53730:[If the optional IF NOT EXISTS clause is present and another index with the same name already exists, then this command becomes a no-op. ]

There are no arbitrary limits on the number of indices that can be attached to a single table. R-13057-33448:[The number of columns in an index is limited to the value set by sqlite3_limit(SQLITE_LIMIT_COLUMN,...). ]

R-25613-37547:[Indexes are removed with the DROP INDEX command. ]

1.1. Unique Indexes

R-06718-34797:[If the UNIQUE keyword appears between CREATE and INDEX then duplicate index entries are not allowed. ] R-17379-32951:[Any attempt to insert a duplicate entry will result in an error. ]

R-55137-26834:[For the purposes of unique indices, all NULL values are considered different from all other NULL values and are thus unique. ] This is one of the two possible interpretations of the SQL-92 standard (the language in the standard is ambiguous). The interpretation used by SQLite is the same and is the interpretation followed by PostgreSQL, MySQL, Firebird, and Oracle. Informix and Microsoft SQL Server follow the other interpretation of the standard, which is that all NULL values are equal to one another.

1.2. Indexes on Expressions

R-11135-63542:[Expressions in an index may not reference other tables and may not use subqueries nor functions whose result might change (ex: random() or sqlite_version()). ] R-40025-59984:[Expressions in an index may only refer to columns in the table that is being indexed. ] Indexes on expression will not work with versions of SQLite prior to version 3.9.0 (2015-10-14). See the Indexes On Expressions document for additional information about using general expressions in CREATE INDEX statements.

1.3. Descending Indexes

R-32925-06786:[Each column name or expression can be followed by one of the "ASC" or "DESC" keywords to indicate sort order. ] R-17151-07205:[The sort order may or may not be ignored depending on the database file format, and in particular the schema format number. ] R-62804-28221:[The "legacy" schema format (1) ignores index sort order. ] R-50227-03668:[The descending index schema format (4) takes index sort order into account. ] Only versions of SQLite 3.3.0 (2006-01-11) and later are able to understand the descending index format. For compatibility, version of SQLite between 3.3.0 and 3.7.9 use the legacy schema format by default. The newer schema format is used by default in version 3.7.10 (2012-01-16) and later. R-50141-17598:[The legacy_file_format pragma can be used to change set the specific behavior for any version of SQLite. ]

1.4. NULLS FIRST and NULLS LAST

The NULLS FIRST and NULLS LAST predicates are not supported for indexes. For sorting purposes, SQLite considers NULL values to be smaller than all other values. Hence NULL values always appear at the beginning of an ASC index and at the end of a DESC index.

1.5. Collations

R-48616-47814:[The COLLATE clause optionally following each column name or expression defines a collating sequence used for text entries in that column. ] R-57876-22123:[The default collating sequence is the collating sequence defined for that column in the CREATE TABLE statement. ] R-09773-40602:[Or if no collating sequence is otherwise defined, the built-in BINARY collating sequence is used. ]