Solved

Trigger to prevent update in Mysql

Posted on 2010-08-22
5
576 Views
Last Modified: 2012-05-10
Dear All,

Please help me providing trigger in mysql that prevent update of some particular record in a table.

For example :
Name             Hobby
-------------      -----------
Willy                Swimming
Willy                Singing
Susan             Reading
Marie               Reading
Susan             Swimming
Michael            Soccer

As you can see Susan has 2 hobbies. I want to prevent update if there is a query that update all record with Susan as name.

0
Comment
Question by:softbless
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33498653
in MYSQL, triggers are "for each row":
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html

so, you cannot see inside the trigger if there are several rows being updated.

what you should do is to create a unique index or unique constraint on username + hobbyname, so an update cannot udpate all the rows from 1 username to the same hobby ...
0
 
LVL 2

Accepted Solution

by:
Sandeepratan earned 500 total points
ID: 33498716
hi
if you want tp prevent the update only in case of the all records being updated where name = 'susan'
here is ur query... you can change according to ur requirement if i misunderstood it ..

CREATE TRIGGER updtrigger BEFORE UPDATE ON Employee
     FOR EACH ROW
     BEGIN
      
       DECLARE num_rows INTEGER;
       DECLARE tot_rows INTEGER;
      
       Select Count(*) into num_rows from Employee where name = NEW.name;
        Select Count(*) into tot_rows from Employee;
             
     IF num_rows = tot_rows THEN
     SET OLD.hobby=OLD.hobby;
     ELSE
     OLD.hobby=NEW.hobby;
     END IF;
     END
0
 

Author Comment

by:softbless
ID: 33689953
ok thanks Sandeepratan
I'll try
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34328540
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now