插入两个表 [英] Inserting into two table

查看:84
本文介绍了插入两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有两张桌子

表A = {ID,Item}

表B = {ID ,IDA,subItem}


其中ID自动递增。


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


如果我在表A中插入,我如何找到项目的ID我只需插入


所以我可以做

INSERT INTO TABLE_A(IDA,''items'')值(''IDA'','''')


非常感谢


Simon

解决方案

simon写道:



我有两张桌子
表A = {ID,Item}
表B = {ID,IDA,subItem}

ID自动增量。

INSERT INTO TABLE_A(''items'')值(''''')

如果我在表A中插入,我怎样才能找到ID我刚刚插入的项目?
所以我可以做
INSERT INTO TABLE_A(IDA,''项目')值(''IDA'','''')

非常感谢

Simon



哎呀。


多数交易 - 您使用的是MySQL还是其他具有交易能力的数据?

数据库?


如果你不是那么有问题 - 两个表中有两个插页

需要一个事务来保证数据的完整性。


>>

哎呀。




为什么?

这是一笔交易 - 你呢?使用MySQL还是其他具有事务功能的数据库?


我正在使用MySQL

如果你没有问题 - 两个表中的两个插入需要
数据事务诚信。




我不确定我是否关注,如果我知道我插入的项目的ID

表A然后我可以用它来表B

我不需要知道表B的自动增量值,我只需要A的

值。


Simon


simon写道:



我有两张桌子
表A = {ID,Item}
表B = {ID,IDA,subItem}


INSERT INTO TABLE_A (''items'')值('''')

如果我在表A中插入,我怎样才能找到我刚刚插入的项目的ID? INSERT INTO TABLE_A(IDA,''items'')值(''IDA'',''a'')

非常感谢

西莫你需要的是: -

>
BEGIN TRANSACTION

如果插入表a成功则{

插入表b

如果插入表b成功则

commit

else

rollback

}

else {

回滚

}

结束交易


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

解决方案

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.


>>

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


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


这篇关于插入两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