How do primary keys and uniqueness constraints help you detect and prevent duplicate records?

Quick Overview

Explain how primary keys and unique constraints stop duplicate rows at write time and how queries find duplicates that already exist. Tests the difference between surrogate and natural keys, composite uniqueness, NULL handling, and warehouses that do not enforce constraints.

How do primary keys and uniqueness constraints help you detect and prevent duplicate records?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do primary keys and uniqueness constraints help you detect and prevent duplicate records? ```hint Separate the two kinds of identity Compare a generated row ID with the attributes that make two rows the same real-world entity, and ask which one each constraint protects. ``` ### What a Strong Answer Covers - What a primary key guarantees, and why a surrogate key alone does not stop logical duplicates - How unique constraints on natural or composite keys block duplicates at write time - How to find existing duplicates with queries before adding a constraint - The limits: exact matching only, NULL handling, and engines that accept constraints without enforcing them ### Follow-up Questions - How would you remove existing duplicates so that a new unique constraint can be added, keeping the earliest row? - How would you make email uniqueness case-insensitive? - Your warehouse accepts `PRIMARY KEY` declarations but does not enforce them. How do you keep a fact table free of duplicates? - Why is a check-then-insert in application code not enough to prevent duplicates?

Overview: Explain how primary keys and unique constraints stop duplicate rows at write time and how queries find duplicates that already exist. Tests the difference between surrogate and natural keys, composite uniqueness, NULL handling, and warehouses that do not enforce constraints.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do primary keys and uniqueness constraints help you detect and prevent duplicate records?

What a Strong Answer Covers Guidance

  • What a primary key guarantees, and why a surrogate key alone does not stop logical duplicates
  • How unique constraints on natural or composite keys block duplicates at write time
  • How to find existing duplicates with queries before adding a constraint
  • The limits: exact matching only, NULL handling, and engines that accept constraints without enforcing them

Follow-up Questions Guidance

  • How would you remove existing duplicates so that a new unique constraint can be added, keeping the earliest row?
  • How would you make email uniqueness case-insensitive?
  • Your warehouse accepts PRIMARY KEY declarations but does not enforce them. How do you keep a fact table free of duplicates?
  • Why is a check-then-insert in application code not enough to prevent duplicates?