Triggers still working even put the wrong input - mysql

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

Related

MySQL Trigger with Multiple IF Statements

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 ;

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 ;

Having issues on a validation for email on a table

What i'm trying to do is to validate the email of a certain register on a trigger. For example if a customer is registered for the first time it should create an email with both first and last name (example: user.last#email.com) however if there's already a register with the same name it should create the email the same way with the first and last name following the customer_id (example: user.last2#email.com).
The code that i have so far is the following:
delimiter $
create trigger before_customer_insert
before insert on customer
for each row
begin
declare emailVal varchar(45);
declare checkData int;
declare checkData2 int;
set checkData = (select count(first_name) from customer where first_name = new.first_name);
set checkData2 = (select count(last_name) from customer where last_name = new.last_name);
if((checkData = 1) and (checkData2 = 1)) then
set new.email = (concat(new.first_name,'.', new.last_name, '#sakilacustomer.org'));
else
set new.email = (concat(new.first_name,'.', new.last_name, new.customer_id, '#sakilacustomer.oeg'));
end if;
if(new.kind_customer is null) then
set new.kind_customer = '1';
end if;
set new.active = 1;
end $
The problem that i'm having is that when it's the first register it inserts the email but with a 0 for example "name.last0#email.com" and if i try and insert with the same information shows the same email. I tried to change the logic in the if statements but still showed the same issues.
Your logic is backward. You have:
if((checkData = 1) and (checkData2 = 1)) then
But you want:
if((checkData = 0) and (checkData2 = 0)) then
You want to use the name without a customer ID when there AREN'T any existing records in the database with those names.
Note I'm not sure your check of looking for first names and last names separately is quite what you want. Wouldn't you want to check if the whole address is found as-is?
The way it's written, if someone named Joe Bloggs tries to join, he'll be joe.bloggs1 if there's anybody else named Joe in the system, even if there is no other Joe Bloggs.