docs/en/loading/SQL_transaction.md
import Beta from '../_assets/commonMarkdown/_beta.mdx'
Start a simple SQL transaction to commit multiple DML statements in a batch.
From v3.5.0, StarRocks supports SQL transactions to assure the atomicity of the updated tables when manipulating data within multiple tables.
A transaction consists of multiple SQL statements that are processed within the same atomic unit. The statements in the transaction are either applied or undone together, thus guaranteeing the ACID (atomicity, consistency, isolation, and durability) properties of the transaction.
Currently, the SQL transaction in StarRocks supports the following operations:
:::note
:::
From v4.0 onwards, within one SQL transaction:
The ACID properties of the transaction are guaranteed only on the limited READ COMMITTED isolation level, that is:
A transaction is associated with a single session. Multiple sessions cannot share the same transaction.
A transaction must be started by executing a START TRANSACTION statement. StarRocks also supports the synonym BEGIN.
{ START TRANSACTION | BEGIN [ WORK ] }
After starting the transaction, you can define multiple DML statements in the transaction. For detailed information, see Usage notes.
A transaction must be ended explicitly by executing COMMIT or ROLLBACK.
To apply (commit) the transaction, use the following syntax:
COMMIT [ WORK ]
To undo (roll back) the transaction, use the following syntax:
ROLLBACK [ WORK ]
Create the demo table desT in a shared-data cluster, and load data into it.
:::note If you want to try this example in a shared-nothing cluster, you must skip Step 3 and define only one INSERT statement in Step 4. :::
CREATE TABLE desT (
k int,
v int
) PRIMARY KEY(k);
INSERT INTO desT VALUES
(1,1),
(2,2),
(3,3);
Start a transaction.
START TRANSACTION;
Or
BEGIN WORK;
Define an UPDATE or DELETE statement.
UPDATE desT SET v = v + 1 WHERE k = 1,
Or
DELETE FROM desT WHERE k = 1;
Define multiple INSERT statements.
-- Insert data with specified values.
INSERT INTO desT VALUES (4,4);
-- Insert data from a native table to another.
INSERT INTO desT SELECT * FROM srcT;
-- Insert data from remote storage.
INSERT INTO desT
SELECT * FROM FILES(
"path" = "s3://inserttest/parquet/srcT.parquet",
"format" = "parquet",
"aws.s3.access_key" = "XXXXXXXXXX",
"aws.s3.secret_key" = "YYYYYYYYYY",
"aws.s3.region" = "us-west-2"
);
Apply or undo the transaction.
To apply the SQL statements in the transaction.
COMMIT WORK;
To undo the SQL statements in the transaction.
ROLLBACK WORK;