Consider a table users with columns id (primary key), email, and name. A UNIQUE index is created on the email column.
What will happen if you try to insert two rows with the same email value?
CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255), name VARCHAR(255)); CREATE UNIQUE INDEX idx_email ON users(email); INSERT INTO users VALUES (1, 'alice@example.com', 'Alice'); INSERT INTO users VALUES (2, 'alice@example.com', 'Alicia');
Think about what a UNIQUE index enforces on the column values.
A UNIQUE index prevents duplicate values in the indexed column. Trying to insert a duplicate value causes an error.
Given a table orders with columns order_id, customer_id, and order_date, you run this query:
SELECT * FROM orders WHERE customer_id = 12345;
Which index will most improve the speed of this query?
Which column is used in the WHERE clause?
The query filters by customer_id, so an index on that column speeds up lookups.
Which of the following is the correct syntax to create a composite index on columns last_name and first_name in MySQL?
Think about how multiple columns are listed in an index.
Columns in a composite index are listed separated by commas inside parentheses.
Given a table products with columns category, price, and stock, you run this query:
SELECT * FROM products WHERE category = 'Books' AND price < 20;
Which index will most improve this query's performance?
Consider the order of columns in a composite index and how the query filters.
The query filters first by category, then by price. An index starting with category then price is best.
Consider this SQL statement:
CREATE INDEX idx_description ON products(description(10));
It fails with an error. What is the most likely reason?
Think about indexing long text columns and how prefix lengths work.
MySQL requires prefix length for indexing TEXT/BLOB columns, but the syntax must be correct. The error usually means the prefix length is missing or misused.