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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:35:56+00:00 2026-05-20T06:35:56+00:00

We are redesigning table schema in postgres. It used to have a linker table

  • 0

We are redesigning table schema in postgres. It used to have a linker table to the two tables partner and advertiser of the name partner_advertiser since we were assuming a many-many relationship between partner and advertiser. A change is made such that an advertiser shall have only one partner, so partner will have a one-may relationship to advertiser.

How do I make the change without losing previous information? The linker table data must be used to fill in the mapping of the new schema design.
Here is my initial code:

BEGIN

FOR r IN SELECT partnerid, advertiserid from partner_advertiser
    LOOP
    NEXT r;
    UPDATE advertiser SET partnerid = r.partnerid WHERE id = r.advertiserid 
    END LOOP;

END

BTW I haven’t done any pg/plsql myself. So if there are any basic steps I should make please give me a heads-up.

  • 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-20T06:35:57+00:00Added an answer on May 20, 2026 at 6:35 am

    You can use:

    UPDATE advertiser a SET partnerid = r.partnerid
    FROM partner_advertiser r
    WHERE a.id = r.advertiserid
    

    Generally, simple relational transformations like these will never need a loop.
    If you really need one, though, look at http://www.postgresql.org/docs/9.0/interactive/plpgsql-control-structures.html

    One extra note: any transformation will obviously lose data if advertiserid is not already unique, thus you should first run something like

    SELECT count(*), advertiserid FROM partner_advertiser
    GROUP BY advertiserid HAVING COUNT(*) > 1
    

    If any rows get returned by that you will want to fix that manually.

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

Sidebar

Related Questions

I have two tables which have the exact same structure. Both tables can store
I need to display all tables that have zero records. I tried, select *
I have quite a few controls scattered throughout many table cells in my table,
I'm currently redesigning a website and have run into an issue with some elements
I have a problem with a site I am redesigning and the problem is
I have a classifieds website, and I am thinking about redesigning the database a
I have a dynamically created table, that has N rows and M QTableWidgetItems (that
I am redesigning a new website and my database table need tidying up a
I need to get the table name to query from a table. var tableName
The following is the structure of database table consisiting of foreign keys from two

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.