← Back to MySQL Course | Chapter 14: Views & Indexes | Lesson 3 of 5

CREATE INDEX

An index is like the index at the back of a book that lets you jump straight to a page instead of flipping through everything.
Syntax
sql
CREATE [UNIQUE] INDEX index_name
ON table_name (column1[, column2]);

What is an Index?

An index is a separate data structure MySQL maintains alongside a table to make lookups on specific columns much faster, working conceptually like the index at the back of a textbook that points you straight to a page instead of forcing you to scan the whole book.

Example: What is an Index?

sql
CREATE TABLE users (id INT, email TEXT);
CREATE INDEX idx_email ON users (email);

Creating a UNIQUE Index

A UNIQUE index does double duty: it speeds up lookups on the indexed column the same way a normal index does, while also enforcing that no two rows can ever store the same value in that column.

Example: Creating a UNIQUE Index

sql
CREATE TABLE users (id INT, email TEXT);
CREATE UNIQUE INDEX idx_unique_email ON users (email);

Creating a Composite Index

A composite index spans multiple columns at once and is most effective when queries filter on those same columns together, in the same order they appear in the index definition.

Example: Creating a Composite Index

sql
CREATE TABLE orders (id INT, customer_id INT, order_date DATE);
CREATE INDEX idx_customer_date ON orders (customer_id, order_date);

Adding Indexes Using ALTER TABLE

You aren't limited to defining indexes only when a table is first created — ALTER TABLE lets you add an index to a table that already exists and already holds data.

Example: Adding Indexes Using ALTER TABLE

sql
CREATE TABLE products (id INT, sku TEXT);
INSERT INTO products VALUES (1, 'SKU123');
ALTER TABLE products ADD INDEX idx_sku (sku);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Indexing Prefix Text Parts

For very long text columns, indexing just the first several characters (a prefix index) keeps the index small and fast while still speeding up most searches, trading a little precision for a large storage savings.

Example: Indexing Prefix Text Parts

sql
CREATE TABLE articles (id INT, title VARCHAR(255));
CREATE INDEX idx_title_prefix ON articles (title(20));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Indexing a TEXT column without a prefix length, like (email(100)), which fails with BLOB/TEXT column used in key specification without a key length.
  2. Adding an index to every column, which slows down INSERT, UPDATE, and DELETE and wastes storage.
  3. Creating a composite index in the wrong column order, so queries that filter only on the second column cannot use it.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.