Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is single sql statement always atomic in innodb?

Tags:

sql

mysql

innodb

There is a sql like that

UPDATE xxx SET num = num -1 WHERE num > 0;

Will this be an atomic operation even if I have no transaction statement?

Can this sql ensure the num field always be non-negative?

like image 253
jilen Avatar asked Mar 16 '12 05:03

jilen


People also ask

Are SQL transactions atomic?

In SQL databases transaction atomicity is implemented most frequently using write-ahead logging (meaning that the transaction log entries are written before the actual tables and indexes are updated).

Are MySQL queries atomic?

MySQL 8.0 supports atomic Data Definition Language (DDL) statements. This feature is referred to as atomic DDL. An atomic DDL statement combines the data dictionary updates, storage engine operations, and binary log writes associated with a DDL operation into a single, atomic operation.

Are transactions atomic in nature?

A transaction is an atomic set of database queries.

Are MySQL inserts atomic?

MyISAM is atomic, however not necessarily consistent, durable or isolated. See my answer. – Johannes H. MyISAM is not atomic, as the term generally applies to database systems.


1 Answers

If autocommit is enabled, yes it will be executed atomically. Every statement will be a single transaction unless autocommit is disabled.

It should be noted that autocommit is enabled by default, hence why START TRANSACTION is usually necessary to initiate a transaction.

like image 116
Ditmar Wendt Avatar answered Sep 30 '22 12:09

Ditmar Wendt