473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting into two table

Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')

many thanks

Simon
Jul 17 '05 #1
14 2008
simon wrote:
Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')

many thanks

Simon

Whoops.

Thats a transaction - are you using MySQL or another transaction capable
database ?

If you aren't then you have a problem - two inserts in two tables
requires a transaction for data integrity.
Jul 17 '05 #2
>>
Whoops.


Why?

Thats a transaction - are you using MySQL or another transaction capable
database ?
I am using MySQL

If you aren't then you have a problem - two inserts in two tables requires
a transaction for data integrity.


I am not sure I follow, if I know the ID of the of the item I inserted in
Table A then I can use it for table B
I don't need to know the auto increment value of Table B, all I need is the
value of A.

Simon
Jul 17 '05 #3
simon wrote:
Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')

many thanks

Simon

What you need is something along the lines of:-

pseudo code:

BEGIN TRANSACTION
if insert into table a succeeds then {
insert into table b
if insert into table b succeeds then
commit
else
rollback
}
else {
rollback
}
END TRANSACTION
Jul 17 '05 #4
Use mysql_insert_id ():

http://us4.php.net/manual/en/functio...-insert-id.php

"simon" <sp********@myo ddweb.com> wrote in message
news:37******** *****@individua l.net...
Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')

many thanks

Simon

Jul 17 '05 #5
>>

What you need is something along the lines of:-

pseudo code:

BEGIN TRANSACTION
if insert into table a succeeds then {
insert into table b
if insert into table b succeeds then
commit
else
rollback
}
else {
rollback
}
END TRANSACTION


I see, but what I was looking for was the ID of newly, (successfully),
inserted item in Table A.

Simon
Jul 17 '05 #6
simon wrote:

What you need is something along the lines of:-

pseudo code:

BEGIN TRANSACTION
if insert into table a succeeds then {
insert into table b
if insert into table b succeeds then
commit
else
rollback
}
else {
rollback
}
END TRANSACTION

I see, but what I was looking for was the ID of newly, (successfully),
inserted item in Table A.

Simon

No problem - Kevin has given you the answer to that. but if you are
using MySQL then you need to use INNODB tables not iSAM tables - ISAM
tables don't support transactions, and you are proposing a two table
operation which is essentially atomic. In other words, two inserts on
two tables both of which have to succeed to ensure data integrity
Jul 17 '05 #7
simon wrote:
Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')

many thanks

Simon


In MySQL:

SELECT LAST_INSERT_ID( )

See this page for detailed information:
<http://dev.mysql.com/doc/mysql/en/information-functions.html>

JP

--
Sorry, <de*****@cauce. org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #8
"simon" <sp********@myo ddweb.com> writes:
Hi,

I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}

Where ID auto increment.

INSERT INTO TABLE_A ('items') values ('a')

If I did an insert into Table A how can I find the ID of the item I just
inserted?
so I can do
INSERT INTO TABLE_A ( IDA, 'items') values ( 'IDA', 'a')


Straight from the PHP manual;

int mysql_insert_id ( [resource link_identifier])

mysql_insert_id () returns the ID generated for an AUTO_INCREMENT column by the
previous INSERT query using the given link_identifier . If link_identifier
isn't specified, the last opened link is assumed.

....You'd be smart doing the two inserts inside a transaction block
also.

--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 17 '05 #9

"simon" <sp********@myo ddweb.com> wrote in message
news:37******** *****@individua l.net...

Whoops.


Why?

Thats a transaction - are you using MySQL or another transaction capable
database ?


I am using MySQL

If you aren't then you have a problem - two inserts in two tables
requires a transaction for data integrity.


I am not sure I follow, if I know the ID of the of the item I inserted in
Table A then I can use it for table B
I don't need to know the auto increment value of Table B, all I need is
the value of A.

Simon


1 - It's always worth reading a little bit. You might notice that
http://php.net/mysql_insert_id has some pertinent information for you.

2 - As pointed out, you might want to think about transactions. IIRC, mysql
offers a SELECT INSERT_ID; or something similar (the mysql manual online
will tell you this) that you could use as part of a transaction to make
things work ok.

3 - There's a certain school of thought that says you might need to rethink
your data/application design to avoid this situation being necessary if
possible...

Matt
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2420
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish, Swedish or English). There are about a dozen tables with columns that need localization. Doing this in the application level was a no-goer. It...
3
6859
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when inserting/deleting rows from a large table. My scenario: Table (lets call it FACT1) with 1000 million rows distributed on 12
1
3708
by: ing42 | last post by:
I have a problem with inserting records into table when an indexed view is based on it. Table has text field (without it there is no problem, but I need it). Here is a sample code: USE test GO CREATE TABLE dbo.aTable ( INT NOT NULL
3
4264
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be created and I could be able to insert data. But with this code below I've failed could someone help me and tell me where I'm going wrong: private...
1
2615
by: madhuxml82 | last post by:
Dear Forum Members, I have generated an XML Schema and a Table of XMLType referencing the XML Schema. Now When I am Inserting the Data into the Table. I am getting the Error 0RA-30937: Error is Comming. I am pasting the Code which I have written. Appreciate any Help... begin dbms_xmlschema.registerSchema(...
5
5688
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I want to insert two or more records into the participants table. My problem is that I have no idea what foreign key to use when inserting names...
2
3063
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography section: Discography --------------------- DiscID
5
2153
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. So here's my method. I created two tables with the same structure as the table I'm inserting from. One table, Split_Temp, is the one I'll be...
5
10037
by: billelev | last post by:
I have a large array of data (1000 x 40 x 3) that I am inserting into a database table. It is incredibly slow, and so I was wondering if there is a quicker way of inserting array data into a table. Here is the code I am currently using: ' ,, With rs For nSec = 0 To UBound(vtData, 2) For ndate = 0 To...
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.