What if your database could stop duplicates before they even happen?
Why UNIQUE constraint in SQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a guest list for a party written on paper. You want to make sure no one is invited twice, but you have to check every name manually each time you add a new guest.
Manually checking for duplicate names is slow and easy to forget. Mistakes happen, and you might end up inviting the same person twice, causing confusion and extra costs.
The UNIQUE constraint in a database automatically stops duplicate entries in a column. It saves you from checking manually and keeps your data clean and reliable.
INSERT INTO guests (name) VALUES ('Alice'); -- Before inserting, SELECT * FROM guests WHERE name = 'Alice'; -- If no rows, then insert
CREATE TABLE guests ( id INT, name VARCHAR(100) UNIQUE ); INSERT INTO guests (name) VALUES ('Alice'); -- duplicates rejected automatically
It lets you trust your data is unique without extra work, making your applications more reliable and easier to maintain.
When users sign up on a website, the UNIQUE constraint ensures no two accounts use the same email address, preventing login problems and confusion.
Manually avoiding duplicates is slow and error-prone.
UNIQUE constraint automatically enforces uniqueness in data.
This keeps data clean and applications trustworthy.
Practice
UNIQUE constraint in SQL?Solution
Step 1: Understand the UNIQUE constraint and compare to options
The UNIQUE constraint ensures that all values in a column or a set of columns are different from each other. The other options describe different SQL features, but only "To prevent duplicate values in a column or group of columns" correctly describes the UNIQUE constraint.Final Answer:
To prevent duplicate values in a column or group of columns -> Option DQuick Check:
UNIQUE constraint = prevent duplicates [OK]
- Confusing UNIQUE with PRIMARY KEY
- Thinking UNIQUE speeds up queries
- Assuming UNIQUE allows duplicates
email in a table users?Solution
Step 1: Recall ALTER TABLE syntax for UNIQUE and check options
The correct syntax to add a UNIQUE constraint is: ALTER TABLE table_name ADD UNIQUE (column_name); "ALTER TABLE users ADD UNIQUE (email);" matches exactly. The other options misuse keywords or omit parentheses.Final Answer:
ALTER TABLE users ADD UNIQUE (email); -> Option CQuick Check:
ALTER TABLE ADD UNIQUE (column) syntax = ALTER TABLE users ADD UNIQUE (email); [OK]
- Omitting parentheses around column name
- Using ADD CONSTRAINT UNIQUE without naming constraint
- Misplacing keywords in ALTER TABLE
products with a UNIQUE constraint on product_code, what happens when you run this SQL?INSERT INTO products (product_code, name) VALUES ('X123', 'Item A');
INSERT INTO products (product_code, name) VALUES ('X123', 'Item B');Solution
Step 1: Analyze UNIQUE constraint effect on the insert statements
The UNIQUE constraint on product_code means no two rows can have the same product_code value. The first insert adds 'X123' successfully. The second insert tries to add the same product_code 'X123', which violates the UNIQUE constraint, causing an error.Final Answer:
The second insert fails with a UNIQUE constraint violation error -> Option AQuick Check:
Duplicate insert on UNIQUE column = error [OK]
- Assuming duplicates overwrite existing rows
- Thinking duplicates are ignored silently
- Believing both inserts succeed
username but got an error. What is the most likely cause?Solution
Step 1: Understand UNIQUE addition failure and evaluate causes
Adding a UNIQUE constraint fails if existing data violates uniqueness (duplicates exist). "The columnusernamecontains duplicate values already" identifies the cause. A non-existent table is unrelated (assuming it exists); UNIQUE can be added later; indexing is irrelevant or automatic.Final Answer:
The columnusernamecontains duplicate values already -> Option BQuick Check:
Duplicates prevent adding UNIQUE constraint [OK]
- Assuming UNIQUE can't be added after creation
- Ignoring existing duplicate data
- Confusing indexing with UNIQUE constraint
first_name and last_name in the employees table is unique, but duplicates are allowed in each column individually. Which SQL statement correctly enforces this?Solution
Step 1: Understand multi-column UNIQUE and analyze options
A UNIQUE constraint on multiple columns ensures the combination is unique (individual duplicates allowed). "ALTER TABLE employees ADD UNIQUE (first_name, last_name);" correctly adds UNIQUE on the pair. Adding UNIQUE separately disallows individual duplicates; other syntax is wrong or ignores one column.Final Answer:
ALTER TABLE employees ADD UNIQUE (first_name, last_name); -> Option AQuick Check:
Multi-column UNIQUE = unique pairs [OK]
- Adding UNIQUE on each column separately
- Using incorrect syntax without parentheses
- Ignoring the need for combined uniqueness
