4.4: Uniqueness, Keys, and Duplicate Data
- Page ID
- 48049
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Uniqueness, Keys, and Duplicate Data
Duplicate or inconsistent records can wreak havoc on data analysis. Ensuring uniqueness in the right places is a major aspect of data cleaning. Uniqueness means that each real-world entity is represented once in the dataset, or that a particular field (or combination of fields) has no repeats when it is supposed to be unique. This concept is often enforced by primary keys in databases: a primary key is a designated column (or set of columns) that uniquely identifies each record in a table. By definition, primary keys also cannot be NULL – every record must have a value for the key. An example of a primary key is a Customer ID in a customer table; no two customers should share the same ID, and every customer must have an ID.
The role of primary keys: Primary keys are the first line of defense against duplicate records. In an ideal scenario, any attempt to insert a duplicate primary key into a well-designed database table would be rejected, preventing the duplication at the source. If your dataset comes from a source without a proper primary key (or if the primary key wasn’t enforced), part of cleaning will involve detecting duplicates and establishing a way to uniquely identify records.
Duplicate detection: Finding duplicate records can be straightforward or complex, depending on the data. On the simple end, if you suspect exact duplicates (every field the same), you can use SQL aggregation: for example, SELECT fields..., COUNT(*) FROM Table GROUP BY fields... HAVING COUNT(*) > 1; will list groups of identical records occurring more than once. More often, duplicates are non-identical but refer to the same entity. For instance, one customer might appear twice with slightly different spellings of their name ("John Smith" vs "Jon Smith") or one record might have a missing field that the other has filled, making them not exact matches but indeed duplicates. Detecting these requires domain knowledge and possibly fuzzy matching techniques. A simple approach is to compare on a subset of fields: e.g., group by email or phone number to see if the same contact info is associated with multiple customer IDs. Data cleaning tools or libraries often have “deduplication” functions that use heuristics or fuzzy logic to cluster similar records.
When duplicates are found, the next step is resolution: deciding which record to keep or how to merge them. Sometimes one record is clearly more complete, so you keep that and drop the other. Other times, you might consolidate information from both (merging records). If automated, this requires careful rules; if manual, it requires judgment. Either way, documenting how duplicates were resolved is important for traceability.
Unique constraints beyond primary keys: Even if a primary key exists, there may be other fields that should be unique but aren’t designated as such in the database. For example, in a well-behaved customer table, email addresses might ideally be unique (one email per customer) – if you find the same email attached to two different Customer IDs, that could indicate a duplicate customer or a shared email scenario. During cleaning, you might check for these by doing SELECT email, COUNT(*) FROM Customers GROUP BY email HAVING COUNT(*) > 1;. If business rules say emails should be unique, any violations need attention (maybe those two customer entries should actually be one). There are also cases of composite uniqueness, e.g., in a transactions table, perhaps the combination of OrderID and ProductID should be unique (to avoid duplicate line items). Data integrity checks like these help expose issues.
Surrogate keys and duplicates: Modern databases often use surrogate keys (auto-increment IDs or UUIDs) as primary keys. While these guarantee uniqueness of the key, they don’t automatically prevent logical duplicates – you could have two different customer records with two different IDs that actually represent the same person. Thus, relying solely on a surrogate primary key might give a false sense of no duplicates while the real-world duplication exists. Data cleaning must therefore also involve checking other attributes for unintended duplicates as described. As Maydanchik points out, multiple records with distinct primary keys may still represent the same real-world entity if the data collection allowed it. De-duplication software or routines are needed in such cases to merge those entries.

Example scenario – duplicate handling: Consider a product catalog database where the ProductID is the primary key. Suppose due to a system glitch, the same product got entered twice with two different IDs. The name, SKU, and price are identical, but since the IDs differ, the database didn’t flag it. In analysis, this duplicate could cause the product to be double-counted in inventory valuation or sales aggregation. A cleaning step would be to identify that these two products have the same SKU and name, and thus likely are duplicates. The resolution might be to retire one of the entries or merge them in the system. Until that’s done, an interim solution might be to at least filter one out of analytical queries to avoid double counting.
Another example: In an e-commerce orders dataset, you might find two rows with the same OrderID and ProductID – meaning the exact same line item was recorded twice. This could be due to a data import error. Enforcing a composite unique constraint (OrderID+ProductID) would prevent that at the database level. If not enforced, the onus is on the data cleaning step to detect and remove such duplicates.
Primary keys at the source: A best practice is to require primary keys at the source and not allow bad data in. If you have control over the data collection or database design, define primary keys and unique constraints upfront. It is far easier to stop duplicate records from being created than to untangle them later. In the context of data warehousing or aggregation, even if source systems have primary keys, when combining multiple sources you might inadvertently create duplicates (e.g., two systems have overlapping entries). Thus, part of data preparation is establishing master keys or performing entity resolution across sources to ensure uniqueness in the combined dataset.
To summarize, maintaining uniqueness and eliminating duplicates is critical for data integrity. Duplicate records can inflate counts, distort statistical analyses, or cause violations of referential integrity. Through primary keys, unique constraints, and careful duplicate detection logic, data cleaning aims to ensure each entity or event is represented once and only once in the analytical dataset. This gives analysts confidence that, for example, when they count customers, they’re not inadvertently counting the same individual twice.


