title: SQL: Insert data from another table in a table that contains foreign keys
body: <p>In a SQL DATABASE</p>
  
  <p>I have a Table Users</p>
  
  <pre><code> Id   Name   Age    AddressId
  ----+------+------+-----------
  </code></pre>
  
  <p>Where AddressId is a foreign key to a table names Addresses</p>
  
  <p>The Addresses Table:</p>
  
  <pre><code> Id   Country   State  City   ZipCode
  ----+---------+------+------+---------
  </code></pre>
  
  <p>This is a ONE-TO-ONE Relationship: Each User Has 1 address and each address has one user</p>
  
  <p>I have a new table named NEWUsers</p>
  
  <pre><code>Id   Name  
  ----+------
  </code></pre>
  
  <p>It has only Id and Name.</p>
  
  <p>What i want to do is this:</p>
  
  <p>Write a script to insert all the records From the NEWUSers Table into the Users Table.</p>
  
  <ul>
  <li>I want The Age to be default 20 for all new users</li>
  <li>And for each new user inserted I need to create a new Address record for him </li>
  <li>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)</li>
  </ul>
  
  <p>How can I do that?</p>
  
  <p>I tried the following:</p>
  
  <pre><code>INSERT INTO Users(Name, Age)
  Values((SELECT Name FROM NewUsers),20)
  </code></pre>
  
  <p>But I don't know how to create a new Address record for each user inserted and specify the foreign key accordingly.</p>
  
  <p>Thanks a lot for any help</p>