Introduction
Unique indexes help keep data clean by making sure no two rows have the same value in certain columns.
CREATE UNIQUE INDEX index_name ON table_name (column1, column2, ...);
CREATE UNIQUE INDEX idx_email ON users (email);
CREATE UNIQUE INDEX idx_product_code ON products (product_code);
CREATE UNIQUE INDEX idx_user_fullname ON users (first_name, last_name);
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50), email VARCHAR(100) ); CREATE UNIQUE INDEX unique_email ON users (email); INSERT INTO users (username, email) VALUES ('alice', 'alice@example.com'); INSERT INTO users (username, email) VALUES ('bob', 'bob@example.com'); -- This next insert will fail because the email is already used INSERT INTO users (username, email) VALUES ('charlie', 'alice@example.com');
UNIQUE index in MySQL?email in a table named users?employees with a UNIQUE index on employee_id, what happens if you run this query?INSERT INTO employees (employee_id, name) VALUES (101, 'Alice');
INSERT INTO employees (employee_id, name) VALUES (101, 'Bob');
username but got an error. Which of these is the most likely cause?first_name and last_name in the contacts table is unique, but duplicates are still appearing. Which is the best way to fix this?Pass the test and this pattern moves unknown