Skip to content

Commit 57eb2d7

Browse files
committed
Add Run Statements In A Transaction as a MySQL TIL
1 parent 613f1f8 commit 57eb2d7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1371 TILs and counting..._
13+
_1372 TILs and counting..._
1414

1515
---
1616

@@ -575,6 +575,7 @@ _1371 TILs and counting..._
575575
- [Dump A Database To A File](mysql/dump-a-database-to-a-file.md)
576576
- [Ignore Duplicates When Inserting Records](mysql/ignore-duplicates-when-inserting-records.md)
577577
- [List Databases And Tables](mysql/list-databases-and-tables.md)
578+
- [Run Statements In A Transaction](mysql/run-statements-in-a-transaction.md)
578579
- [Select Rows After An Offset](mysql/select-rows-after-an-offset.md)
579580
- [Show Create Statement For A Table](mysql/show-create-statement-for-a-table.md)
580581
- [Show Tables That Match A Pattern](mysql/show-tables-that-match-a-pattern.md)
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Run Statements In A Transaction
2+
3+
I'm connecting to a production MySQL database to make some changes to a
4+
specific user account. That means I am going to run an `update` statement and I
5+
expect that statement to affect exactly *one* row in the `users` table.
6+
7+
If I run the `update` statement in a transaction, then I can verify all looks
8+
good before committing those changes. And importantly, I can rollback the
9+
changes if anything looks off.
10+
11+
```sql
12+
> start transaction;
13+
14+
> update users set roles = 'admin' where id = '1234';
15+
Query ok, 1 row affected
16+
17+
> select * from users where id = '1234';
18+
-- check that all looks good
19+
20+
> commit;
21+
```
22+
23+
In the above case, all looked good, so I ran `commit`. If more rows than I
24+
expected were affected or the changed record didn't look right, I could instead
25+
`rollback`. None of those changes would make it into live production data.
26+
27+
[source](https://dev.mysql.com/doc/refman/8.0/en/commit.html)

0 commit comments

Comments
 (0)