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→