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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:27:59+00:00 2026-06-04T11:27:59+00:00

In a SQL DATABASE I have a Table Users Id Name Age AddressId —-+——+——+———–

  • 0

In a SQL DATABASE

I have a Table Users

 Id   Name   Age    AddressId
----+------+------+-----------

Where AddressId is a foreign key to a table names Addresses

The Addresses Table:

 Id   Country   State  City   ZipCode
----+---------+------+------+---------

This is a ONE-TO-ONE Relationship: Each User Has 1 address and each address has one user

I have a new table named NEWUsers

Id   Name  
----+------

It has only Id and Name.

What i want to do is this:

Write a script to insert all the records From the NEWUSers Table into the Users Table.

  • I want The Age to be default 20 for all new users
  • And for each new user inserted I need to create a new Address record for him
  • the new Address record will have all it’s values (country, city, state, zipcode) equal to “abcd” except the Id which will be used to set the foreign key AddressId for the new user)

How can I do that?

I tried the following:

INSERT INTO Users(Name, Age)
Values((SELECT Name FROM NewUsers),20)

But I don’t know how to create a new Address record for each user inserted and specify the foreign key accordingly.

Thanks a lot for any help

  • 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-04T11:28:01+00:00Added an answer on June 4, 2026 at 11:28 am

    One method would be with two queries, formed like the following:

    INSERT INTO Addresses (Country, State, City, ZipCode)
    SELECT 'abcd', 'abcd', 'abcd', 'abcd' FROM NewUsers
    
    INSERT INTO Users (Name, Age, AddressId)
    SELECT Name, 20, ?? FROM NewUsers
    

    Edit: One simple way to link the users to the addresses would be to temporarily set the country to the username. Then you would have a link to determine the addressId. Once the users table is populated and linked up properly, you can set country back to the default value abcd:

    insert addresses (country, state, city, zipcode)
    select name, 'abcd', 'abcd', 'abcd' from newusers;
    
    insert users (name, age, addressid)
    select u.name, 20, a.id from newusers u
    join addresses a on a.country = u.name;
    
    update a
    set a.country = 'abcd'
    from addresses a join newusers u on a.country = u.name;
    

    Demo: http://www.sqlfiddle.com/#!3/1f09b/8

    There are more complex ways to do this if you want to guarantee transactional consistency if multiple inserts can happen simultaneously, or if you want to allow duplicate names, etc. But based on the example you’ve given and details so far, this method should work.

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

Sidebar

Related Questions

I have an sql database with a table holding users(UserID, Name), a table holding
I am working on an SQL Database I have a Table Users Id Name
I have a SQL database with two tables like this: Users Id (PK) Name
I have a table in SQL database: ID Data Value 1 1 0.1 1
I have a table with data from an SQL database and the table exists
Ok I have a table in my SQL Server database that stores comments. My
I have a table like this in my database (SQL Server 2008) ID Type
In my SQL Server database schema I have a data table with a date
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a SQL database that has a table with a field set to

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.