WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MySQL · Intermediate · question 33 of 100

Can you explain the concept of indexing, and what are the differences between a unique index and a full-text index in MySQL?

📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

In MySQL, indexing refers to the process of creating an efficient data structure that enables faster searching, filtering, and sorting of data in a database table. Indexing allows MySQL to avoid scanning the entire table to find matching records, which can greatly improve query performance.

There are different types of indexes in MySQL, and two of the most common ones are unique indexes and full-text indexes.

A unique index is an index that enforces uniqueness on a column or a set of columns. It allows the database to ensure that no two rows in the same table have the same value in the indexed column(s). This type of index is useful when you want to enforce data consistency and prevent duplicate values in a column.

For example, let’s say you have a table of users with the following columns: ‘id‘, ‘email‘, and ‘name‘. If you want to ensure that no two users have the same email address, you can create a unique index on the ‘email‘ column:

CREATE UNIQUE INDEX email_unique_idx ON users (email);

Now, if you try to insert a new user with an email address that already exists in the table, MySQL will throw an error and prevent the insertion.

A full-text index, on the other hand, is an index that enables full-text search on one or more columns that contain text data, such as ‘VARCHAR‘, ‘TEXT‘, or ‘BLOB‘. Full-text search is a powerful feature that allows you to search for words or phrases within a text column, even if the words/phrases are not exact matches.

For example, let’s say you have a blog with a ‘posts‘ table that has a ‘content‘ column storing the blog post content. You can create a full-text index on the content column to enable full-text search:

CREATE FULLTEXT INDEX content_fulltext_idx ON posts (content);

Now, you can search for posts that contain a given word or phrase using the ‘MATCH‘ and ‘AGAINST‘ keywords in a ‘SELECT‘ statement:

SELECT * FROM posts WHERE MATCH (content) AGAINST ('cat video');

This query will return all posts that contain the words "cat" and "video", even if they’re not next to each other or are in a different order.

To sum up, a unique index enforces uniqueness on a column or set of columns to prevent duplicates, while a full-text index enables full-text search on a text column to find words or phrases within the text.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MySQL interview — then scores it.
📞 Practice MySQL — free 15 min
📕 Buy this interview preparation book: 100 MySQL questions & answers — PDF + EPUB for $5

All 100 MySQL questions · All topics