插入两个表

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

问题描述




我有两张桌子

表A = {ID,Item}

表B = {ID ,IDA,subItem}


其中ID自动递增。


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


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


所以我可以做

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


非常感谢


Simon

解决方案



哎呀。


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

数据库?


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

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





为什么?

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


我正在使用MySQL

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




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

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

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

值。


Simon



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.




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



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


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

07-17 18:08
查看更多