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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:35:30+00:00 2026-05-28T00:35:30+00:00

I have table A(id). I need to create table B(id) add a foreign key

  • 0

I have table A(id).

I need to

  • create table B(id)
  • add a foreign key to table A that references to B.id
  • for every row in A, insert a row in B and update A.b_id with the newly inserted row in B

Is it possible to do it without adding a temporary column in B that refers to A? The below does work, but I’d rather not have to make a temporary column.

alter table B add column ref_id integer references(A.id);
insert into B (ref_id) select id from A;
update A set b_id = B.id from B where B.ref_id = A.id;
alter table B drop column ref_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-28T00:35:31+00:00Added an answer on May 28, 2026 at 12:35 am

    Assuming that:

    1) you’re using postgresql 9.1

    2) B.id is a serial (so actually an int with a default value of nextval(‘b_id_seq’)

    3) when inserting to B, you actually add other fields from A otherwise the insert is useless

    …I think something like this would work:

    with n as (select nextval('b_id_seq') as newbid,a.id as a_id  from a),
       l as (insert into b(id) select newbid from n returning id as b_id)
     update a set b_id=l.b_id from l,n where a.id=n.a_id and l.b_id=n.newbid;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table A in Oracle that has a primary key (id). I need
I have a table of data that I need to dynamically add a column
I have table 'products' and I need to update 'price' field by 20% if
I have table to test score data that I need to pivot and I
I have a table with string values that need to matched individually to a
I have a table of about a million rows and I need to update
i have a table with two foreign keys as composite key. ActivityTbl - (activityNbr(PK),
I have table: id name type where type is 1 or 2 I need
I have table with an auto-increment field, but I need to transfer the table
I have a table in which I need both the values to be primary

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.