Solved

Trigger to prevent update in Mysql

Posted on 2010-08-22
5
559 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 68

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

'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 …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

21 Experts available now in Live!

Get 1:1 Help Now