MySQL Trigger with Multiple IF Statements - mysql

I am new to this and cant seem to find a situation similar to mine and Im sure the answer is simple.
I have two columns, one is a predefined licenseplate ("licenseplate") and one that will be a user inputted licenseplate ("enterlicenseplate").
I made a third column which will be the sort of error check column and will have its values inputted by a trigger. Basically if the entered license plate is the same as the predefined licenseplate, set that third column to 0, if the entered license plate is null(not inputted anything yet( set that to 1, and if theres values in both and they dont match, set it 2. But i keep getting script errors. Not sure if its my syntax or im going about this all wrong.
I appreciate any help!
CREATE TRIGGER MatchPlateOnUPDATE
BEFORE UPDATE ON wp_wpdatable_3
FOR EACH ROW
BEGIN
IF (NEW.enterlicenseplate is NULL) THEN
UPDATE wp_wpdatatable_3
SET MatchingPlates = 0;
ELSEIF (NEW.enterlicensplate = New.licenseplate) THEN
UPDATE wp_Wpdatatable_3
SET MatchingPlates = 1;
ELSE UPDATE wp_Wpdatatable_3
SET MatchingPlates = 2;
END

Your problem with the if statement is that you are missing closing end if;.
But there is more: a trigger cannot action the table it was fired upon - and even if it could, then your query would basically update all rows in the table, since your updates have no where clause.
Here, you just need to modify the value of column matchingPlates in the record that is about to be inserted:
delimiter //
create trigger matchplateonupdate
before update on wp_wpdatable_3
for each row
begin
if new.enterlicenseplate is null then
set new.matchingplates = 0;
elseif new.enterlicensplate = new.licenseplate then
set new.matchingplates = 1;
else
set new.matchingplates = 2;
end if;
end;
//
delimiter ;

Related

Triggers still working even put the wrong input

My problem is, even i put the wrong alphabet other than 'F' or 'M', the trigger still updated. Also, even i put the 'F' or 'M' alphabet in lowercase, the trigger still function. how i want to make the trigger not function if the input is not correct? such as putting the wrong alphabet with lowercase.
here is my code :
CREATE TRIGGER trgAllowance -- trigger name
BEFORE UPDATE
ON tab_employee FOR EACH ROW
BEGIN
IF new.EmpGender = 'F' THEN
SET new.SpecialAllowance = (new.Salary*0.2); /* Count the amount of Salary times by specific amount */
ELSEIF new.EmpGender = 'M' THEN
SET new.SpecialAllowance = (new.Salary*0.05);
END IF;
END

MySQL: Trigger Before Update Not Working

I'm trying to get a trigger to run before an update is processed on a per record level of a table.
What I'm trying to do is when an update occurs on a record for the qst_title column, if the column qst_perma_title is NULL then the qst_perma_title column is updated with the value in the qst_title column and the qst_title column is updated as per the original update query.
I'm using the code below but no errors are shown and the column qst_perma_title isn't updated.
The current values are:
qst_title = 'Old Title'
qst_perma_title = NULL
query
UPDATE TD_QUESTION SET qst_title = 'New Title Value' WHERE qst_id = 1;
trigger
DELIMITER //
CREATE TRIGGER TRG_QST_UPD
BEFORE UPDATE ON TD_QUESTION
FOR EACH ROW
BEGIN
IF OLD.`qst_perma_title` IS NULL THEN
SET NEW.`qst_perma_title` = OLD.`qst_title`;
END IF;
END//
DELIMITER ;
CREATE TRIGGER TRG_QST_UPD
BEFORE UPDATE ON TD_QUESTION
FOR EACH ROW
IF OLD.qst_perma_title IS NULL THEN
SET NEW.qst_perma_title = OLD.qst_title;
END IF;
First run this query and then try updating. I checked, it is working fine for me.

How can I make SQL not execute the query if the field goes negative?

I have something like this:
UPDATE users SET credits=credits-1000 WHERE id=1
How do I make this fail if credits goes negative? I want to avoid having to query the database twice just to check for that. I need it to FAIL/return false if it goes negative, not set the credits to zero.
What about adding the condition ?
UPDATE users SET credits=credits-1000 WHERE id=1 AND credits > 1000
I think you have to use stored procedure for this purpose. For example
CREATE procedure UpdateUserCredit
#user_id INT,
#credits INT,
#op_status BIT OUTPUT
AS
BEGIN
SET #op_status = 0
UPDATE Users
SET credits = credits - #credits
WHERE id = #user_id
AND credits - #credits > 0
IF ROW_COUNT() = 0
RETURN
ELSE
SET #op_status = 1
END
This stored procedure will return 0 in variable #op_status if current credits + #credits for #user_id would be less zero.
DELIMITER $$
CREATE TRIGGER users_bu BEFORE UPDATE ON users FOR EACH ROW
BEGIN
IF NEW.credits != OLD.credits AND NEW.credits < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'negative balances are not supported';
END IF;
END $$
DELIMITER ;
Before the update is written to a row in the table, the new (replacement) value for credits is checked for negative values if that value has been changed by this update. If it changed, and is now negative, deny the update by throwing an exception. Requires MySQL Server 5.5 or later as written, but can be made to work with 5.1 or 5.0 with some modifications.
Note that FOR EACH ROW doesn't mean the whole table, it means that each row affected by the update is evaluated individually by this code, which will of course typically be just one row.

Mysql issue update command in before update trigger

Looking for assistance crafting a mysql trigger. My current code does not work as intended. What I would like to do is if field A in table A is modified, copy field A to field B in table A.
Current code looks like this:
BEGIN
IF new.set_id=301 THEN
UPDATE lighting_io_settings SET slider1_val=new.val WHERE set_id=402;
END IF;
END
Obviously it fails because an update is calling an update.
On before update you can check the old tuple value on variable "OLD" and check the new tuple values on "NEW" variable.
BEGIN
IF NEW.columnA != OLD.columnA THEN
//do whatever you want here like
NEW.columnB = NEW.columnA;
//can call update again, just don't change the columnA
update tableA set columnB = NEW.columnA where id = 402;
END IF;
END

MySQL nested Conditional in Update Trigger

I am trying to figure out a conditional trigger for an update to a MySQL table.
The issue is that the update trigger fires off even when the data being entered is the same as the data that was already there. So if I have a column with a row:value set up like this plus_votes:2 if I update with UPDATE votes SET plus_votes = 2; even though nothing has really changed the UPDATE trigger still gets fired off. So to prevent all of this I want to add a conditional clause to my trigger like this
BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
UPDATE my_other_table
SET plus_votes =
CASE NEW.vote_value WHEN '1'
THEN plus_votes + 1
ELSE plus_votes
END,
minus_votes =
CASE NEW.vote_value WHEN '1'
THEN minus_votes
ELSE minus_votes +1
END
WHERE my_other_table.id=NEW.votes_join_id;
END
END
the second half of the issue is that I have CASE condition inside the IF statement.
Can someone help on this issiu and show how to do nested conditionals inside a TRIGGER?
much appreciated
although the syntax you have posted is completely wrong, I have figured out what you want try this ( not tested )
DROP TRIGGER IF EXISTS upd_vote;
DELIMITER $$
CREATE TRIGGER upd_vote AFTER UPDATE ON `my_other_table`
FOR EACH ROW BEGIN
IF NEW.vote_value <> OLD.vote_value THEN
SET
NEW.plus_votes = IF(NEW.vote_value=1,OLD.plus_votes+1,OLD.plus_votes),
NEW.minus_votes = IF(NEW.vote_value=1,OLD.minus_votes,OLD.minus_votes +1 )
END IF;
END$$
DELIMITER ;