What if your database could instantly stop duplicates before they cause trouble?
Why Unique indexes in MySQL? - Purpose & Use Cases
Imagine you have a list of email addresses stored in a simple spreadsheet. You want to make sure no email is entered twice, but you have to check each new entry manually by scanning the entire list every time.
This manual checking is slow and tiring. It's easy to miss duplicates, especially as the list grows. Mistakes can cause confusion, lost messages, or even security problems if two users share the same email by accident.
Unique indexes automatically stop duplicates from being saved in your database. They act like a smart gatekeeper that checks every new entry and only allows it if it's truly unique, saving you time and avoiding errors.
Check each new email by scanning the whole list before adding it.
CREATE UNIQUE INDEX idx_email ON users(email);
It lets your database keep data clean and reliable without extra work from you.
When signing up for a website, unique indexes ensure no two users can register with the same email address, preventing account mix-ups.
Manual duplicate checks are slow and error-prone.
Unique indexes automatically prevent duplicate data entries.
This keeps your data accurate and your applications reliable.
Practice
UNIQUE index in MySQL?Solution
Step 1: Understand the role of UNIQUE index
A UNIQUE index ensures that no two rows have the same value in the indexed column(s).Step 2: Compare options with this definition
Only To prevent duplicate values in one or more columns correctly states that UNIQUE prevents duplicates. Others are incorrect or unrelated.Final Answer:
To prevent duplicate values in one or more columns -> Option AQuick Check:
UNIQUE index = prevent duplicates [OK]
- Thinking UNIQUE speeds up inserts
- Believing UNIQUE allows duplicates
- Confusing UNIQUE with automatic deletion
email in a table named users?Solution
Step 1: Recall correct syntax for UNIQUE index creation
The correct syntax is: CREATE UNIQUE INDEX index_name ON table_name(column_name);Step 2: Match options with correct syntax
CREATE UNIQUE INDEX idx_email ON users(email); matches exactly. Others have wrong keyword order or syntax errors.Final Answer:
CREATE UNIQUE INDEX idx_email ON users(email); -> Option CQuick Check:
CREATE UNIQUE INDEX ... ON ... (column) [OK]
- Swapping UNIQUE and INDEX keywords
- Using FROM instead of ON
- Incorrect keyword order
employees with a UNIQUE index on employee_id, what happens if you run this query?INSERT INTO employees (employee_id, name) VALUES (101, 'Alice');
INSERT INTO employees (employee_id, name) VALUES (101, 'Bob');
Solution
Step 1: Understand UNIQUE index effect on inserts
A UNIQUE index on employee_id prevents duplicate values in that column.Step 2: Analyze the inserts
The first insert adds employee_id 101. The second insert tries to add the same employee_id 101, violating the UNIQUE constraint, causing an error.Final Answer:
The second insert fails with a duplicate key error -> Option AQuick Check:
UNIQUE index blocks duplicate inserts [OK]
- Assuming duplicates are allowed
- Thinking second insert overwrites first
- Believing second insert is ignored silently
username but got an error. Which of these is the most likely cause?Solution
Step 1: Understand UNIQUE index creation requirements
Creating a UNIQUE index requires all existing values in the column to be unique.Step 2: Identify common error causes
If duplicates exist, MySQL throws an error when creating the UNIQUE index.Final Answer:
The username column contains duplicate values -> Option BQuick Check:
Duplicates block UNIQUE index creation [OK]
- Assuming index name is mandatory
- Believing UNIQUE not allowed on text columns
- Ignoring existing duplicates
first_name and last_name in the contacts table is unique, but duplicates are still appearing. Which is the best way to fix this?Solution
Step 1: Understand uniqueness on multiple columns
To ensure uniqueness on a combination, a composite UNIQUE index on both columns is needed.Step 2: Analyze other options
Separate UNIQUE indexes allow duplicates across combinations. PRIMARY KEY on one column doesn't cover both. CHECK constraints can't enforce uniqueness.Final Answer:
Create a UNIQUE index on both first_name and last_name columns together -> Option DQuick Check:
Composite UNIQUE index = unique pairs [OK]
- Creating separate UNIQUE indexes instead of composite
- Using PRIMARY KEY on one column only
- Relying on CHECK constraints for uniqueness
Ready to prove it?
Pass the test and this pattern moves unknown
