Consider a table Users with a UNIQUE constraint on the email column. What happens if you try to insert two rows with the same email?
CREATE TABLE Users ( id INT PRIMARY KEY, email VARCHAR(255) UNIQUE ); INSERT INTO Users (id, email) VALUES (1, 'user@example.com'); INSERT INTO Users (id, email) VALUES (2, 'user@example.com');
Think about what UNIQUE means for a column in a database table.
The UNIQUE constraint prevents duplicate values in the specified column. Trying to insert a duplicate value causes an error.
Choose the correct statement about UNIQUE constraints in SQL.
Think about how NULL values are treated in UNIQUE columns.
Most SQL databases allow multiple NULLs in a UNIQUE column because NULL means unknown, so duplicates are not considered.
You have a table Employees with a column phone_number. Which SQL statement correctly adds a UNIQUE constraint to this column?
Adding a named UNIQUE constraint requires a constraint name and column list.
The correct syntax uses ADD CONSTRAINT with a name and specifies the column(s) in parentheses.
Which of the following is a true effect of adding a UNIQUE constraint on a column in terms of query performance?
Think about how databases enforce uniqueness internally.
Most databases create a unique index to enforce UNIQUE constraints, which also speeds up lookups on that column.
In this database, a UNIQUE constraint on column code causes an error when inserting multiple rows with NULL in code. Why?
CREATE TABLE Products ( id INT PRIMARY KEY, code VARCHAR(10) UNIQUE ); INSERT INTO Products (id, code) VALUES (1, NULL); INSERT INTO Products (id, code) VALUES (2, NULL);
Some databases handle NULLs differently in UNIQUE constraints.
Some database systems treat NULL as a value that must be unique, so multiple NULLs violate UNIQUE constraints.