Error 1064 when creating Trigger - mysql

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

Related

Before trigger in mysql database

I am trying to create a trigger where if the no tickets are less than the no of adults then value 1 will be inserted in trig table.But, I am getting an error.The code is:
CREATE TRIGGER ch
BEFORE INSERT
ON packagebooking
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
Begin atomic
insert into trig values(1)
MySQL said: Documentation
#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 'IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Pa' at line 4.
What changes should I do?
Your syntax for trigger creation has an error, place the IF statement inside the FOR EACH ROW as shown below:
CREATE TRIGGER ch BEFORE INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
THEN Begin atomic
insert into trig values(1)
END IF;
END;

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 ;

query to create trigger in mysql

I have a table named sample_table and its columns are col_a, col_b, col_c.
I am trying to create a trigger in MySQL 5.6 which will update col_c(as concat(col_a,col_b)) on insertion of a row. Query which I have written for this is:
create trigger trg_sample_table
after insert on sample_table
for each row
begin
set col_c=concat(col_a,col_b)
end;
While running this sql, the error I am getting is:
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 'end' at line 6.
Could any one of you please help me to create a trigger as I need?
You need to make it a before insert. Try this:
create trigger trg_sample_table before insert on sample_table for each row
begin
set new.col_c=concat(new.col_a,new.col_b);
end;
You cannot modify the row that you just inserted in an after insert trigger. If you want to modify it, it needs to be done in a before insert.

MySql on update trigger. Error of DELIMITER

I try to create trigger in MySql 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 'DELIMITER' at line 1
DELIMITER $$
CREATE TRIGGER library_update
AFTER UPDATE ON wq6vt_vehiclemanager_vehicles
FOR EACH ROW
BEGIN
INSERT IGNORE INTO wq6vt_vehiclemanager_library (maker, model) VALUES(NEW.maker, NEW.vmodel);
INSERT INTO wq6vt_vehiclemanager_library_data (co2_class)
SELECT co2_class FROM wq6vt_vehiclemanager_vehicles
WHERE maker = NEW.maker AND vmodel = NEW.vmodel;
END $$
DELIMITER;
The first query in the trigger do not lead to errors, but second one does. There is some problem with SELECT inside of INSERT ...I think so
there should be a space between the keyword and the symbol,
DELIMITER ;
-- ^ space in between here

MySQL trigger: ERROR 1064 (42000): You have an error in your SQL syntax

I'm trying to create a trigger that monitor changes on a table and then insert those change to another table as follows
CREATE TRIGGER userChangePasswd
BEFORE UPDATE ON originalTable
FOR EACH ROW
BEGIN
INSERT INTO logs (email,clear,name ) SELECT email,clear,name FROM originalTable
END
mysql keeps showing the following 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 'INSERT INTO logs (email,clear,name ) SELECT email,clear,name FROM ' originalTable at line 5
the following statement works very file with a where clause criteria
INSERT INTO logs (email,clear,name ) SELECT email,clear,name FROM originalTable
what's wrong with the insertion statement within the trigger
CREATE TRIGGER `orgTbl_before_upd_tr` BEFORE UPDATE ON `orgTbl`
FOR EACH ROW
BEGIN
INSERT INTO newTbl (field1, field2) VALUES (old.field1, old.field2);
END;