List:General Discussion« Previous MessageNext Message »
From:Jon Frisby Date:July 24 2002 10:49pm
Subject:RE: Primary and Foreign Keys in MySQL
View as plain text  
I'm assuming you have AUTO_INCREMENT columns in each table?

Try this:

INSERT INTO TABLE1(...) VALUES(...);
SELECT @t1id := LAST_INSERT_ID();
INSERT INTO TABLE1A(table1_id, ...) VALUES(@t1id, ....);
INSERT INTO TABLE1B(table1_id, ...) VALUES(@t1id, ....);
...

-JF


> -----Original Message-----
> From: Aamer Rauf [mailto:aamer@stripped]
> Sent: Wednesday, July 24, 2002 12:55 PM
> To: mysql@stripped
> Subject: Primary and Foreign Keys in MySQL
> 
> 
> Hello,
> 
> I am working on MySQL database. I have come across a 'problem' 
> but don't know 
> how to get around that. The problem is the following:
> I have a table, say TABLE1, with primary key being called as table1_id. 
> Now there are other tables, like TABLE1A, TABLE1B, TABLE1C etc.. 
> where I want to 
> use primary key of TABLE1 (i.e table1_id) as foreign key. I see a 
> problem in 
> using the function LAST_INSERT_ID(). Lets say I use it to insert 
> a record in 
> TABLE1A, that will work fine. But now if I use it to put a record 
> in TABLE1B it 
> would give me the primary key ID of TABLE1A rather than TABLE1. 
> Please help me 
> out on this.
> 
> Aamer 
> 
> 
> 
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
> 
> To request this thread, e-mail <mysql-thread115468@stripped>
> To unsubscribe, e-mail 
> <mysql-unsubscribe-jfrisby=yourfreedvds.com@stripped>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
> 
> 
Thread
Primary and Foreign Keys in MySQLAamer Rauf24 Jul
  • RE: Primary and Foreign Keys in MySQLJon Frisby25 Jul