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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:24:55+00:00 2026-05-30T22:24:55+00:00

So I started to write a forum, because I want to learn a little

  • 0

So I started to write a forum, because I want to learn a little SQL (MySQL).

So firstly, I have a posts table, and there are a post_id and topic_id columns.

But, on the other side I have a topics table.
In the topics table I also have a last_post_id and topic_id, too !

So they are referencing to each other.

So the question is: When a user creates a topic (and the same time writes the first post), how to INSERT these id’s effectively??

Because now I have to do the following steps:

  1. Insert topic related data (topic title..etc, insert last_post_id =0 cause I dont know this value here), then retrieve topic_id

  2. Insert post data (text, username.., topic_id), and retieve this particular post_id

  3. Update topics table, with the retrieved post_id (set last_post_id).

Isn’t it too long, tedious, uneffective..?

Thanks for your time. My english is not the best.. sry.

  • 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-30T22:24:57+00:00Added an answer on May 30, 2026 at 10:24 pm

    You could do it using a single transaction, but the basic idea is the same. You need to insert the new topic before having topic_id, and you need to insert the new post before having last_post_id. With your current tables, there’s no way around that.

    You could also use a TRIGGER that automatically updates the last_post_id field when a new record is added to posts. That doesn’t change the number of queries executed, but it would be handled transparently by MySQL itself.

    Also, the point raised by mu is too short in his answer about setting last_post_id to NULL is a good idea.

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

Sidebar

Related Questions

I'm an avid vim user and have started to write some SQL code recently.
I have recently started to learn Objective-C and write my tests using OCUnit that
Question simple and quick: I have started to use Netbeans to write some code
I started using spirit yesterday. I have to write a parser for a small
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write
I started by googling and found the article How to write INSERT if NOT
I just started with a project that requires me to write to PDF file.
As an experiment I'm trying to write a generic MVP framework. I started with:
Recently an application I wrote started not working on Internet Explorer. There has been
I know there already are a lot of posts floating on the web regarding

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.