MySQL · MariaDB · GUI · Safe Lock

Preview SQL before a MySQL GUI write

pimvdmolen.nl · · ~6 min read

A results grid writes to production as soon as you confirm a save. The statement behind that click is easy to skip: a missing WHERE, the wrong primary key, or an INSERT aimed at the live schema. MySQL can refuse some of those writes with sql_safe_updates. A GUI that previews the SQL catches a different set, including values the server would accept without complaint.

Two different guards

sql_safe_updates is a session flag on the server. When it is on, an UPDATE or DELETE must use a key column in the WHERE clause, or it must include LIMIT. Otherwise MySQL returns ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. INSERT is not part of that rule. An UPDATE that sets status where id = 18442 still runs if 18442 is simply the wrong order.

MySQL Workbench calls that flag Safe Updates in the SQL Editor preferences. It refuses unbounded UPDATE and DELETE. It does not print the statement.

A preview dialog does a different job. It shows the INSERT, UPDATE, or DELETE the client built and waits for you to accept or cancel. A statement can be valid SQL and still be the wrong change.

Guard Missing WHERE on UPDATE or DELETE Shows the SQL INSERT Wrong primary key
sql_safe_updates, Workbench Safe Updates Error 1175, unless LIMIT is present No Allowed Allowed
A delete prompt with no SQL Depends on the client Usually no Usually no Allowed
Preview before the grid write You can cancel after reading it Yes Yes, when the grid builds the INSERT You see the WHERE value

LIMIT is not a substitute for reading the statement. UPDATE orders SET status = 'paid' LIMIT 50 satisfies safe-update mode and still changes fifty rows.

Turn Safe Lock on for the live server

Color-label the production connection in the new-connection form so the sidebar entry does not look like local MySQL on 127.0.0.1 port 3306. Then use a client that prints generated SQL before a grid write lands.

TabulaSQL is one free desktop option for MySQL and MariaDB. The lock icon in the top-right corner turns on Safe Lock. While that lock is on, saves, inserts, deletes, and row duplicates show the SQL and wait for confirmation. Deletes still ask when the lock is off. Leave the lock off for everyday local work, and leave it on for a live host. The app is at https://tabulasql.com/download.

  1. Open the production connection and check the host. A Docker published port or a Laravel Herd socket on your laptop is not the VPS.
  2. Click the lock icon in the top-right so Safe Lock is on.
  3. Open the table, or run a SELECT that includes the primary key, and filter down to the one row you intend to change.
  4. Double-click the cell. The pending value stays highlighted until you save or cancel. A single click only selects the row.
  5. Save. Read the statement in the confirmation. Check the database name, the column list, and the WHERE clause.
  6. Confirm only when the WHERE matches one primary key. Cancel when there is no WHERE, a long IN list, or the wrong schema.
  7. For a new row, click + Row, fill the NOT NULL columns, and read the INSERT before you confirm.
  8. If you only needed to inspect the row, cancel. Highlighted cells are not written until you accept the SQL.

Right-click insert, duplicate, and delete use that same preview while Safe Lock is on. A duplicate builds an INSERT, and auto-increment can create a second live row instead of failing on the primary key.

Editor SQL is not previewed for you

Safe Lock applies to grid saves, inserts, deletes, and duplicates. It does not wrap a script you run from the editor. Ctrl/Cmd+Enter runs the whole script. Ctrl/Cmd+Shift+Enter runs the selection. If you paste an UPDATE, you are the preview.

Put a hand-written change in a transaction when the account has that privilege:

START TRANSACTION;
UPDATE shop.orders
SET status = 'paid'
WHERE id = 18442;
SELECT ROW_COUNT();

Then ROLLBACK or COMMIT. ROW_COUNT() should be 1. Zero matched nothing. More than 1 should be rolled back. Autocommit is on by default, so an UPDATE outside a transaction is already durable when the server returns OK.

Set the session flag beside that habit:

SET SESSION sql_safe_updates = 1;

The flag will not display the SQL, and it will not stop WHERE id = 18442 when that id is the wrong row. Read the text yourself. Grid editing and Safe Lock for this client are documented at /docs.

Read the preview like a one-line diff

A one-cell edit should look close to this:

UPDATE `shop`.`orders` SET `status` = 'paid' WHERE `id` = 18442;

Take the parts in order. shop must be the database you opened. orders must be the table in the tab. The SET list should contain only the column you double-clicked. The literal must match the cell: the string paid is not the number 1, and it is not an unquoted identifier. The WHERE should name the primary key, not a status or email shared by many rows.

Cancel when the statement is an UPDATE with no WHERE, an UPDATE filtered only on status = 'pending', or an INSERT that supplies id 18442 when you meant to change the existing row. The first rewrites the table. The second can touch every open order. The third creates a row. A preview only helps if you cancel those.

NULL is not an empty string. Setting a cell to NULL produces SET col = NULL. A NOT NULL column errors. A nullable column stores NULL, which application code often treats as missing. The right-click menu keeps NULL, empty string, and column default as separate actions.

After the server says OK

Confirmation is not proof the business change is right. Run the same SELECT you used before the edit and read the cell back. If the account cannot read the general log, that SELECT is the audit you actually have.

Row previews do not make schema changes safe. An ALTER that adds an index can still lock writes on a busy table, so read that statement in /articles/add-mysql-index-gui-without-lock before you run it. JSON cells add invalid-document errors on top of the WHERE check, covered in /articles/edit-mysql-json-column-gui.

Leave Safe Lock on until you disconnect from production. Turn it off when the active connection is local again, so the confirmation does not become a click you stop reading.

Frequently asked questions

No. sql_safe_updates returns error 1175 when an UPDATE or DELETE has no key column in the WHERE and no LIMIT. It does not show the statement, and it does not apply to INSERT.

In TabulaSQL, the lock icon in the top-right makes grid saves, inserts, deletes, and row duplicates show the SQL and wait. Deletes still confirm when the lock is off. Scripts you run with Ctrl or Cmd+Enter are not previewed.

Turn it on for a production connection and off for everyday local work. A confirmation you click through on localhost trains you to skip the same dialog on the live server.