0
0
MySQLquery~10 mins

UNIQUE constraints in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - UNIQUE constraints
Start Table Creation
Define Columns
Add UNIQUE Constraint
Insert Data
Check for Duplicate Values
Insert Row
Continue or End
When creating a table, you define columns and add UNIQUE constraints to ensure no duplicate values in those columns. When inserting data, the database checks for duplicates and rejects rows that violate the UNIQUE rule.
Execution Sample
MySQL
CREATE TABLE users (
  id INT PRIMARY KEY,
  email VARCHAR(100) UNIQUE
);

INSERT INTO users VALUES (1, 'a@example.com');
INSERT INTO users VALUES (2, 'a@example.com');
This code creates a users table with a UNIQUE constraint on the email column, then tries to insert two rows with the same email.
Execution Table
StepActionData InsertedCheck UNIQUE ConstraintResult
1Create table with UNIQUE on emailN/AN/ATable created
2Insert row (1, 'a@example.com')(1, 'a@example.com')No duplicate foundRow inserted
3Insert row (2, 'a@example.com')(2, 'a@example.com')Duplicate email foundInsert rejected with error
💡 Insert rejected at step 3 because email 'a@example.com' already exists violating UNIQUE constraint
Variable Tracker
VariableStartAfter Step 2After Step 3
users table rowsempty[(1, 'a@example.com')][(1, 'a@example.com')] (no change, insert rejected)
Key Moments - 2 Insights
Why does the second insert fail even though the id is different?
Because the UNIQUE constraint is on the email column, not the id. The second insert tries to add a duplicate email, which violates the UNIQUE rule as shown in execution_table step 3.
Can a UNIQUE constraint allow NULL values?
Yes, UNIQUE constraints allow multiple NULLs because NULL means unknown, so duplicates are not considered. This is not shown in the example but is a common behavior in MySQL.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
AThe row is inserted because no duplicate email exists
BThe insert is rejected due to duplicate email
CThe table is dropped
DThe UNIQUE constraint is removed
💡 Hint
Check the 'Result' column in execution_table row 2
At which step does the UNIQUE constraint cause an error?
AStep 2
BStep 3
CStep 1
DNo error occurs
💡 Hint
Look at the 'Check UNIQUE Constraint' and 'Result' columns in execution_table
If the UNIQUE constraint was removed, what would happen at step 3?
ATable would be dropped
BInsert would still fail
CInsert would succeed, duplicate emails allowed
DInsert would cause syntax error
💡 Hint
UNIQUE constraints prevent duplicates; removing it allows duplicates as per concept_flow
Concept Snapshot
UNIQUE constraints ensure column values are unique in a table.
Defined during table creation or altered later.
Inserts violating UNIQUE cause errors.
Allows multiple NULLs in MySQL.
Used to prevent duplicate data entries.
Full Transcript
This visual execution shows how UNIQUE constraints work in MySQL. First, a table is created with a UNIQUE constraint on the email column. When inserting the first row with email 'a@example.com', it succeeds because the table is empty. When inserting a second row with the same email, the database checks the UNIQUE constraint, finds a duplicate, and rejects the insert with an error. The variable tracker shows the table rows after each step, confirming no change after the failed insert. Key moments clarify why the second insert fails despite different ids and that UNIQUE allows multiple NULLs. The quiz tests understanding of when inserts succeed or fail due to UNIQUE constraints. The snapshot summarizes the main points for quick review.