Click here to Skip to main content
15,914,452 members
Home / Discussions / Database
   

Database

 
AnswerRe: How To Create Non Effect Update Pin
Ashfield19-Dec-08 1:47
Ashfield19-Dec-08 1:47 
GeneralNon Effect Update Pin
David Mujica19-Dec-08 4:36
David Mujica19-Dec-08 4:36 
AnswerWrap the logic in TSQL Pin
David Mujica19-Dec-08 4:29
David Mujica19-Dec-08 4:29 
GeneralOops Pin
David Mujica19-Dec-08 4:38
David Mujica19-Dec-08 4:38 
GeneralRe: Wrap the logic in TSQL Pin
Ashfield19-Dec-08 5:02
Ashfield19-Dec-08 5:02 
GeneralRe: Wrap the logic in TSQL Pin
Wendelius19-Dec-08 5:10
mentorWendelius19-Dec-08 5:10 
QuestionInstall SQL Database automatically...... [modified] Pin
grks18-Dec-08 20:52
grks18-Dec-08 20:52 
QuestionLinking tables properly with foreign keys Pin
mevatron118-Dec-08 7:28
mevatron118-Dec-08 7:28 
Hi,

I'm fairly new to database programming. So, here is the problem I'm having.

Here is a basic description of the two tables I'm using. (The database is a MySQL RDBMS)
PersonTable
PersonId, INT, AUTO_INCREMENT (Primary Key)
Name, VARCHAR(32)

ImageTable
ImageId, INT, AUTO_INCREMENT (Primary Key)
ImageBlob, LARGE_BLOB
PersonId, INT (Foreign Key)


Basically, I want to insert the person record into the person table, next insert the image for that person into the image table, and finally link the ImageTable.PersonId to the PersonTable.PersonId.

So, here is the code I'm currently using

CRecordset personTable;
CRecordset imageTable;

personTable.Open(CRecordset::snapshot, "SELECT * FROM person");
imageTable.Open(CRecordset::snapshot, "SELECT * FROM image");

//...

personTable.AddNew();
personTable.m_Name = "Joe";
personTable.Update();

personTable.Requery();
personTable.MoveLast();

long personId_ForeignKey = personTable.m_PersonId;

//... later in the image table insert code
imageTable.AddNew();
// set the image blob here
imageTable.m_PersonId = personId_ForeignKey;
imageTable.Update();


The above code is not safe when clients concurrently submit records to the database. This is due to my faulty assumption that the last record is the one that the current client inserted, which may or may not be true depending on the interleaving of operations.

My question is this, is there a way to safely get the value of the primary key after inserting the record into the table, so that I can use it to populate the foreign key columns of the other table?

-Will
AnswerRe: Linking tables properly with foreign keys Pin
Wendelius18-Dec-08 8:29
mentorWendelius18-Dec-08 8:29 
Questionsqltransaction.rollback in terms of programming sequence of actions Pin
vjvjvjvj18-Dec-08 3:42
vjvjvjvj18-Dec-08 3:42 
AnswerRe: sqltransaction.rollback in terms of programming sequence of actions Pin
Wendelius18-Dec-08 9:11
mentorWendelius18-Dec-08 9:11 
QuestionSQLDMO.dll Error Pin
SelvaSR18-Dec-08 1:08
SelvaSR18-Dec-08 1:08 
AnswerRe: SQLDMO.dll Error Pin
Rob Graham18-Dec-08 4:17
Rob Graham18-Dec-08 4:17 
GeneralRe: SQLDMO.dll Error Pin
SelvaSR18-Dec-08 22:43
SelvaSR18-Dec-08 22:43 
GeneralRe: SQLDMO.dll Error Pin
Rob Graham20-Dec-08 3:35
Rob Graham20-Dec-08 3:35 
QuestionHow to creat a consolidat setup for an application built in vb 2005 Pin
kennylal18-Dec-08 0:37
kennylal18-Dec-08 0:37 
AnswerRe: How to creat a consolidat setup for an application built in vb 2005 Pin
Rob Graham20-Dec-08 7:36
Rob Graham20-Dec-08 7:36 
QuestionHotel reservation Pin
WebMaster17-Dec-08 23:11
WebMaster17-Dec-08 23:11 
AnswerRe: Hotel reservation Pin
Blue_Boy17-Dec-08 23:40
Blue_Boy17-Dec-08 23:40 
AnswerRe: Hotel reservation Pin
Ashfield18-Dec-08 1:04
Ashfield18-Dec-08 1:04 
QuestionTable values import to excel Sheet Pin
thangarajappa17-Dec-08 23:01
thangarajappa17-Dec-08 23:01 
AnswerRe: Table values import to excel Sheet Pin
Wendelius18-Dec-08 10:02
mentorWendelius18-Dec-08 10:02 
Questionsql server 2000 Pin
kulandaivel_mca200717-Dec-08 22:56
kulandaivel_mca200717-Dec-08 22:56 
AnswerRe: sql server 2000 Pin
Blue_Boy17-Dec-08 23:27
Blue_Boy17-Dec-08 23:27 
Questioneditor Stored procedure in MYSQL Pin
ptr_Electron17-Dec-08 21:44
ptr_Electron17-Dec-08 21:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.