Solved

Trigger to prevent update in Mysql

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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