mirror of
https://github.com/jbranchaud/til
synced 2026-01-08 01:28:02 +00:00
Add Run Statements In A Transaction as a MySQL TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1371 TILs and counting..._
|
_1372 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -575,6 +575,7 @@ _1371 TILs and counting..._
|
|||||||
- [Dump A Database To A File](mysql/dump-a-database-to-a-file.md)
|
- [Dump A Database To A File](mysql/dump-a-database-to-a-file.md)
|
||||||
- [Ignore Duplicates When Inserting Records](mysql/ignore-duplicates-when-inserting-records.md)
|
- [Ignore Duplicates When Inserting Records](mysql/ignore-duplicates-when-inserting-records.md)
|
||||||
- [List Databases And Tables](mysql/list-databases-and-tables.md)
|
- [List Databases And Tables](mysql/list-databases-and-tables.md)
|
||||||
|
- [Run Statements In A Transaction](mysql/run-statements-in-a-transaction.md)
|
||||||
- [Select Rows After An Offset](mysql/select-rows-after-an-offset.md)
|
- [Select Rows After An Offset](mysql/select-rows-after-an-offset.md)
|
||||||
- [Show Create Statement For A Table](mysql/show-create-statement-for-a-table.md)
|
- [Show Create Statement For A Table](mysql/show-create-statement-for-a-table.md)
|
||||||
- [Show Tables That Match A Pattern](mysql/show-tables-that-match-a-pattern.md)
|
- [Show Tables That Match A Pattern](mysql/show-tables-that-match-a-pattern.md)
|
||||||
|
|||||||
27
mysql/run-statements-in-a-transaction.md
Normal file
27
mysql/run-statements-in-a-transaction.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# Run Statements In A Transaction
|
||||||
|
|
||||||
|
I'm connecting to a production MySQL database to make some changes to a
|
||||||
|
specific user account. That means I am going to run an `update` statement and I
|
||||||
|
expect that statement to affect exactly *one* row in the `users` table.
|
||||||
|
|
||||||
|
If I run the `update` statement in a transaction, then I can verify all looks
|
||||||
|
good before committing those changes. And importantly, I can rollback the
|
||||||
|
changes if anything looks off.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> start transaction;
|
||||||
|
|
||||||
|
> update users set roles = 'admin' where id = '1234';
|
||||||
|
Query ok, 1 row affected
|
||||||
|
|
||||||
|
> select * from users where id = '1234';
|
||||||
|
-- check that all looks good
|
||||||
|
|
||||||
|
> commit;
|
||||||
|
```
|
||||||
|
|
||||||
|
In the above case, all looked good, so I ran `commit`. If more rows than I
|
||||||
|
expected were affected or the changed record didn't look right, I could instead
|
||||||
|
`rollback`. None of those changes would make it into live production data.
|
||||||
|
|
||||||
|
[source](https://dev.mysql.com/doc/refman/8.0/en/commit.html)
|
||||||
Reference in New Issue
Block a user