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

  • SEARCH
  • Home
  • 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 261781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:27:38+00:00 2026-05-11T22:27:38+00:00

I need to make a column unique in one of our database tables, and

  • 0

I need to make a column unique in one of our database tables, and we want to completely remove any duplicates from the table. There is however a snag, in that there are a bunch of dependencies to other tables that will be affected.

For example, let’s say we’ve got the following relationship:

-------------------   -------------------
*     Customer    *   *      Order      *
-------------------   -------------------
* ID              *   * ID              *
* Name            *   * CustomerID      *
* Address         *   * Item            *
-------------------   -------------------

Fairly obvious relationship there – an Order needs a CustomerID, and that’s a foreign key. So we can’t delete the customer and keep the Order data.

In the example I’m totally ok with losing the redundant customer data, but for later reference I’d like to change the CustomerID from Order to a Customer ID for a “Deleted Customer”-row.

Is there any way of saying “delete this, and if there are foreign key constraints, change the CustomerID to that ID instead”? The DB is MS SQL 2005.

  • 1 1 Answer
  • 1 View
  • 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-11T22:27:38+00:00Added an answer on May 11, 2026 at 10:27 pm

    I’d say:

    step 1: create a list of the duplicate IDs to be deleted along with the corresponding ID to keep.
    The method really depends on how you’re detecting duplicates. Say you get a table:

    -------------
    *  Dupes    *
    -------------
    * del_id    *
    * keep_id   *
    -------------
    

    step 2: relink orders

    update order o
    set CustomerID=(select keep_id from Dupes d where d.del_id=o.CustomerID)
    where CustomerID in (select del_id from Dupes)
    

    step 3: delete the old customers

    delete from Customer
    where ID in (select del_id from Dupes)
    

    And voilà.

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

Sidebar

Related Questions

I need to make two columns from single column price depending upon if it
I've got a column in a table (eg. UserName) which I want to make
I need a SQL query to make all data in a column UPPER CASE?
I need to make a moderate system like the one in fmylife.com. Basically the
I need to gather Distinct Id's from a particular table using LINQ. The catch
I have counter for unique column in my table. Now I have list where
We have a massive table where I need to make an update on a
I need to encapsulate a set of tables JOINs that we freqently make use
I currently have two joined tables in a SQL Server database, one with news
I need to make some changes to a SQL Server 2008 database. This requires

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.