How to Create a Trigger MySQL 5.6 - mysql

we are having a problem with the created_at on a WordPress system. Somehow some users get that field updated "randomly" to an invalid value, so it get's set as '00000000' datetime. We haven't found the cause on the WP code, and we are now analizing the few plugins the project has. It has a large Code and user base, so we thought it might be faster to use a MySQL trigger to catch this "random" updates.
The thing is we somehow keep on hitting a syntax error on or Trigger code, and lame SQLer's as we are, we need help trying to figure out what it could be.
What we are tying to accomplish is:
Detect when the user table gets updated
Check if the created_at row has been modified
Insert the old data into a new table we created just for this purpose (user_registration_changed_records).
We decided to check for each row just in case there is some weird behaviour going on.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at)
END IF;
END;$$
DELIMITER ;
Thanks in advance for any suggestions!
Syntax Error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.I' at line 6

You have a couple of syntax errors:
missing then keyword before the insert (this is the one the error message relates to)
missing ; after the insert's row
extra ; after the end
At least these are the ones I can see.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at THEN
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at);
END IF;
END$$
DELIMITER ;
It is worthwile using mysql manual on compound statement syntax because all these can be found there.

Related

Prefix of the Year to a column and achieve it though trigger and auto increment

I would like to have the prefix of the Year to a column and achieve it though trigger and auto increment. My trigger is as follows:
CREATE TRIGGER Trg_table_insert
BEFORE INSERT ON Table
FOR EACH ROW
BEGIN
INSERT INTO T_Seq VALUES (NULL);
SET NEW.id = CONCAT(DATE_FORMAT(NOW(),'%y%m'), LPAD(LAST_INSERT_ID(), 4, '0'));
END
But I am receiving the following error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
I tried gone through many sites but the syntax is same and could not identify the place, where I did the mistake. Please have a look and let me know, if you found one.
Many Thanks for your time in advance.
Regards,
Chaitanya
It is absolutely delimiter issue. I had executed the same in CLI and it went fine.
Step 1: delimiter //
Step 2: CREATE TRIGGER Trg_table_insert
BEFORE INSERT ON Table
FOR EACH ROW
BEGIN
INSERT INTO T_Seq VALUES (NULL);
SET NEW.id = CONCAT(DATE_FORMAT(NOW(),'%y%m'), LPAD(LAST_INSERT_ID(), 4, '0'));
END; //
Step 3: delimiter ;

Mysql trigger to insert a new row into another table after an update

I'm trying to write a trigger that will warn us when a job is completed in an unusually quick way (less than 70% estimated time) so that somebody can go check if there has been an error.
There's a job table that looks like this:
id
machine_id
estimated_time
hour_of_completion
state (this is 0 for unfinished jobs and 1 for finished ones)
A job has many subjobs so there's also a subjob table that looks like this:
id
job_id (references job.job_id)
subjob_time (this is in seconds)
And the warning table where we want to log the warnings:
id (auto_increment)
machine_id
hour_of_completion (this is the time of the day when the job finished)
This is what I've tried so far:
DELIMITER $$
DROP TRIGGER IF EXISTS `job_too_quick` $$
CREATE TRIGGER `job_too_quick` $$
AFTER UPDATE ON `job`
BEGIN
IF OLD.state <> NEW.state AND NEW.state = 1 THEN
IF (SELECT sum(subjob_time)
FROM subjob
WHERE job_id = OLD.id)
<
(TIME_TO_SEC(NEW.estimated_time) * 0.7) THEN
INSERT INTO warnings (machine_id, hour_of_completion)
VALUES (OLD.machine_id, OLD.hour_of_completion);
END;
END IF;
END IF;
END$$
DELIMITER ;
But when I try to run it I get this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AFTER UPDATE ON `jobs`
BEGIN
IF OLD.state <> NEW.state AND NEW.s' at line 1
I feel like I must be making some silly syntax mistake but I'm going crazy trying to find out what it is. Is there something obvious that I'm missing?
In case somebody has a similar problem, the mistake was indeed quite embarrassingly silly, I was missing FOR EACH ROW as the people on the comments so helpfully pointed out.

