MySQL
Database Indexing
PRIMARY KEY
UNIQUE INDEX
FULLTEXT INDEX

Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

MySQL is a widely used relational database management system that supports various types of indexes to optimize data retrieval efficiency. Understanding the differences between these indexes—INDEX, PRIMARY, UNIQUE, and FULLTEXT—is crucial for database design and performance tuning. This article delves into each type, offering technical explanations, examples, and comparisons.

General Overview of Indexes

In MySQL, an index is a data structure that improves the speed of data retrieval operations on a table, at the cost of additional storage and overhead on data manipulation. Indexes can significantly enhance query performance, especially on large datasets. The primary types of indexes in MySQL are:

  • INDEX: A general-purpose index that improves the performance of data retrieval.
  • PRIMARY: A unique index that ensures no two rows have the same values in specified columns, also used for table constraints.
  • UNIQUE: An index that ensures all values in the indexed column(s) are distinct.
  • FULLTEXT: An index used for full-text searches, particularly in textual columns.

Basic Syntax for Creating Indexes

sql
1-- Create Index
2CREATE INDEX index_name ON table_name (column_name);
3
4-- Primary Key Example
5CREATE TABLE table_name (
6    column_name data_type,
7    PRIMARY KEY (column_name)
8);
9
10-- Unique Index
11CREATE UNIQUE INDEX index_name ON table_name (column_name);
12
13-- Fulltext Index
14CREATE FULLTEXT INDEX index_name ON table_name (column_name);

Detailed Explanation of Index Types

INDEX

  • Purpose: Improve the speed of queries by creating pointers to data within a table.
  • Usage: Using an INDEX is ideal for fields used often in WHERE clauses. They are also beneficial in joins and frequent sorting operations.
  • Example:
sql
  CREATE INDEX idx_employee_name ON employees (name);
  • Consideration: Adding an index increases the speed of select queries but can affect the performance of insert, update, or delete operations, as the index must be maintained.

PRIMARY

  • Purpose: Designate a unique identifier for each record in a table.
  • Characteristics:
    • Automatically implies the index is unique.
    • No other column or set of columns can hold NULL values.
    • A table can have only one PRIMARY key.
  • Example:
sql
1  CREATE TABLE customers (
2      id INT NOT NULL,
3      name VARCHAR(100),
4      PRIMARY KEY (id)
5  );
  • Consideration: As the primary key uniquely identifies each record, it is often used in joins and database relationships.

UNIQUE

  • Purpose: Ensure all values in a column or a set of columns are distinct across rows.
  • Characteristics:
    • Multiple UNIQUE indexes can be created on a table.
    • Unlike PRIMARY, columns with UNIQUE constraints may contain one NULL value.
  • Example:
sql
  CREATE UNIQUE INDEX idx_unique_email ON users (email);
  • Consideration: Useful in cases where a column value must remain unique, such as an email address in user datasets.

FULLTEXT

  • Purpose: Optimizes text-based searches in MySQL through natural language operations.
  • Characteristics:
    • Applicable to CHAR, VARCHAR, or TEXT columns.
    • Works best when used for searching large text blocks or numerous textual records.
  • Example:
sql
  CREATE FULLTEXT INDEX idx_article_content ON articles (content);
  • Search Example:
sql
  SELECT * FROM articles WHERE MATCH(content) AGAINST('keyword');
  • Consideration: Suitable for complex textual queries where partial keyword matches are necessary.

Comparison Table

Here's a summarized comparison of the different index types:

Index TypeUnique ConstraintAllows NULLsMulti-column AllowedBest Use
INDEXNoYesYesGeneral-purpose
PRIMARYYesNoNo (single constraint per table)Unique record identification
UNIQUEYesYesYesUnique constraint for column(s)
FULLTEXTNoYesYesFull-text search capabilities

Conclusion

Indexes are a powerful feature in MySQL, essential for high-performance applications. By selecting the appropriate index type—whether it's INDEX, PRIMARY, UNIQUE, or FULLTEXT—one can significantly enhance the speed and efficiency of database operations. However, it's crucial to balance the benefits against the overhead introduced by maintaining these indexes, especially in write-heavy environments.


Course illustration
Course illustration

All Rights Reserved.