March 22, 2011
Can you lock updates which will change too many rows in a database?
Question by Abe Petrillo
Is there a way of implementing a rule in mysql which will block updates effecting over 10 rows?
For instance, lets say i have tbl1 with 50,000 rows and I do
update tbl1 set tb1.name = 'bob'
Then quickly follow on with a “OH NO! I FORGOT THE WHERE!?!?”.
The reason I ask is because a junior developer made this mistake and now its repair time :(.
Answer by nick rulez
You can prevent update (and delete) queries without where clause with this query
set sql_safe_updates=1;
Take a look at this link
http://dev.mysql.com/doc/refman/5.0/en/mysql-command-options.html#option_mysql_safe-updates
i-am-a-dummy is the correct switch for your colleague. 🙂
Answer by Starx
Use LIMIT
Example
update tbl1 set tb1.name = 'bob' LIMIT 0,10
This will only update the first 10 rows;