MySQL trigger does not work - mysql

I've been trying to create a MySQL Trigger in order to set a random value to a column called confcode which is used for authentication purposes.
The issue is, the value never sets after I insert a new row and keeps being empty.
I use phpMyAdmin to create the trigger, and these are the defines:
Trigger name: confcode
Table: ebaysales
Time: BEFORE/AFTER (both don't work)
Event: INSERT
Definition: SET #confcode = FLOOR(RAND()*999999)+111111
The trigger itself gets inserted successfully, but doesn't seem to affect anything..

I guess you want:
SET NEW.confcode = FLOOR(RAND()*999999)+111111;
-- instead of
SET #confcode = FLOOR(RAND()*999999)+111111;

Related

What is difference between old value and new value in trigger in mysql

What is differen between old value and new value in trigger in mysql
If we have a variable called "amount" and for example we use it in a trigger. We will have two variants of that variable ("NEW.amount", "OLD.amount"). These variants refer to the value BEFORE (OLD) and AFTER (NEW) of the trigger. So you can use the different values.
I clarify that in an insert there is no OLD value and that in a delete there is no NEW value.
An example is the following:
mysql> CREATE TRIGGER ins_transaction BEFORE INSERT ON account
FOR EACH ROW PRECEDES ins_sum
SET
#deposits = #deposits + IF(NEW.amount>0,NEW.amount,0),
#withdrawals = #withdrawals + IF(NEW.amount<0,-NEW.amount,0);
More information: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html

SQL where syntax error with trigger

create trigger cal_retweet before insert on T
for each row begin
set NEW.retweet_change = NEW.retweet_count - retweet_count where id_str = NEW.id_str
end
SQL said there is syntax error near "where id_str = NEW.id_str"
My table looks like this. Where id_str is a unique identifier for a specific tweet. Since I am inserting 50 tweets from a single user every minute, there would be many same id_str. What I want to look at is the change of retweet_count every minute. tweeted_at is when the user tweeted, created_at is when this data is inserted into my database. I want to generate retweet_change for each new data inserted into the database compared to the same old tweet (into the column retweet_change). How should I write the trigger?
After reading some of your comments I changed my code to :
create trigger cal_retweet before update on T
for each row
begin
set NEW.retweet_change = NEW.retweet_count - OLD.retweet_count;
end;
There is still syntax error
There are several issues with this trigger.
You have some syntax errors. You need proper semicolons to delimit your statements.
You have a WHERE statement that is out of place (and actually not needed). You are acting on only a single row at a time, you don't have to match on the id_str.
In order to factor in a calculation using an existing value from the row, you need access to the OLD keyword. For that, you need a trigger that happens on UPDATE, not INSERT. On INSERT, the retweet_change is simply the same as retweet_count; you could alter your INSERT statement to fix that problem.
You may need to explicitly add a statement delimiter as per the comments below.
So all together, I think this trigger should look like:
DELIMITER //
CREATE TRIGGER cal_retweet BEFORE UPDATE ON T
FOR EACH ROW
BEGIN
SET NEW.retweet_change = NEW.retweet_count - OLD.retweet_count;
END;//
DELIMITER ;

Error Creating Trigger Unknown System Variable

I am trying to create a trigger in mysql using the following:
CREATE TRIGGER ins_daft BEFORE INSERT ON jos_ezrealty
FOR EACH ROW BEGIN
SET preschool = livingarea*10.76391041671
END;
When I do I get the following error:
Error
SQL query:
CREATE TRIGGER ins_daft BEFORE INSERT ON jos_ezrealty
FOR EACH
ROW BEGIN
SET preschool = livingarea * 10.76391041671 END
MySQL said: Documentation
#1193 - Unknown system variable 'preschool'
I am trying to have the value of one field converted to square feet by multiplying by 10.76391041671. Can anyone see what I am doing wrong?
Thank you.
Any time you want to reference the columns of a row that fired the trigger, qualify them like NEW.column_name.
Otherwise the SET command thinks you want to set a MySQL configuration variable called preschool.

Update trigger disables functionality of database

I am trying to add a trigger to my SQL DB from phpMyAdmin.
When applying the trigger:
CREATE TRIGGER `download_url` AFTER INSERT ON
`tbl_files` FOR EACH ROW UPDATE tbl_files SET
download = CONCAT('http://website/', url)
WHERE 1
When trying to upload a file, I get no results; if I remove the trigger it functions properly. I need the download column to update with the prefix [http://website/] and value [url].
Thank you!!!
You can use a considerably simpler approach - instead of having an AFTER INSERT trigger, use a BEFORE INSERT trigger and manipulate the incoming row before writing it to the table. You can use the special variable NEW to reference this new row:
CREATE TRIGGER download_url
BEFORE INSERT ON tbl_files
FOR EACH ROW
SET NEW.download = CONCAT('http://website', NEW.url);
END;

MYSQL Trigger to calculate percent

I'm trying to calculate the percentage column in my table project after an update occurs on any of the following columns fund and goal.
This is the query I made up:
CREATE TRIGGER percentcalc AFTER UPDATE ON project
FOR EACH ROW
SET percent = ( fund / goal ) *100
but I seem to get an error:
#1193 - Unknown system variable 'percent'
delimiter //
CREATE TRIGGER percentcalc AFTER UPDATE ON project
FOR EACH ROW BEGIN
SET NEW.percent = ( NEW.fund / NEW.goal ) * 100;
END
//
delimiter ;
There are two major problems:
You have to use NEW keyword to access values of columns of a row that is being updated
You can't change values of NEW variables in an AFTER trigger. If you try to do so you'll get 'Updating of NEW row is not allowed in after trigger' error. Therefore you have to use only BEFORE event for your trigger
Trigger Syntax and Examples
...Within the trigger body, the OLD and NEW keywords enable you to
access columns in the rows affected by a trigger. (OLD and NEW are not
case sensitive.)...
...In a BEFORE trigger, you can also change its value
with SET NEW.col_name = value if you have the UPDATE privilege for it.
This means you can use a trigger to modify the values to be inserted
into a new row or used to update a row. (Such a SET statement has no
effect in an AFTER trigger because the row change will have already
occurred.)...
That being said your trigger should look like
CREATE TRIGGER percentcalc
BEFORE UPDATE ON project
FOR EACH ROW
SET NEW.percent = (NEW.fund / NEW.goal) * 100;
Here is SQLFiddle demo