Error Creating Trigger Unknown System Variable - mysql

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.

Related

MySQL trigger error doesn't work because it needs a value from another table column

I'm new to the php mysql developpement, I want to make a trigger to be launched after I insert a row in the evolution table. The trigger must take a value (prixMisDaccord) from another table (inscription) and reduce it value from the evolution column prixAPaye.
Here is what I tried and what I found on Stack Overflow:
DELIMITER $$
CREATE TRIGGER trg_rap
BEFORE INSERT ON evolution FOR EACH ROW
BEGIN
DECLARE pmd float;
-- Check BookingRequest table
SELECT prixMisDaccord
INTO #pmd
FROM inscription
WHERE inscription.idETD= 1;
SET NEW.resteAPaye = #pmd-NEW.prixPaye
WHERE idETD = 1;
END;
$$
DELIMITER `;
'i have a probleme from this line SELECT' - Is not the error I get, I do get an error on the set statement because you cannot apply a where clause to a set..There are other problems with your code and you don't seem to know the difference between user defined variables and declared variables see - How to declare a variable in MySQL? and temporary tables..so #pmd-NEW.prixPaye is just nonsense.
If you want more help read https://stackoverflow.com/help/how-to-ask and provide table definitions,sample data and desired outcome all as text in the question.

sqlite trigger insert convert to json

I'm using SQLite 3.37.2 through Python 3.10.5, and editing the database with DBeaver - maybe it's not always at its best when reporting database errors.
Anyway, I want to convert a text (JSON) in SQLite proper JSON format upon insertion, by calling its json() function.
I thought about adding a simple trigger to handle that, but I can't figure out what's wrong in my syntax:
CREATE TRIGGER t1_before_insert AFTER INSERT ON t1 FOR EACH ROW
BEGIN
SET NEW.json:=json(NEW.json);
END
;
This is the latest version I attempted, I always get an error like
SQL error or missing database (near "SET": syntax error)
I tried:
SET NEW.json:=json(NEW.json)
SET NEW.json=json(NEW.json)
SELECT NEW.json:=json(NEW.json)
using an AFTER INSERT trigger
but none worked.
You can't change the value of a column of the new row like that.
You must update the table in an AFTER INSERT trigger:
CREATE TRIGGER t1_after_insert AFTER INSERT ON t1 FOR EACH ROW
BEGIN
UPDATE t1
SET json = json(NEW.json)
WHERE t1.id = NEW.id;
END;
Change id to the table's primary key.
See the demo.

MySQL trigger does not work

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;

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 ;

MySQL 5.7 Create Trigger Syntax Error?

I've been trying to create a simple BEFORE INSERT trigger on a database table (MySQL v 5.7 ) but I keep receiving a vague "#1064 ... syntax error" message which doesn't help resolve the issue.
Here's the SQL:
CREATE OR REPLACE TRIGGER `CREATE_QUIZ_TRIG` BEFORE INSERT ON `quiz`
FOR EACH ROW BEGIN
SET NEW.ACTIVE = UPPER(NEW.ACTIVE);
SET NEW.CREATED = NOW();
END
/
All I'm trying to do is enforce a column to uppercase and then insert the current date & time into a timestamp column. I've been following the documentation from:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and realise that for multi-statement expression I have to redefine the delimiter at the beginning of the trigger's creation but the same '#1064' error occurs.
This is made even more confusing because when I use phpmyadmin's interface for creating the same trigger it works fine - but won't when I export the generated SQL and try to create the trigger using that!?
Thanks for any help
I didn't realise that, by default, phpmyadmin adds a ; delimiter which was breaking the ; used to end a statement within the BEGIN END block.