query to create trigger in mysql - 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.

Related

Error adding update trigger to select columns in a table

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

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?

Error 1064 when creating Trigger

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

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 ;

MySQL trigger results in error #1064

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 ;