MySQL Insert after trigger clarification

I have this code here:
CREATE TRIGGER testTrigger
AFTER INSERT ON users
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,#uid,5.0,5.0)
END;
The idea is to insert generated user id into other table alongside some other data as soon as it hits the first 'users' table but phpMyAdmin gives this error code:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
BEGIN
DECLARE #uid VARCHAR(60)
SET #uid = (SELECT userid FROM inserted)
at line 3
Can someone clarify why this trigger is bad?
I see four problems:
You have to use DELIMITERs so that your able to finish the commands with a semicolon as usual.
FOR EACH ROW is missing.
Use new.uid to access the recently inserted uid.
I'd also suggest using procedure variables instead of session-specific user-defined #variables, the latter ones being loosely typed and not declared as you've done.
But you don't even have to declare a variable. If you don't use phpMyAdmin:
DELIMITER //
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END//
DELIMITER ;
Check this answer about delimiter and the MySQL 5.7 docs on triggers and this answer about variables.
Edit, I overread you're using phpMyAdmin:
I don't use phpMyAdmin. But you can (stolen from here)
In phpMyAdmin, select the database that you want to work with.
Go to the SQL tab at the top of the page.
In the "Run SQL query/queries on database" form, change the Delimiter to $$. (Located in a small box at the bottom of the form)
Enter your SQL trigger into the main dialog box on the form. The correct syntax is as follows:
CREATE TRIGGER testTrigger
AFTER INSERT ON users FOR EACH ROW
BEGIN
INSERT INTO user_locations (id,uid,lat,lng) VALUES (0,new.uid,5.0,5.0);
END;$$
Hit "GO" with Super privilege.

Using a trigger on a table after any modification to copy affected row into a new table

I would like to create a trigger to this example table when I insert, update or delete any row into the table. I have never worked with triggers before so this will be my first one.
I have this code so far:
DROP TRIGGER auditemployees
CREATE TRIGGER auditemployees AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_trigger select * from employees where emp_no = NEW.emp_no;
END;
The problem now is:
I would like to copy the affected row into another table in this case employees_trigger, but in order to copy the whole row and to pass the values all I can think of is using variables for each field which in my opinion sounds inefficient since if I had 50 fields I will have to use 50 variables and then insert all those values into the new table.
Is there any better and efficient way to do this?
Also I get the error:
> ERROR 1064 (42000): You have an error in your SQL syntax; check the
> manual that corresponds to your MySQL server version for the right
> syntax to use near 'CREATE TRIGGER auditemployees AFTER INSERT ON
> employees
Every statement needs to be terminated -- there is no ; at the end of the DROP statement.
You are missing a DELIMITER statement, and you are not using a different delimiter for the CREATE.
What's wrong with OLD.* for the 50 variables?

Trigger on MySQL

I'm trying to create a trigger in MySQL using phpMyAdmin, but I'm getting and error and I can't detect the mistake. The main idea its to create a row in the tables stuff and config when a new user is insered.
The code:
CREATE TRIGGER create_stuff_and_config
AFTER INSERT ON user
FOR EACH ROW
BEGIN
insert into stuff(user_id) values (NEW.user_id);
insert into config(user_id) values(NEW.user_id);
END;
The error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
I've checked the MySQL doc but I see like I'm doing it well...
I've tried too: INSERT INTO stuff SET user_id = NEW.user_id;
Thank you!
Okay, the solution is to add delimiters...
DELIMITER //
CREATE TRIGGER create_stuff_and_config
AFTER INSERT ON user
FOR EACH ROW
BEGIN
insert into stuff(user_id) values (NEW.user_id);
insert into config(user_id) values(NEW.user_id);
END; //
DELIMITER ;