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 8927439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:13:23+00:00 2026-06-15T08:13:23+00:00

I have the following two tables and data:- CREATE TABLE customers ([id] int, [name]

  • 0

I have the following two tables and data:-

CREATE TABLE customers
    ([id] int, [name] varchar(10), [sex] varchar(1))
;

INSERT INTO customers
    ([id], [name], [sex])
VALUES
    (1050, 'John Doe', 'M'),
    (1060, 'Jane Doe', 'F'),
    (1031, 'Joe Bloggs', 'M')
;

CREATE TABLE orders
    ([id] int, [fk] int, [product] varchar(13))
;

INSERT INTO orders
    ([id], [fk], [product])
VALUES
    (51, 1050, 'Blue car'),
    (57, 1050, 'Yellow car'),
    (43, 1060, 'Pink bus'),
    (32, 1031, 'Black pen'),
    (87, 1031, 'Orange jacket')
;

What i want to do is re-number the id column in both tables sequentially starting from 1.

The linked rows in the orders table must also be renumbered, and the foreign key in this table must match the new number in the customers table.

so the data needs to end up looking like this:-

ID      NAME        SEX
0001    John Doe    M
0002    Jane Doe    F
0003    Joe Bloggs  M

ID    FK    PRODUCT
0001  0001  Blue car
0002  0001  Yellow car
0003  0002  Pink bus
0004  0003  Black pen
0005  0003  Orange jacket

How would I go about doing this in SQL Server ?

  • 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-06-15T08:13:24+00:00Added an answer on June 15, 2026 at 8:13 am

    Absolutely no need to resort to cursor (yikes!) here…. you need something like this:

    -- Table "Customers" - rename column "id" to "old_id"
    EXEC sp_rename 'dbo.Customers.id', 'old_id'
    
    -- add new "id" column
    ALTER TABLE Customers ADD id INT 
    
    -- fill new "id" column with sequential values, ordered by the "old_id" value
    ;WITH CTE AS
    (
        SELECT old_id, new_id = ROW_NUMBER() OVER (ORDER BY old_id) 
        FROM Customers
    )
    UPDATE dbo.Customers
    SET id = CTE.new_id
    FROM CTE
    WHERE CTE.old_id = dbo.Customers.old_id
    
    -- Table "Orders" - rename column "id" to "old_id"
    EXEC sp_rename 'dbo.orders.id', 'old_id'
    
    -- add new "id" column
    ALTER TABLE Orders ADD id INT 
    
    -- update the FK references to the new "id" values in table "dbo.Customers"
    UPDATE dbo.Orders 
    SET fk = c.id
    FROM dbo.Customers c
    WHERE dbo.ORders.fk = c.old_id
    
    -- fill new "id" column with sequential values, ordered by the "old_id" value
    ;WITH CTE AS
    (
        SELECT old_id, new_id = ROW_NUMBER() OVER (ORDER BY old_id) 
        FROM dbo.Orders
    )
    UPDATE dbo.Orders
    SET id = CTE.new_id
    FROM CTE
    WHERE CTE.old_id = dbo.Orders.old_id
    
    -- drop the old, no longer required columns "old_id" from both tables
    ALTER TABLE dbo.Customers DROP COLUMN old_id
    ALTER TABLE dbo.Orders DROP COLUMN old_id
    

    That’ll work, if you don’t have any other FK relationships that are referencing one of those two tables. If you do, then you might need to disable or drop those FK relationships before you start this upgrade script.

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

Sidebar

Related Questions

I have the following two tables (SQL scripts with data): CREATE TABLE [dbo].[Item_Master]( [Item_Name]
I have the following two tables defined... CREATE TABLE [LogLevel] ( [Id] int primary
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have the following two tables in my mysql database. Table Name: groups id
I have the following two tables: Customer { int Id int Name } Bills
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
For example I have the following tables resulting from: CREATE TABLE A (Id int,
I have two database tables with some demo data like shown below Create table
1I have the following two tables (sample data) and need to be able to
I have the following two tables Groups Id (int) People Id (int) GroupId (int,

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.