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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:56:43+00:00 2026-06-05T20:56:43+00:00

I have a oracle table A which contains a column A.a which used to

  • 0

I have a oracle table A which contains a column A.a which used to store an attribute of the table. It is a big table with data. Now the semantic requirement of attribute A.a needs to develop into another table called B, B has an id column, and a column B.a which stores the same set of data as A.a (both columns are unique).

So now A.a should upgrade to a foreign key of the table B, then A.a should store B.id instead of VARCHAR2 and also the more tough part is I need to use A.a to find B.id via B.a and rewrite A.a with B.id because table A has existing data.

And I have to achieve this via SQL query.

So how to get this work? Apologize if I didn’t make myself clear. Please feel free to leave any idea. Thanks in advance.

  • 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-05T20:56:44+00:00Added an answer on June 5, 2026 at 8:56 pm

    If you have the following schema:

    tableA { a, x }
    

    You want to transform it to the following schema:

    tableB { id, a }
    tableA { id, x } where id has a FK constraint to tableB (id)
    

    You can do it with something like the following sequence of commands:

    -- sequence to generate the tableB's surrogate key
    CREATE SEQUENCE tableB_id_seq;
    
    -- generate tableB
    CREATE TABLE tableB AS
      SELECT tableB_id_seq.NEXTVAL AS id, a
      FROM (SELECT DISTINCT a
            FROM   tableA);
    
    ALTER TABLE tableB MODIFY (id PRIMARY KEY);
    
    -- add the FK column on tableA
    ALTER TABLE tableA ADD (id NUMBER);
    
    -- populate it
    UPDATE tableA SET id = 
      (SELECT tableB.id
       FROM tableB
       WHERE tableB.a = tableA.a);
    
    -- make it a FK
    ALTER TABLE tableA ADD CONSTRAINT afk
      FOREIGN KEY (id) REFERENCES tableB (id);
    
    -- drop the old column
    ALTER TABLE tableA DROP COLUMN a;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have a table in an Oracle Database which contains a column with the
I have a table in Oracle where one of the column contains UserIds which
We have a Oracle 9i database and OrderDetails table which has a column to
I have to get data from Oracle Table in which I have one datefield
In Oracle, I have a table with a column (X) which can have strings
I have a table in database which contains start date and end date. Now
I have a query which retrieves the data from Oracle DB. The table which
I have an Oracle table which contains event log messages for an application. We
Hi I have a table column in Oracle 10 which contain a string array
I have a oracle table which contains char(n) type for several columns. I use

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.