mysql错误检测_检测mysql错误而不执行它

bd96500e110b49cbb3cd949968f18be7.png

Suppose I make a query "UPDATE table SET etc etc"

Then I "Execute" this query

if the query is fine, it executes

and if the query has errors, it will return mysql errors

My question is...is there a way to do the following:

if the query has errors, it will return mysql errors. However,

if the query is fine, DON'T execute and instead just say that the query is fine

In other words, is there a way to check that mysql will accept my query WITHOUT actually modifying the entries in the database in the event that there is no error?

解决方案

EXPLAIN does the trick if you're running MySQL 5.6 or greater.

explain update whatever;

If the query is ok, it shows the execution plan. Else, it returns the syntax error.

If you're running a lesser version of MySQL, I see a few options:

The recommended option: Have a test database ready that mirrors your production database at least in structure. Ideally have it populated with test data to verify the query is not only syntactically correct; but that it works as expected.

Run the query in the scope of a TRANSACTION that is immediately rolled back.

Run a version of the query that is slightly modified to match NO ROWS.

For instance:

update table set col1 = @val1 where col2 = @val2;

Becomes:

update table set col1 = @val1 where (col2 = @val2) and 1=0;

So, if you're running 5.6 or greater, the EXPLAIN trick is neat. If not, options 2 and 3 from list are also neat(ish) tricks. But, you should generally be hitting a development server with your in-development queries anyway.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值