3

I want to insert a row in a table, and when I do the insertion I want to insert another row in another table at the same time.

What is the more efficient way to do this in an Android app?

Is it more efficient to do a trigger when I insert one row and SQL takes care of the insertion, or do two inserts with database.insert() in Java?

The information needed to insert the second row is already in the first insertion.

Thanks!

oxygenpt
  • 368
  • 2
  • 17

1 Answers1

1

I want to insert a row in a table, and when I do the insertion I want to insert another row in another table at the same time.

For sure use for this TRIGGER, because it working on database layer so you only need to create it and then it will be called itself when you do something(depends on how you implement trigger).

Also it's more faster and you do not call it explicit, just let database work. All what is database able to do, let do it on database.

Update:

So after a little discussion with @Graham Borland you can use also inserts in TRANSACTION.
Have look at Android Database Transaction.

And conclusion what is faster?

I exactly don't know it because i don't have performance tests but for sure TRANSACTIONS are the safest i think but whether faster than TRIGGERS this i don't know exactly but usage of transactions is very good and mainly safe approach to prevent database get to incorrect state that is very undesirable.

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • Why is it faster and safer than multiple inserts inside a transaction? Can you provide any references? – Graham Borland Jul 03 '12 at 11:38
  • @GrahamBorland maybe my claim is too brave but i think that all what is database able to do itself is faster and safer but your suggestion is very right, i forgot that he can use transactions and also its maybe better because transaction (you know it but for others) guarantees that is done if all operations are sucessfull, so known feature ACID. – Simon Dorociak Jul 03 '12 at 11:45
  • So, from your answer, I understood that Android Database Transactions are definitely safer, but you weren't clear on the performance issue. Are Triggers faster? And are they that faster that counterbalance the safety and simplicity of Transactions? – Marco Castanho Feb 24 '16 at 15:43