TRANSACTION Beta
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Synopsis
The transaction statement block makes changes to multiple rows in one or more tables in a distributed ACID transaction.
Syntax
Diagram
Grammar
transaction_block ::= BEGIN TRANSACTION
( insert | update | delete ) ';'
[ ( insert | update | delete ) ';' ...]
END TRANSACTION ';'
Where
ANSI SQL Syntax
Alternatively, YubaByte DB supports ANSI SQL START TRANSACTION
and COMMIT
statements.
transaction_block ::= START TRANSACTION ';'
( insert | update | delete ) ';'
[ ( insert | update | delete ) ';' ...]
COMMIT ';'
Semantics
- An error is raised if transactions are not enabled in any of the tables inserted, updated, or deleted.
- Currently, an error is raised if any of the
INSERT
,UPDATE
, orDELETE
statements contains anIF
clause.
Examples
Create a table with transactions enabled
You can do this as shown below.
cqlsh:example> CREATE TABLE accounts (account_name TEXT,
account_type TEXT,
balance DOUBLE,
PRIMARY KEY ((account_name), account_type))
WITH transactions = { 'enabled' : true };
Insert some data
You can do this as shown below.
cqlsh:example> INSERT INTO accounts (account_name, account_type, balance)
VALUES ('John', 'savings', 1000);
cqlsh:example> INSERT INTO accounts (account_name, account_type, balance)
VALUES ('John', 'checking', 100);
cqlsh:example> INSERT INTO accounts (account_name, account_type, balance)
VALUES ('Smith', 'savings', 2000);
cqlsh:example> INSERT INTO accounts (account_name, account_type, balance)
VALUES ('Smith', 'checking', 50);
cqlsh:example> SELECT account_name, account_type, balance, writetime(balance) FROM accounts;
account_name | account_type | balance | writetime(balance)
--------------+--------------+---------+--------------------
John | checking | 100 | 1523313964356489
John | savings | 1000 | 1523313964350449
Smith | checking | 50 | 1523313964371579
Smith | savings | 2000 | 1523313964363056
Update 2 rows with the same partition key
You can do this as shown below.
cqlsh:example> BEGIN TRANSACTION
UPDATE accounts SET balance = balance - 200 WHERE account_name = 'John' AND account_type = 'savings';
UPDATE accounts SET balance = balance + 200 WHERE account_name = 'John' AND account_type = 'checking';
END TRANSACTION;
cqlsh:example> SELECT account_name, account_type, balance, writetime(balance) FROM accounts;
account_name | account_type | balance | writetime(balance)
--------------+--------------+---------+--------------------
John | checking | 300 | 1523313983201270
John | savings | 800 | 1523313983201270
Smith | checking | 50 | 1523313964371579
Smith | savings | 2000 | 1523313964363056
Update 2 rows with the different partition keys
You can do this as shown below.
cqlsh:example> BEGIN TRANSACTION
UPDATE accounts SET balance = balance - 200 WHERE account_name = 'John' AND account_type = 'checking';
UPDATE accounts SET balance = balance + 200 WHERE account_name = 'Smith' AND account_type = 'checking';
END TRANSACTION;
cqlsh:example> SELECT account_name, account_type, balance, writetime(balance) FROM accounts;
account_name | account_type | balance | writetime(balance)
--------------+--------------+---------+--------------------
John | checking | 100 | 1523314002218558
John | savings | 800 | 1523313983201270
Smith | checking | 250 | 1523314002218558
Smith | savings | 2000 | 1523313964363056
See Also
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .