Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:29:01+00:00 2026-05-27T21:29:01+00:00

I have to clean up records from a table that doesn’t have a primary

  • 0

I have to clean up records from a table that doesn’t have a primary key or a a unique constraint.

Table definition:

create table person(
    name text,
    staff_id integer,
    work_code text,
    location
);

Unsurprisingly, it contains a lot of duplicates and partial duplicates.
What is the best way to transform the records to a unique set. I don’t have to care about other columns besides name and staff_id

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:29:02+00:00Added an answer on May 27, 2026 at 9:29 pm

    As you

    don’t have to care about other columns besides name and staff_id

    This could be your procedure to clean up the table:

    1.) Create a temporary table of unique rows:

    CREATE TEMP TABLE p_tmp AS
    SELECT DISTINCT ON (name, staff_id)
           name, staff_id, work_code, location
    FROM   person
    ORDER  BY name, staff_id, work_code, location;
    

    I arbitrarily pick the “first row per (name, staff_id) – minimum work_code and matching location.

    2.) Empty table:

    TRUNCATE person;
    

    3.) Re-INSERT unique tuples:

    INSERT INTO person SELECT * FROM p_tmp;
    

    Make sure, dupes don’t creep back in. Add a surrogate primary key:

    ALTER TABLE person ADD COLUMN person_id serial PRIMARY KEY;
    ALTER TABLE person ADD UNIQUE (name, staff_id);
    

    Or just add a multi-column primary key:

    ALTER TABLE person ADD PRIMARY KEY (name, staff_id);
    

    The temporary table will be dropped at the end of the session automatically.

    Of course, all of this is best done inside one transaction, so you don’t lose anything in the unlikely case that you run into a problem half way. Some clients do that automatically for a batch of SQL statements executed at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that basically just returns all records from a table, and
I currently have a table that is 17 columns wide and has 30 records.
I have written a function that gets a given number of random records from
I have some unicode text that I want to clean up using regular expressions.
I have an HTTP Module that I use to clean up the JSON returned
I have an Eclipse RCP application that will launch properly after a clean install
I have a very large database with about 120 Million records in one table.I
Helo, I have a two Grid where I have to display some records from
I have a C# List, that is filled from a database.. So far its
I have an sqlite database with over 400k records. I have just found that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.