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;

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!