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.
Which statement about a primary key is true?
Show answer and explanation
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).
email is UNIQUE and Ravi's email is already NULL. What happens?
| id | name |
|---|---|
| 1 | Engineering |
| 2 | Sales |
| id | name | dept_id | salary | |
|---|---|---|---|---|
| 1 | asha@x.com | Asha | 1 | 90000 |
| 2 | NULL | Ravi | 2 | 40000 |
| 3 | meena@x.com | Meena | 1 | 75000 |
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
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.
MySQL and MariaDB support upserts. What is Ravi's salary afterwards?
| id | name |
|---|---|
| 1 | Engineering |
| 2 | Sales |
| id | name | dept_id | salary | |
|---|---|---|---|---|
| 1 | asha@x.com | Asha | 1 | 90000 |
| 2 | NULL | Ravi | 2 | 40000 |
| 3 | meena@x.com | Meena | 1 | 75000 |
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
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.