Consider a table Users with columns id (primary key) and name. What will this query return?
SELECT COUNT(*) FROM Users WHERE id IS NULL;
Primary keys cannot be NULL by definition.
Primary key columns must have unique, non-null values. Therefore, no row can have id as NULL, so the count is zero.
Choose the correct statement about UNIQUE constraints in SQL.
Think about how NULL values are treated in UNIQUE columns.
UNIQUE constraints allow multiple NULLs because NULL is not considered equal to another NULL in SQL.
Given a table Orders with columns order_id and product_id, which SQL statement correctly defines a composite primary key on both columns?
Composite primary keys list columns inside parentheses after PRIMARY KEY.
Option A correctly defines a composite primary key on both columns. Option A tries to assign two primary keys which is invalid. Option A defines a unique constraint, not a primary key. Option A has incorrect syntax.
Consider this SQL statement:
CREATE TABLE Employees (id INT, email VARCHAR(100) UNIQUE, PRIMARY KEY (id, email));
Why does this statement cause an error?
Think about the uniqueness guarantees of primary keys.
Declaring email UNIQUE is redundant because the PRIMARY KEY constraint already enforces uniqueness on all columns in the key.
You have a large table Products with a column sku that must be unique. Which option is the most efficient way to enforce uniqueness and optimize lookups?
Primary keys automatically create unique indexes and optimize lookups.
Defining sku as the PRIMARY KEY creates a unique index and optimizes queries. UNIQUE constraints also create indexes, but PRIMARY KEY is preferred if sku is the main identifier.