What if your database could stop mistakes before they happen, all by itself?
Why Unique index behavior in SQL? - Purpose & Use Cases
Imagine you have a big notebook where you write down names and phone numbers by hand. You want to make sure no two people have the same phone number, so you have to check every page before adding a new entry.
Checking every page manually is slow and easy to mess up. You might miss duplicates or accidentally write the same number twice, causing confusion and errors later.
A unique index in a database automatically keeps track of all entries and stops duplicates from being added. It works like a smart guard that quickly checks and blocks repeated values without you lifting a finger.
INSERT INTO contacts (name, phone) VALUES ('Alice', '12345'); -- must check manually for duplicates
CREATE UNIQUE INDEX idx_phone ON contacts(phone); -- database prevents duplicates automatically
It lets you trust your data is clean and unique, saving time and avoiding costly mistakes.
When signing up for an email account, the system uses unique indexes to ensure no two users can register with the same email address.
Manual duplicate checks are slow and error-prone.
Unique indexes automatically prevent duplicate values.
This keeps data accurate and reliable without extra effort.
Practice
UNIQUE index in a database table?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 describe unrelated behaviors.Final Answer:
To prevent duplicate values in one or more columns -> Option DQuick Check:
UNIQUE index = prevent duplicates [OK]
- Thinking UNIQUE speeds up insertion
- Confusing UNIQUE with sorting
- Believing UNIQUE allows duplicates
email in a table named users?Solution
Step 1: Recall correct syntax for unique index creation
The standard syntax is: CREATE UNIQUE INDEX index_name ON table_name(column_name);Step 2: Match options to syntax
CREATE UNIQUE INDEX idx_email ON users(email); matches exactly. CREATE INDEX UNIQUE idx_email ON users(email); has wrong keyword order. CREATE UNIQUE KEY idx_email ON users(email); uses UNIQUE KEY which is for constraints, not index creation. CREATE UNIQUE TABLE idx_email ON users(email); is invalid syntax.Final Answer:
CREATE UNIQUE INDEX idx_email ON users(email); -> Option BQuick Check:
CREATE UNIQUE INDEX = CREATE UNIQUE INDEX idx_email ON users(email); [OK]
- Swapping UNIQUE and INDEX keywords
- Using UNIQUE KEY instead of UNIQUE INDEX
- Trying to create UNIQUE TABLE (invalid)
products with a unique index on sku, what happens when you run this SQL?INSERT INTO products (sku, name) VALUES ('A123', 'Item1');
INSERT INTO products (sku, name) VALUES ('A123', 'Item2');Solution
Step 1: Understand unique index effect on inserts
The unique index onskumeans no two rows can have the sameskuvalue.Step 2: Analyze the insert statements
The first insert adds 'A123' successfully. The second tries to insert 'A123' again, violating uniqueness, causing an error.Final Answer:
Second insert fails with a duplicate key error -> Option AQuick Check:
Duplicate insert = error [OK]
- Assuming duplicates are allowed
- Thinking first insert fails
- Believing both inserts fail
username column. The following query causes an error:INSERT INTO users (username) VALUES ('john');
INSERT INTO users (username) VALUES ('john');What is the best way to fix this error?
Solution
Step 1: Identify cause of error
The unique index prevents duplicate usernames. Inserting 'john' twice violates this.Step 2: Choose a fix that respects uniqueness
Changing the second insert to a different username avoids duplicates and keeps data integrity.Final Answer:
Change the second insert to a different username -> Option AQuick Check:
Fix duplicates by changing values [OK]
- Removing unique index (loses data integrity)
- Inserting NULL (may not be allowed or unique)
- Using SELECT instead of INSERT (doesn't fix duplicates)
employees with a unique index on (email, department_id). Which of these inserts will cause a unique index violation if the table already has a row with email='alice@example.com' and department_id=10?Solution
Step 1: Understand composite unique index
The unique index on (email, department_id) means the combination of both values must be unique.Step 2: Check each insert against existing row
Only INSERT INTO employees (email, department_id) VALUES ('alice@example.com', 10); duplicates both email and department_id, causing violation. Others differ in at least one value, so allowed.Final Answer:
INSERT with email='alice@example.com' and department_id=10 causes violation -> Option CQuick Check:
Composite UNIQUE = unique pairs [OK]
- Thinking unique applies to each column separately
- Assuming different department_id allows duplicate email
- Ignoring composite key uniqueness
Ready to prove it?
Pass the test and this pattern moves unknown
