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

  • Home
  • SEARCH
  • 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 628009
In Process

The Archive Base Latest Questions

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

Earlier today I asked this question which arose from A- My poor planning and

  • 0

Earlier today I asked this question which arose from A- My poor planning and B- My complete disregard for the practice of normalizing databases. I spent the last 8 hours reading about normalizing databases and the finer points of JOIN and worked my way through the SQLZoo.com tutorials.

I am enlightened. I understand the purpose of database normalization and how it can suit me. Except that I’m not entirely sure how to execute that vision from a procedural standpoint.

Here’s my old vision: 1 table called “files” that held, let’s say, a file id and a file url and appropos grade levels for that file.

New vision!: 1 table for “files”, 1 table for “grades”, and a junction table to mediate.

But that’s not my problem. This is a really basic Q that I’m sure has an obvious answer- When I create a record in “files”, it gets assigned the incremented primary key automatically (file_id). However, from now on I’m going to need to write that file_id to the other tables as well. Because I don’t assign that id manually, how do I know what it is?

If I upload text.doc and it gets file_id 123, how do I know it got 123 in order to write it to “grades” and the junction table? I can’t do a max(file_id) because if you have concurrent users, you might nab a different id. I just don’t know how to get the file_id value without having manually assigned it.

  • 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-13T19:35:26+00:00Added an answer on May 13, 2026 at 7:35 pm

    You may want to use LAST_INSERT_ID() as in the following example:

    START TRANSACTION;
    INSERT INTO files (file_id, url) VALUES (NULL, 'text.doc');
    INSERT INTO grades (file_id, grade) VALUES (LAST_INSERT_ID(), 'some-grade');
    COMMIT;
    

    The transaction ensures that the operation remains atomic: This guarantees that either both inserts complete successfully or none at all. This is optional, but it is recommended in order to maintain the integrity of the data.

    For LAST_INSERT_ID(), the most
    recently generated ID is maintained in
    the server on a per-connection basis.

    It is not changed by another client.
    It is not even changed if you update
    another AUTO_INCREMENT column with a
    nonmagic value (that is, a value that
    is not NULL and not 0).

    Using
    LAST_INSERT_ID() and AUTO_INCREMENT
    columns simultaneously from multiple
    clients is perfectly valid. Each
    client will receive the last inserted
    ID for the last statement that client
    executed.

    Source and further reading:

    • MySQL Reference: How to Get the Unique ID for the Last Inserted Row
    • MySQL Reference: START TRANSACTION, COMMIT, and ROLLBACK Syntax
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This arose from a question earlier today on the subject of bignum libraries and
Earlier today I asked this question . After spending some time investigating this issue,
I know I have asked this question in a different manner earlier today but
Earlier today I asked a question about configuring log4net from code and got an
Earlier today I posted another post where @Darin Dimitrov helped me great, however once
...and I mean LITERALLY just beginning. As in, I just started earlier today. All
edit 10/8/10 @ 8:20am est - since I can't make this work in prod,
I've been playing around with Pyramid lately and, coming from a Pylons background, I've
I am writing a script to make chunks of a text and send it
I just forked a copy of an app on github and saved it on

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.