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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:58:26+00:00 2026-06-04T21:58:26+00:00

I have a table that has an autoincrement surrogate key. I want to use

  • 0

I have a table that has an autoincrement surrogate key. I want to use it as a foreign key of my other table. The thing is, I cant figure out how I can reference it to that table, because it is nearly impossible to determine what I have to reference(the actual value of the surrogate key).

Please be noted that what I am trying to do is adding a tuple/record through my program(outside the dbms). The process is:

  1. Add a new record in Table1 and generate an autoincrement key. Update

  2. Add a new record in Table2 and reference its foreign key to the primary key of Table1. Update

My question is : HOW do I store the foreign key if I didnt know what is it?

Edit:

Sorry for not specifying the database and for the long reply. I use microsoft sql server.

  • 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-04T21:58:28+00:00Added an answer on June 4, 2026 at 9:58 pm

    If your DBMS supports sequences (which most modern DBMS do), simply generate the PK value for table1 then reference that FK value using the “currval” feature of your DBMS.

    Something like (PostgreSQL syntax, but other DBMS supporting sequences have very similar functions):

    INSERT INTO table1 (id, col1, col2) 
    VALUES (nextval('id_sequence'), 'foo', 'bar');
    
    INSERT INTO table2 (t1_id, col1)
    VALUES (currval('id_sequence'), 'foobar');
    

    Another option would be to simply get the ID value in your program and then use that value in both inserts.

    If you have to deal with a DBMS that does not support sequences (e.g. MySQL or SQL Server before 2012) there is usually a function (e.g. @@IDENTITY for SQL Server) that lets you reference the generated ID value of table1 in the second INSERT statement (instead of the currval call)

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

Sidebar

Related Questions

I have a table that has a foreign key to a table that stores
I have a table that has its primary key set to autoincrement. This table
i have a table that has event name and event date. if i want
Hi all I have an appointments table that has among other fields a DATE
I have a table set up that currently has no primary key. All I
I have a table in MySQL that has 3 fields and I want to
I have a MySQL table with a primary key field that has AUTO_INCREMENT on.
I have a table that has an AUTO_INCREMENT field. Currently, it is also a
We have a table that has had the same data inserted into it twice
I have a table that has the following 3 columns, ID, ShortCode, UploadDate. I

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.