I am trying to add a trigger, when a table is updated (except for two columns in that table), it updates another table. The query I used is given below:
CREATE TRIGGER my_trigger
BEFORE UPDATE ON town_1a2b3c4d
BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN
UPDATE town_details SET game_index = game_index + 1 WHERE town_id = '1a2b3c4d';
END;
END;
But it gives me this error:
MySQL said
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN...' at line 3
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?
I am using MYSQL 5.6 and phpMyAdmin 4.1.14
I am trying to create a Trigger that will copy data from one db to another but get the following 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 'new.airplane_id,new.user_id,new.icao24,new.created_at,new.timestamp,new.altitude' at line 7
Any pointers would be gratefully received
My code is:
CREATE TRIGGER airfields_insert
BEFORE INSERT
ON realtime
FOR EACH ROW
BEGIN
if (new.squawk between 6160 and 6167 or new.squawk between 6171 and 6177) and new.altitude <= 4000 then
insert into airfields (airplane_id,user_id,icao24,created_at,timestamp,altitude,flight_number,latitude,longitude,heading,squawk,horizontal_speed,vertical_speed,Interested) (new.airplane_id,new.user_id,new.icao24,new.created_at,new.timestamp,new.altitude,new.flight_number,new.latitude,new.longitude,new.heading,new.squawk,new.horizontal_speed,new.vertical_speed,new.Interested);
end if;
You have Syntax error in insert query Your QUERY should be like that
insert into airfields (airplane_id,user_id,icao24,.......)
values (new.airplane_id,new.user_id,new.icao24,........);
or simply
insert into airfields values (new.airplane_id,new.user_id,new.icao24,........);
The form does not specify the column names where the data will be inserted, only their values:
You are missing the VALUES keyword in your INSERT statement
Your current INSERT query
insert into airfields (airplane_id,user_id,icao24,.......)
(new.airplane_id,new.user_id,new.icao24,........);
end if;
It should be
insert into airfields (airplane_id,user_id,icao24,.......)
values (new.airplane_id,new.user_id,new.icao24,........);
end if;
Also, you are missing the END keyword after END IF
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 ;
I tried writing this small trigger in MySQL,
CREATE TRIGGER `leg` BEFORE INSERT ON `bckoff`
FOR EACH ROW BEGIN
INSERT INTO `bckoff` SET `step`=1;
END;
after which I get this error.. I'm a newbie to MySQL.. so please help me out here..
#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 3
Even after you fix this error - you'll get another one: you cannot modify the table that your trigger was created at.
Btw, this is how you should create this trigger:
delimiter |
CREATE TRIGGER `leg` BEFORE INSERT ON `bckoff`
FOR EACH ROW BEGIN
INSERT INTO `bckoff` SET `step`=1;
END;
|
delimiter ;