LearnFrenzy
Sign in Take a free test

DBMS & SQL · Topic 5 of 11

Keys and Constraints: questions with answers

Primary, candidate, super, foreign and composite keys, NOT NULL, UNIQUE, CHECK and DEFAULT, referential actions and key closures.

Questions
45
Timed sets
3
Queries executed
22
Your accuracy
–

What this topic covers

22 subtopics
  • Foreign key7
  • Finding candidate keys5
  • Referential actions5
  • Composite key3
  • Primary key3
  • Super key3
  • Key design2
  • NOT NULL2
  • UNIQUE and NULL2
  • Alternate key1
  • Attribute closure1
  • AUTO_INCREMENT1
Show all 22 subtopics
  • Candidate key1
  • CHECK1
  • CHECK and NULL1
  • DEFAULT1
  • Integrity in practice1
  • Primary key vs UNIQUE1
  • Prime attributes1
  • Self-referencing foreign key1
  • UNIQUE1
  • Upsert1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Keys identify rows; constraints stop bad data from ever entering a table. Interviews ask what each constraint allows (can a UNIQUE column hold two NULLs? does CHECK reject NULL?), what happens to child rows when a parent row is deleted, and – especially in GATE – how to find candidate keys from functional dependencies. Every constraint question here was run on MariaDB, so the errors and results are real.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Primary key Beginner Practice

Which statement about a primary key is true?

It can contain NULL as long as values are unique
Its values must be unique and cannot be NULL
A table can have several primary keys
It must be a single column
Show answer and explanation
Correct answer B

Explanation

A primary key uniquely identifies each row, so it is UNIQUE and NOT NULL. A table has at most one primary key, though it may span several columns (a composite key).

Why the other options are wrong

  • ANULL is never allowed in a primary key.
  • COnly one primary key per table; other keys are declared UNIQUE.
  • DComposite primary keys are common, e.g. (student_id, course_id).
Question 2 UNIQUE and NULL Beginner Practice Query executed

email is UNIQUE and Ravi's email is already NULL. What happens?

departments
idname
1Engineering
2Sales
employees
idemailnamedept_idsalary
1asha@x.comAsha190000
2NULLRavi240000
3meena@x.comMeena175000
INSERT INTO employees (id, email, name, dept_id, salary) VALUES (4, NULL, 'John', 2, 50000);
SELECT COUNT(*) AS null_emails FROM employees WHERE email IS NULL;
null_emails
0
null_emails
2
null_emails
1
null_emails
4
Show answer and explanation
Correct answer B

Explanation

A UNIQUE constraint compares values, and NULL is not equal to NULL, so any number of rows may have a NULL email (in MySQL, MariaDB, PostgreSQL and Oracle; SQL Server allows only one).

Why the other options are wrong

  • ARavi already had a NULL email.
  • CThe INSERT succeeds, so there are two NULLs.
  • DAsha and Meena have emails.
Question 3 Upsert Advanced Practice Query executed

MySQL and MariaDB support upserts. What is Ravi's salary afterwards?

departments
idname
1Engineering
2Sales
employees
idemailnamedept_idsalary
1asha@x.comAsha190000
2NULLRavi240000
3meena@x.comMeena175000
INSERT INTO employees (id, email, name, dept_id, salary)
VALUES (2, NULL, 'Ravi', 2, 40000)
ON DUPLICATE KEY UPDATE salary = salary + 5000;
SELECT salary FROM employees WHERE id = 2;
salary
85000
salary
40000
salary
5000
salary
45000
Show answer and explanation
Correct answer D

Explanation

The INSERT hits the existing primary key 2, so the UPDATE part runs instead: 40,000 + 5,000 = 45,000. PostgreSQL writes the same idea as ON CONFLICT … DO UPDATE.

Why the other options are wrong

  • AThe new values are not added to the old ones.
  • BThe row was updated.
  • Csalary + 5000 uses the existing salary.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

DBMS & SQL

All 11 topics

Topics, levels and full mocks
  1. 01 DBMS and SQL Fundamentals45 questions · 3 levels
  2. 02 Joins and Subqueries45 questions · 3 levels
  3. 03 Aggregates and GROUP BY45 questions · 3 levels
  4. 04 Window Functions45 questions · 3 levels
  5. 05 Keys and Constraints45 questions · 3 levels
  6. 06 ER Model and Relational Mapping45 questions · 3 levels
  7. 07 Normalization45 questions · 3 levels
  8. 08 Transactions and Concurrency45 questions · 3 levels
  9. 09 Indexing and Query Optimisation45 questions · 3 levels
  10. 10 Advanced SQL Queries45 questions · 3 levels
  11. 11 GATE-Style DBMS Questions (MSQ & NAT)45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links