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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:44:35+00:00 2026-05-25T03:44:35+00:00

Lets say that I have two tables. The first is: table lists, with list_id

  • 0

Lets say that I have two tables.

The first is: table lists, with list_id SERIAL, list_name TEXT

The second table is, trivially, a table which says if the list is public: list_id INT, is_public INT

Obviously a bit of a contrived case, but I am planning out some tables and this seems to be an issue. If I insert a new list_name into table lists, then it’ll give me a new serial number…but now I will need to use that serial number in the second table. Obviously in this case, you could simply add is_public to the first table, but in the case of a linking list where you have a compound key, you’ll need to know the serial value that was returned.

How do people usually handle this? Do they get the return type from the insert using whatever system they’re interacting with the database with?

  • 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-25T03:44:35+00:00Added an answer on May 25, 2026 at 3:44 am

    One approach to this sort of thing is:

    • INSERT...
    • SELECT lastval()
    • INSERT...

    INSERT into the first table, use lastval() to get the “value most recently obtained with nextval for any sequence” (in the current session), and then use that value to build your next INSERT.

    There’s also INSERT ... RETURNING:

    The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted. This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number.

    Using INSERT ... RETURNING id basically combines the first two steps above into one so you’d do:

    • INSERT ... RETURNING id
    • INSERT ...

    where the second INSERT would use the id returned from the first INSERT.

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

Sidebar

Related Questions

Lets say I have two tables, one for transactions, and another table who's primary
Lets say I have a simple table that only contains two columns: MailingListUser -
Lets say I have these two tables: CREATE TABLE nodes ( id INTEGER PRIMARY
Let's say I have two tables: Table 1 has the columns NOTE_ID (a unique
I have two tables that I want to join into one table and use
Ok, so lets say I have a table comprising three columns. The first column
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Lets say that I have a header user control in a master page, and
Lets say you have interface definition. That interface can be Operation . Then you
I have one field that I need to sum lets say named items However

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.