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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:30:01+00:00 2026-05-26T21:30:01+00:00

Let’s say I have a database with two tables Persons and PhoneNumbers , where

  • 0

Let’s say I have a database with two tables Persons and PhoneNumbers, where the PhoneNumbers table has a foreign key to Persons. If I want to insert a person with a phone number in a single transaction, I can write a query like this:

BEGIN TRANSACTION;
INSERT INTO Persons(Name) VALUES(...);
INSERT INTO PhoneNumbers(PersonForeignKey, Number) VALUES(last_insert_rowid(), ...);
END TRANSACTION;

But what if I want to insert a person with multiple phone numbers? The obvious way:

BEGIN TRANSACTION;
INSERT INTO Persons(Name) VALUES(...);
INSERT INTO PhoneNumbers(PersonForeignKey, Number) VALUES(last_insert_rowid(), ...);
INSERT INTO PhoneNumbers(PersonForeignKey, Number) VALUES(last_insert_rowid(), ...);
INSERT INTO PhoneNumbers(PersonForeignKey, Number) VALUES(last_insert_rowid(), ...);
END TRANSACTION;

won’t work, of course, because for the second phone number, last_insert_rowid() will return the rowid of the first phone number instead of the person.

Is there a way to do this using basic SQL (SQLite, specifically)?

Conclusion

Apparently, there is no direct way to do this. I’ve made a few benchmarks with @Michal Powaga’s suggestions and a few other ideas based on temporary tables, and the fastest way to do it seems to be something like this:

CREATE TEMPORARY TABLE IF NOT EXISTS Insert_PhoneNumbers(PersonForeignKey INTEGER, PhoneNumber VARCHAR);
DELETE FROM Insert_PhoneNumbers;
INSERT INTO Insert_PhoneNumbers(PhoneNumber) VALUES ('Phone 1');
INSERT INTO Insert_PhoneNumbers(PhoneNumber) VALUES ('Phone 2');

INSERT INTO Persons(Name) VALUES(...);

UPDATE Insert_PhoneNumbers SET PersonForeignKey=last_insert_rowid();

INSERT INTO PhoneNumbers(PersonForeignKey, Number)
   SELECT PersonForeignKey, PhoneNumber
   FROM Insert_PhoneNumbers

creating and updating a temporary table seems to be very fast (compared to queries on the Persons or PhoneNumbers-tables) and the insert speed won’t depend on the number of persons/phone numbers already in the database, so that’s the solution I chose.

  • 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-26T21:30:01+00:00Added an answer on May 26, 2026 at 9:30 pm

    You can store last_insert_rowid() after person insertion to temporary table or this might work too:

    BEGIN TRANSACTION;
    INSERT INTO Persons(Name) VALUES(...);
    
    INSERT INTO PhoneNumbers(PersonForeignKey, Number) 
    VALUES(last_insert_rowid(), 'number 1');
    
    INSERT INTO PhoneNumbers(PersonForeignKey, Number) 
    SELECT PersonForeignKey, 'number 2' 
    FROM PhoneNumbers where PhonePrimaryKey = last_insert_rowid();
    
    INSERT INTO PhoneNumbers(PersonForeignKey, Number) 
    SELECT PersonForeignKey, 'number 3' 
    FROM PhoneNumbers where PhonePrimaryKey = last_insert_rowid();
    
    END TRANSACTION;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two files.. I want to compare them side-by-side and see
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say my SQL database contains a table called 'Customer'. This Customer has an
Let's say I have two Databases like so : DatabaseA create table Table1 (
Let's say I have a drive such as C:\ , and I want to
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say i have an android device that has some extra buttons on it,
let's say i have three tables, each one relates to another, when i need

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.