Solved

Trigger to prevent update in Mysql

Posted on 2010-08-22
5
590 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 143

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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