Solved

Inserting data into a MySQL(InnoDB) table with an auto-increment, foreign key

Posted on 2008-06-10
3
920 Views
Last Modified: 2008-06-10
Hi Experts,

I'm working on a new database which contains a few foreign key relationships. one particular relationship has a parent table with an auto-increment primary key which is used as the foreign key for another table.

here is a quick overview of the two tables:

Batch                                             Record_Counts
--------------                                    -------------------
(PK) Batch_ID                                (PK, FK)Batch_ID_FK
Creation_Date                                E_Record_Count
Batch_Number                               O_Record_Count
.....                                                  ........

now, given that the PK/FK Batch_ID is a auto-increment entry, what would be the best way to insert
the record_counts entry that corresponds to the batch table entry?
0
Comment
Question by:myabko
  • 2
3 Comments
 
LVL 24

Expert Comment

by:fridom
Comment Utility
I'm not getting the trouble spot.

insert into Record_Counts (Batch_ID_FK)
       select Batch_ID where
        Creation_Date = "2008-03-21" and
       Batch_number = 10;


or the like

Regards
Friedrich
0
 

Author Comment

by:myabko
Comment Utility
is there a possibility of getting the last created auto-incremented variable without relying on other data within the other fields within the table? while rare, there may be a case where the creation date and batch number are repeated with a different batch ID
0
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
Comment Utility
insert into Record_Counts (Batch_ID_FK)
       select max(Batch_ID)

or the like, I guess MySQL has some fancier way to retrieve the last generated value.
Regards
Friedrich
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mysql sql statement - SQL INSERT INTO SELECT 11 46
Query question 4 29
Oracle TEXT search question 9 26
Instering to MySQL table 5 15
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now