Concept Flow - Unique index behavior
Create Unique Index
Insert Row
Check if Value Exists in Index
Reject Insert
Next Insert or End
This flow shows how a unique index checks for duplicates before allowing data insertion.
CREATE UNIQUE INDEX idx_email ON users(email); INSERT INTO users (id, email) VALUES (1, 'a@example.com'); INSERT INTO users (id, email) VALUES (2, 'b@example.com'); INSERT INTO users (id, email) VALUES (3, 'a@example.com');
| Step | Action | Email Value | Index Check Result | Insert Result |
|---|---|---|---|---|
| 1 | Create unique index on email | - | - | Index created |
| 2 | Insert row with email 'a@example.com' | a@example.com | Not found | Insert successful |
| 3 | Insert row with email 'b@example.com' | b@example.com | Not found | Insert successful |
| 4 | Insert row with email 'a@example.com' | a@example.com | Found duplicate | Insert rejected |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|
| users.email | empty | ['a@example.com'] | ['a@example.com', 'b@example.com'] | ['a@example.com', 'b@example.com'] |
Unique index ensures column values are unique. Before inserting, it checks if value exists. If duplicate found, insert is rejected. Helps maintain data integrity. Useful for columns like email or username.