Testing Queries Safely Print

  • databasesphpmyadmin, databases, database, performance, guide, howto, solution, zillionkinghost
  • 0

Working out what a query does before it does it.

THE METHODS

Run it as a SELECT first, with the same WHERE clause Add LIMIT while testing Use a transaction and roll back Work on a copy of the database

THE SELECT FIRST RULE

Whatever you intend to UPDATE or DELETE, SELECT it first with the identical condition.

The rows returned are exactly the rows you will affect. Look at them.

COUNTING

SELECT COUNT(*) with the same condition tells you the scale in one number.

If it returns four thousand and you expected three, stop.

USING LIMIT

Adding LIMIT 1 to an UPDATE restricts the damage while you confirm the syntax is right.

Then remove it once you have verified the effect.

WORKING ON A COPY

For anything substantial, clone the database and test there.

Slower, and it eliminates the risk entirely.

TRANSACTIONS

Start a transaction, run the change, check with a SELECT, then commit or roll back.

Available on InnoDB, and it gives you the undo SQL otherwise lacks.

THE HABIT WORTH BUILDING

Never run a write query you have not first run as a read.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot