I have the following two statements in a shell script:
32 /usr/local/mysql/bin/mysql -u root files -e "TRUNCATE path"
33 /usr/local/mysql/bin/mysql -u root files -e "
34 LOAD DATA INFILE '/tmp/files.txt'
35 INTO TABLE path
36 FIELDS TERMINATED BY ','
37 (size, @d2, @d3, @d4, @d5, path)
38 SET last_modified=str_to_date(CONCAT(@d2, ',', @d3, ',', @d4, ',', @d5), '%b,%d,%T,%Y');"
39 }
How would I make it such that the commands starting on line 32 & 32 either both work or both fail (a transaction) ?
What you need is a transaction:
Unless you
COMMITthe transaction, the database will not have those rows loaded or the columns updated.As a note, some operations cannot be “transactionalized” in MySQL, and this includes schema alterations as well as
TRUNCATE TABLE. Some statements will force a commit regardless of your intentions, so be careful to avoid those when trying to create a transaction.