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.
UNIQUE index in a database table?email in a table named users?products with a unique index on sku, what happens when you run this SQL?INSERT INTO products (sku, name) VALUES ('A123', 'Item1');
INSERT INTO products (sku, name) VALUES ('A123', 'Item2');sku means no two rows can have the same sku value.username column. The following query causes an error:INSERT INTO users (username) VALUES ('john');
INSERT INTO users (username) VALUES ('john');employees with a unique index on (email, department_id). Which of these inserts will cause a unique index violation if the table already has a row with email='alice@example.com' and department_id=10?Pass the test and this pattern moves unknown