Sample questions
Question 1Easy
orders.customer_id references customers.id with ON DELETE CASCADE. What happens when a customer row is deleted?
- The delete is rejected while orders exist
- The customer's orders get customer_id set to NULL
- Nothing happens to the orders
- The customer's orders are deleted as well
Answer: D. The customer's orders are deleted as well
Explanation
CASCADE propagates the delete to referencing rows. Rejecting the delete is the default behaviour (NO ACTION / RESTRICT), and ON DELETE SET NULL sets the column to NULL.
Step-by-step solution
- ON DELETE CASCADE propagates deletes.
- Deleting a customer deletes its orders.
- Rejecting would be RESTRICT; NULL would be SET NULL.
Why the other options are wrong
- A. That is the RESTRICT/NO ACTION behaviour.
- B. That is SET NULL.
- C. Cascade acts on child rows.
Question 2Easy
How many PRIMARY KEY constraints can a single table have?
- Two: primary and secondary
- One (it may be composite)
- One per column
- Any number
Answer: B. One (it may be composite)
Explanation
A table has at most one primary key, but it can be a composite key made of several columns.
Step-by-step solution
- A table has at most one primary key.
- That key can include several columns, which is a composite key.
- So the answer is one, which may be composite (option B).
Why the other options are wrong
- A. A table does not have a separate secondary primary key.
- C. A primary key is one key for the table, not one per column.
- D. Only one primary key is allowed per table.
Tip: A table can have many UNIQUE keys but only one PRIMARY KEY.
Question 3Easy
Which property does a PRIMARY KEY guarantee?
- Values are sorted
- Values are unique and not NULL
- Values reference another table
- Values are unique but may be NULL
Answer: B. Values are unique and not NULL
Explanation
A primary key uniquely identifies each row, so its values must be unique and cannot be NULL.
Step-by-step solution
- A primary key identifies each row uniquely.
- So its values must be unique and cannot be NULL.
- That is option B.
Why the other options are wrong
- A. A primary key does not sort the values; sorting is a separate ORDER BY.
- C. Referencing another table describes a foreign key.
- D. A primary key cannot contain NULL values.
Tip: PRIMARY KEY = UNIQUE plus NOT NULL.
Question 4Easy
What is the main purpose of a FOREIGN KEY constraint?
- To make lookups on the column faster
- To encrypt the column's values at rest
- To enforce referential integrity
- To guarantee the column is unique
Answer: C. To enforce referential integrity
Explanation
A foreign key ensures that each value in the child column matches an existing key in the parent table (or is NULL).
Step-by-step solution
- A foreign key requires each child value to match an existing parent key (or be NULL).
- This keeps data consistent between tables.
- So option C is the main purpose.
Why the other options are wrong
- A. It can help joins but its main purpose is consistency, not speed.
- B. A foreign key does not encrypt values.
- D. Uniqueness comes from a UNIQUE or PRIMARY KEY constraint, not a foreign key.
Tip: A foreign key stops orphan rows: every child value must exist in the parent.
Question 5Medium
Given salary INT CHECK (salary > 0), what happens on INSERT ... VALUES (..., -500)?
- The row is stored and a warning is logged
- The value is stored as 0
- The value is stored as 500
- The insert is rejected with a constraint violation
Answer: D. The insert is rejected with a constraint violation
Explanation
A CHECK constraint rejects any row for which the condition evaluates to FALSE.
Step-by-step solution
- CHECK (salary > 0) tests each row.
- −500 gives FALSE for salary > 0.
- The insert is rejected with a constraint violation (option D).
Why the other options are wrong
- A. A CHECK failure rejects the row; it does not store it with a warning.
- B. The value is not changed to 0.
- C. The value is not changed to 500; the insert is refused.
Tip: A CHECK constraint is enforced on every insert and update; a FALSE result rejects the row.
Question 6Medium
In standard SQL, what must the referenced column(s) of a FOREIGN KEY be in the parent table?
- Any column with the same data type as the child
- A PRIMARY KEY or UNIQUE constraint
- Any column declared NOT NULL in the parent
- Any column that has an index of any kind
Answer: B. A PRIMARY KEY or UNIQUE constraint
Explanation
The parent side must be guaranteed unique so each child value points to exactly one parent row, which means a primary key or unique constraint.
Step-by-step solution
- A foreign key must point to a column that is guaranteed unique.
- A PRIMARY KEY or UNIQUE constraint provides that guarantee.
- So option B is required.
Why the other options are wrong
- A. The same data type does not guarantee that the parent values are unique.
- C. NOT NULL does not stop duplicate values in the parent.
- D. A plain index, even a non-unique one, does not guarantee uniqueness.
Tip: A foreign key's parent side needs PRIMARY KEY or UNIQUE.
Question 7Medium
A nullable column email has a UNIQUE constraint. In PostgreSQL and SQLite (default settings), what happens when you insert two rows with email = NULL?
- The second insert fails with a uniqueness violation
- Both inserts fail
- The second NULL is converted to an empty string
- Both inserts succeed, because NULLs are not considered equal to each other
Answer: D. Both inserts succeed, because NULLs are not considered equal to each other
Explanation
UNIQUE compares non-NULL values. Since NULL is not equal to NULL, multiple NULLs are allowed by default. Some databases, such as SQL Server, behave differently.
Step-by-step solution
- UNIQUE compares non-NULL values only.
- NULL is not equal to NULL, so two NULLs do not violate UNIQUE in PostgreSQL or SQLite by default.
- Both inserts succeed (option D).
Why the other options are wrong
- A. The second NULL does not violate UNIQUE in these databases.
- B. Neither insert fails in these databases.
- C. NULL is not converted to an empty string.
Tip: Multiple NULLs are allowed in a UNIQUE column in most databases; SQL Server behaves differently by default.
Question 8Hard
Which statement about keys is correct?
- A foreign key must be unique in the child table
- A table can have only one candidate key
- Every super key is also a candidate key
- A candidate key is a minimal super key
Answer: D. A candidate key is a minimal super key
Explanation
A super key is any set of columns that uniquely identifies rows. Candidate keys are the minimal ones, and one of them is chosen as the primary key.
Step-by-step solution
- A super key is any set of columns that uniquely identifies rows.
- A candidate key is a super key with no extra columns that can be removed.
- So a candidate key is a minimal super key (option D).
Why the other options are wrong
- A. A foreign key does not need to be unique; the referenced parent column must be.
- B. A table can have several candidate keys; one is chosen as primary key.
- C. Many super keys have extra columns and are not candidate keys.
Tip: Candidate keys are minimal super keys; the primary key is one of them.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →