From 5412e2e8b1ddd64a068929de5e35aa2e28ee3fcc Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 1 Mar 2024 17:20:10 -0600 Subject: [PATCH] Add Connect To A Database In Safe Update Mode as a MySQL TIL --- README.md | 3 +- ...nnect-to-a-database-in-safe-update-mode.md | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 mysql/connect-to-a-database-in-safe-update-mode.md diff --git a/README.md b/README.md index 10e3914..5af0f7e 100644 --- a/README.md +++ b/README.md @@ -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). -_1385 TILs and counting..._ +_1386 TILs and counting..._ --- @@ -579,6 +579,7 @@ _1385 TILs and counting..._ ### MySQL - [Change Existing Column To Not Null](mysql/change-existing-column-to-not-null.md) +- [Connect To A Database In Safe Update Mode](mysql/connect-to-a-database-in-safe-update-mode.md) - [Default Username And Password For New Instance](mysql/default-username-and-password-for-new-instance.md) - [Display Output In A Vertical Format](mysql/display-output-in-a-vertical-format.md) - [Doing Date Math](mysql/doing-date-math.md) diff --git a/mysql/connect-to-a-database-in-safe-update-mode.md b/mysql/connect-to-a-database-in-safe-update-mode.md new file mode 100644 index 0000000..4f9f0fc --- /dev/null +++ b/mysql/connect-to-a-database-in-safe-update-mode.md @@ -0,0 +1,31 @@ +# Connect To A Database In Safe Update Mode + +The MySQL client has a _Safe-Updates Mode_ that you can use when connecting to +a database. When this mode is active, the client will interrupt `update` and +`delete` commands that don't specify a `where` clause that filters by a _key_ +value. That, or you need to explicitly `limit` the number of rows impacted by +the query. + +To start a connection in this mode, you can use either the `--safe-updates` +flag or the cheekier `--i-am-a-dummy` flag. + +```bash +$ mysql --i-am-a-dummy -h ::1 -P 3309 -u root -D my-database +``` + +Then if you try to do an unrestricted `update` or `delete`, you'll see the +following message: + +```sql +mysql> update users set email = 'oops@email.com'; +ERROR 1175 (HY000): You are using safe update mode and you tried to update +a table without a WHERE that uses a KEY column. +``` + +This can also be set within the connection like so: + +```sql +mysql> set sql_safe_updates=1; +``` + +[source](https://dev.mysql.com/doc/refman/8.0/en/mysql-tips.html#safe-updates)