📖 Scenario: You are creating a simple database for a small online bookstore. You want to make sure that each book's ISBN number is unique so that no two books can have the same ISBN.
🎯 Goal: Build a MySQL table called books with columns for id, title, and isbn. Add a UNIQUE constraint on the isbn column to prevent duplicate ISBN numbers.
📋 What You'll Learn
Create a table named books with columns id (integer, primary key, auto-increment), title (varchar 100), and isbn (varchar 20).
Add a UNIQUE constraint on the isbn column.
Use standard MySQL syntax.
💡 Why This Matters
🌍 Real World
Ensuring unique values like ISBNs in a bookstore database prevents data errors and duplicates.
💼 Career
Database developers and administrators often use UNIQUE constraints to maintain data integrity in real applications.
Progress0 / 4 steps
1
Create the books table with columns
Write a MySQL statement to create a table called books with three columns: id as an integer primary key with auto-increment, title as a varchar(100), and isbn as a varchar(20).
MySQL
Hint
Use CREATE TABLE with the column definitions separated by commas. Remember to set id as the primary key and auto-increment.
2
Add a UNIQUE constraint on the isbn column
Modify the books table creation statement to add a UNIQUE constraint on the isbn column to ensure no duplicate ISBNs.
MySQL
Hint
Add the keyword UNIQUE right after the isbn VARCHAR(20) column definition.
3
Insert sample data into the books table
Write two INSERT INTO books statements to add these books: 1. Title: 'Learn SQL', ISBN: '123-4567890123' 2. Title: 'Mastering Databases', ISBN: '987-6543210987'
MySQL
Hint
Use two separate INSERT INTO books (title, isbn) VALUES (...) statements with the exact titles and ISBNs given.
4
Try inserting a duplicate ISBN to see the UNIQUE constraint in action
Write an INSERT INTO books statement to add a book with title 'Duplicate ISBN Test' and ISBN '123-4567890123' (which already exists). This should fail due to the UNIQUE constraint.
MySQL
Hint
Try inserting a book with the same ISBN as the first book to see the UNIQUE constraint prevent duplicates.
Practice
(1/5)
1. What is the main purpose of a UNIQUE constraint in MySQL?
easy
A. To prevent duplicate values in a column or set of columns
B. To allow NULL values in a column
C. To speed up query performance
D. To automatically generate unique IDs
Solution
Step 1: Understand the role of UNIQUE constraint
A UNIQUE constraint ensures that all values in a column or a group of columns are different from each other.
Step 2: Compare with other options
Options A, C, and D describe other features: allowing NULLs, indexing, and auto-increment, which are not the main purpose of UNIQUE.
Final Answer:
To prevent duplicate values in a column or set of columns -> Option A
Quick Check:
UNIQUE constraint = prevent duplicates [OK]
Hint: UNIQUE means no two rows share the same value [OK]
Common Mistakes:
Confusing UNIQUE with PRIMARY KEY
Thinking UNIQUE allows duplicates
Assuming UNIQUE speeds up queries
2. Which of the following is the correct syntax to add a UNIQUE constraint to an existing column email in a table users?
easy
A. ALTER TABLE users SET UNIQUE email;
B. ALTER TABLE users ADD UNIQUE (email);
C. ALTER TABLE users ADD UNIQUE email;
D. ALTER TABLE users ADD CONSTRAINT UNIQUE email;
Solution
Step 1: Recall correct ALTER TABLE syntax for UNIQUE
The correct syntax is to use ADD UNIQUE (column_name) inside ALTER TABLE.
Step 2: Check each option
ALTER TABLE users ADD UNIQUE (email); matches the correct syntax. Options A, B, and C are invalid or incomplete syntax.
Final Answer:
ALTER TABLE users ADD UNIQUE (email); -> Option B
Quick Check:
ALTER TABLE ADD UNIQUE (column) [OK]
Hint: Use ADD UNIQUE (column) to add constraint [OK]
Common Mistakes:
Omitting parentheses around column name
Using SET UNIQUE which is invalid
Missing ADD keyword
3. Given the table users(id INT, username VARCHAR(50), email VARCHAR(100)) with a UNIQUE constraint on email, what happens if you run this query twice?
INSERT INTO users (id, username, email) VALUES (1, 'alice', 'alice@example.com');
medium
A. Both inserts succeed, creating two identical rows
B. Both inserts fail due to UNIQUE constraint
C. Second insert updates the existing row
D. First insert succeeds, second insert fails with duplicate error
Solution
Step 1: Understand UNIQUE constraint effect on inserts
The UNIQUE constraint on email prevents duplicate emails. The first insert adds the row successfully.
Step 2: Analyze second insert behavior
The second insert tries to add the same email again, violating the UNIQUE constraint, causing an error.
Final Answer:
First insert succeeds, second insert fails with duplicate error -> Option D
Quick Check:
UNIQUE blocks duplicate inserts [OK]
Hint: Duplicate insert fails due to UNIQUE constraint [OK]
Common Mistakes:
Assuming duplicates are allowed
Thinking second insert updates existing row
Confusing UNIQUE with PRIMARY KEY behavior
4. You have this table creation statement:
CREATE TABLE products (id INT, code VARCHAR(20) UNIQUE);
You try to insert two rows:
INSERT INTO products VALUES (1, 'X123'); INSERT INTO products VALUES (2, 'X123');
But the second insert fails. What is the most likely cause?
medium
A. The UNIQUE constraint on code prevents duplicate values
B. The id column is missing a PRIMARY KEY
C. The table does not allow NULL values in code
D. The syntax of the INSERT statement is incorrect
Solution
Step 1: Identify the UNIQUE constraint effect
The code column has a UNIQUE constraint, so duplicate values like 'X123' are not allowed.
Step 2: Analyze the insert failure reason
The second insert tries to add 'X123' again, violating the UNIQUE constraint, causing failure.
Final Answer:
The UNIQUE constraint on code prevents duplicate values -> Option A
Hint: UNIQUE blocks duplicate values in that column [OK]
Common Mistakes:
Thinking PRIMARY KEY is required for uniqueness
Assuming NULL disallows duplicates
Blaming syntax error for insert failure
5. You want to ensure that the combination of first_name and last_name in a contacts table is unique, but each column alone can have duplicates. Which is the correct way to enforce this in MySQL?
hard
A. Add a PRIMARY KEY on first_name
B. Add UNIQUE constraints separately on first_name and last_name
C. Add a UNIQUE constraint on (first_name, last_name) together
D. Add a UNIQUE constraint only on last_name
Solution
Step 1: Understand composite UNIQUE constraints
A UNIQUE constraint on multiple columns ensures the combination is unique, but allows duplicates in each column individually.
Step 2: Evaluate options for enforcing uniqueness
Add a UNIQUE constraint on (first_name, last_name) together correctly adds a UNIQUE constraint on the pair (first_name, last_name). Options B, C, and D enforce uniqueness incorrectly or partially.
Final Answer:
Add a UNIQUE constraint on (first_name, last_name) together -> Option C
Quick Check:
Composite UNIQUE = unique pairs, not individual columns [OK]
Hint: Use UNIQUE on multiple columns for combined uniqueness [OK]
Common Mistakes:
Adding UNIQUE on each column separately
Confusing PRIMARY KEY with UNIQUE
Not using composite constraint for pairs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.