Constraints & Keys Questions with Answers

8 practice questions on Constraints & Keys (SQL) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

orders.customer_id references customers.id with ON DELETE CASCADE. What happens when a customer row is deleted?

  1. The delete is rejected while orders exist
  2. The customer's orders get customer_id set to NULL
  3. Nothing happens to the orders
  4. 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

  1. ON DELETE CASCADE propagates deletes.
  2. Deleting a customer deletes its orders.
  3. 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?

  1. Two: primary and secondary
  2. One (it may be composite)
  3. One per column
  4. 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

  1. A table has at most one primary key.
  2. That key can include several columns, which is a composite key.
  3. 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?

  1. Values are sorted
  2. Values are unique and not NULL
  3. Values reference another table
  4. 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

  1. A primary key identifies each row uniquely.
  2. So its values must be unique and cannot be NULL.
  3. 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?

  1. To make lookups on the column faster
  2. To encrypt the column's values at rest
  3. To enforce referential integrity
  4. 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

  1. A foreign key requires each child value to match an existing parent key (or be NULL).
  2. This keeps data consistent between tables.
  3. 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)?

  1. The row is stored and a warning is logged
  2. The value is stored as 0
  3. The value is stored as 500
  4. 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

  1. CHECK (salary > 0) tests each row.
  2. −500 gives FALSE for salary > 0.
  3. 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?

  1. Any column with the same data type as the child
  2. A PRIMARY KEY or UNIQUE constraint
  3. Any column declared NOT NULL in the parent
  4. 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

  1. A foreign key must point to a column that is guaranteed unique.
  2. A PRIMARY KEY or UNIQUE constraint provides that guarantee.
  3. 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?

  1. The second insert fails with a uniqueness violation
  2. Both inserts fail
  3. The second NULL is converted to an empty string
  4. 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

  1. UNIQUE compares non-NULL values only.
  2. NULL is not equal to NULL, so two NULLs do not violate UNIQUE in PostgreSQL or SQLite by default.
  3. 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?

  1. A foreign key must be unique in the child table
  2. A table can have only one candidate key
  3. Every super key is also a candidate key
  4. 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

  1. A super key is any set of columns that uniquely identifies rows.
  2. A candidate key is a super key with no extra columns that can be removed.
  3. 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 →

Related topics

See all SQL topics · All topics