Consider a table Orders with a composite primary key on (OrderID, ProductID). The table has the following rows:
OrderID | ProductID | Quantity --------+-----------+--------- 1 | 101 | 2 1 | 102 | 1 2 | 101 | 5 2 | 103 | 3
What will be the output of this query?
SELECT OrderID, ProductID FROM Orders WHERE OrderID = 1;
SELECT OrderID, ProductID FROM Orders WHERE OrderID = 1;
Remember that the composite primary key means each combination of OrderID and ProductID is unique, but multiple rows can share the same OrderID.
The query filters rows where OrderID = 1. There are two such rows with ProductID 101 and 102 respectively, so both are returned.
Which statement best describes the uniqueness enforced by a composite primary key on columns (A, B)?
Think about how composite keys combine columns to enforce uniqueness.
A composite primary key requires that the pair (A, B) is unique for every row, but individual columns can have duplicates.
Which of the following SQL statements correctly defines a composite primary key on columns user_id and role_id in a table UserRoles?
Look for the correct syntax to declare multiple columns as a single primary key.
Option B correctly uses parentheses to list columns for the composite primary key. Options B, C, and D have syntax errors or misuse keywords.
You have a table with a composite primary key on (customer_id, order_id). You frequently query the table filtering only by order_id. What is the best indexing strategy to optimize these queries?
Think about how indexes work with leading columns in composite keys.
The composite primary key index is ordered by customer_id first, so queries filtering only by order_id cannot efficiently use it. Creating a separate index on order_id improves performance.
Given the following table creation statement, what error will occur?
CREATE TABLE Enrollment ( student_id INT, course_id INT, PRIMARY KEY student_id, course_id );
Check the syntax for declaring composite primary keys.
The PRIMARY KEY clause requires parentheses around the column list. Omitting them causes a syntax error.