tags:

views:

69

answers:

3

Hello.

Is it any information about commands separated with ';' inside one query - are they atomic or not? I'm interested in actual versions of popular databases: MySQL, MSSQL, SQLite etc? For example, if 100 clients will spam following query:

"insert into test ( name ) values ( '1' ); insert into test ( name ) values ( '2' )"

Will database contains items in sequence '1', '2', '1', '2' etc or it is possible for each command divided by ';' from 100 clients to race each over, resulting in '1', '1', '2', '1', '2', '2', '2' etc ?

+5  A: 

Wrap it in a transaction. Otherwise, no, there is in general no guarantee that your statements will be atomic.

Steven Schlansker
Thanks. Is it any way in SQL to execute > 1 'insert' or 'update' inside a query without creating a transaction? Or it's a strict rule that only single modifications are atomic and i need to create transaction every time?
Eye of Hell
Why do you care so much that it's not in a transaction?
Steven Schlansker
Transaction code is a lot of commands and a rollback in case of a fail. Single SQL request is a... single request? :)
Eye of Hell
+1  A: 

Within the batch they will execute in sequential order, but that's not your problem. If 100 clients emit that SQL at the same time there is no guarantee that the 2 INSERTS will execute one after the other without another INSERT executing in-between.

Mitch Wheat
+1  A: 

I guess it's a theoretical question, but why would you need the sequence?

You can't rely on the sequence in the database anyway, so add some sort of timestamp-column to your table, and let your clients initialize a timestamp variable which they add to their records when inserting.

Peter Lang
A sequence in important if database holds some OOP objects that are linked to each over. Inserting and deleting such objects will modify > 1 record, and modification itself must be atomic or data consistency will be breaked :). I'm new to database programming, and i'm researching now is transactions the only way or not for atomic changes.
Eye of Hell
Then that's exactly what transactions are for, right? It's a great benefit of databases to allow concurrent modifications where possible. The records could still be inserted/updated in random order, but changes are only visible to other sessions after COMMIT.
Peter Lang