jemz — 2012-06-25T14:31:28-04:00 — #1
I need some advice is it possible to reference a primary key which is auto_increment?..I am using MySQL.
Thank you in avdvance.
r937 — 2012-06-25T20:36:51-04:00 — #2
yes, it's possible
jemz — 2012-06-27T11:29:17-04:00 — #3
Hi,But i tried to make table with one primary key which is auto_increment,and one foreign key auto_increment which is referencing to other tables primary key..but when i create the table it will generate error because it will not allowed to have 2 auto_increment...
create table secondTable( id int not null auto_increment,
f_id int auto_increment,
foreign key(f_id) referencing firstTable(id));
r937 — 2012-06-27T11:38:44-04:00 — #4
f_id in secondTable should ~not~ be auto_increment, even if id in firstTable is
jemz — 2012-06-27T23:31:20-04:00 — #5
Hi, thank you for the reply...but if i don't make my f_id auto_increment the value of my f_id will be null,everytime i insert a record to the 2 tables.can you help me please how do i fixed this or what should i do...
Thank you in advance.
r937 — 2012-06-28T04:40:23-04:00 — #6
please show the INSERT statements you are using to insert into both tables
jemz — 2012-06-29T05:13:46-04:00 — #7
Hi this is my script in inserting the records.
function addTable1( $id,
$sql = "INSERT INTO table1 values(default,'$id','$fld1','$fld2','$fld3')";
$sql = "insert into table2 values(default,
$result = mysql_query($sql);
r937 — 2012-06-29T06:04:58-04:00 — #8
i don't do php but that script looks quite incomplete
i thought your tables were called firstTable and secondTable, not table1 and table2 ???
i don't see you using LAST_INSERT_ID() or mysql_insert_id() anywhere, which is what you need to use to retrieve the auto_increment value of the first table's insert, so that you can use it for the value of f_id in the second
jemz — 2012-06-29T10:03:22-04:00 — #9
Hi, Thank you for the reply...Can you please show me what you mean i am confuse...How do i retrieve the auto_increment value for my first table then,so that i can insert it to the second table.?...
r937 — 2012-06-29T10:15:11-04:00 — #10
jemz — 2012-06-30T02:22:45-04:00 — #11
Hi,Thank you for this link..okay i will try this,I will write back to let you know if it is working.Thank you again
jemz — 2012-06-30T07:59:54-04:00 — #12
Hi, it's working i used the las_insert_id() ?..thank you so much for helping me.
mittineague — 2014-09-22T11:54:06-04:00 — #13
This topic is now archived. It is frozen and cannot be changed in any way.