What-If Walkthrough: A Table With No Primary Key and Duplicate Rows
This illustrative walkthrough is invented for teaching. Learner F built a sign-up table without a primary key, and duplicates crept in. We follow the clean-up, with what-if branches showing how different choices would play out.
The situation
The table looked like this:
CREATE TABLE signups (name TEXT, email TEXT, signed_up_on DATE);
A sign-up form sometimes submitted twice when people double-clicked. After a few weeks:
| name | signed_up_on | |
|---|---|---|
| Ola | ola@example.com | 2025-04-02 |
| Ola | ola@example.com | 2025-04-02 |
| Raj | raj@example.com | 2025-04-03 |
Learner F wants to delete one of Ola's rows. But DELETE FROM signups WHERE email = 'ola@example.com' would delete both. Without a primary key, there is no simple way to point at just one row.
Step 1: make a backup
Before any clean-up, Learner F copies the database file. What if they skip this? One wrong DELETE and the data is gone for good.
Step 2: see how big the problem is
SELECT email, COUNT(*) FROM signups GROUP BY email HAVING COUNT(*) > 1;
This lists every email that appears more than once, and how many times.
Step 3: choose a clean-up method
Option A: build a clean copy.
CREATE TABLE signups_clean AS
SELECT DISTINCT name, email, signed_up_on FROM signups;
DISTINCT keeps one copy of each identical row. Learner F checks the counts, then replaces the old table with the clean one.
Option B: use a hidden row id. SQLite has a built-in rowid for ordinary tables, which can target one row: DELETE FROM signups WHERE rowid = 2;. It works in SQLite, but other databases differ, so it is less portable.
What if the duplicates are not identical? If one row says Ola and the other Ola K., DISTINCT keeps both. Learner F would need to decide which row is correct by hand.
Learner F chooses Option A, because their duplicates are exact copies.
Step 4: add a primary key and a rule
The new table gets a proper design:
CREATE TABLE signups_new (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
signed_up_on DATE NOT NULL
);
INSERT INTO signups_new (name, email, signed_up_on)
SELECT name, email, signed_up_on FROM signups_clean;
Now each row has an id, and the UNIQUE rule on email refuses a second sign-up with the same address.
Step 5: fix the app
The form's code now catches the database's "unique constraint" error and shows a friendly message: "You're already signed up." Learner F also disables the button after the first click.
What-if summary
| Choice | Result |
|---|---|
| No backup | Risk of losing data permanently |
| DELETE by email | Removes every copy, not just extras |
| DISTINCT copy | Clean, simple, works when duplicates are identical |
| rowid | Quick in SQLite, not portable |
| Add PRIMARY KEY and UNIQUE | Stops the problem returning |
What the scenario teaches
A primary key is not just tidiness; it is what lets you work with one row at a time. A UNIQUE rule turns "please don't double-submit" into something the database enforces. See a checklist before choosing a primary key for your next table.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.