QA
Beginner LevelSQL & MySQL

What is the difference between a UNIQUE Constraint and a UNIQUE Index?

❓ Interview Prompt: Compare UNIQUE Constraints (declarative data integrity) vs UNIQUE Indexes (underlying indexing engine implementation).

Comparing unique constraints and unique indexes:

•
UNIQUE Constraint: A declarative database rule enforcing that no two rows can contain duplicate non-null values in specified columns.
•
UNIQUE Index: The underlying physical B-Tree index built by the database engine to enforce uniqueness and accelerate query lookups.
•
Functional Equivalence: In MySQL InnoDB, defining a 'UNIQUE' constraint automatically creates a matching 'UNIQUE B-Tree index' behind the scenes!

Code Implementation Example

sql
ALTER TABLE users
ADD CONSTRAINT uq_users_username UNIQUE (username);
💡Enforcing unique column constraints via ALTER TABLE.
🎯 Interview Key Takeaway

UNIQUE constraints enforce data integrity, automatically creating underlying UNIQUE indexes to accelerate lookups.

#SQL#UNIQUE Constraint#UNIQUE Index#Data Integrity#Beginner
Back to all SQL & MySQL Questions→

More SQL & MySQL Interview Questions

Beginner LevelSQL & MySQL

How do B-Tree Indexes work in SQL/MySQL and how do you optimize slow queries using EXPLAIN?

Beginner LevelSQL & MySQL

How do SELECT, WHERE, ORDER BY, and LIMIT clauses filter relational datasets?

Beginner LevelSQL & MySQL

What are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN?