mysql error 1360, cannot drop trigger. is this a bug? - mysql

delimiter $$
CREATE TRIGGER carslibrary_trigger
AFTER insert ON carslibrary
FOR EACH ROW
BEGIN
insert into facerecord (carslibrary_ID) values (new.CarID);
END$$
delimiter;
i am trying to delete my trigger
Showing rows 0 - 0 (1 total, Query took 0.0087 sec)
SELECT trigger_name
FROM information_schema.triggers
WHERE event_object_table = 'carslibrary'
AND action_timing = 'AFTER'
AND event_manipulation = 'INSERT'
LIMIT 0 , 30
drop trigger carslibrary_trigger;
#1360 - Trigger does not exist
DROP trigger carslibrary_trigger;
my query returned a record, but it can't drop?
why i can't drop my trigger?
i also made a general query still returns 1 record
Showing rows 0 - 0 (1 total, Query took 0.0606 sec)
SELECT *
FROM TRIGGERS
LIMIT 0 , 30

What version of MySQL? You may have to include table name.
DROP TRIGGER carslibrary.carslibrary_trigger;

If anyone get this issue using MyISAM engine, check the TRN files in the data folder, in my case those were there, even though I deleted the trigger using command line.
Manually Deleting TRN files of the triggers that are throwing error 1360 will solve the issue.

if the error trigger does not exist with error code 1360 occurs,
simply execute the triggers from command line
mysql -u root -p
then mysql> execute the triggers
then check the information_schema in mysql
the triggers can be viewed there
just edit table data

Related

Insert values into a table using a trigger with values from stored procedure

I'm a newbie here. So, please feel free to correct me if my code or thinking approach can be worked upon.
I am working in MySQL Workbench and MySQL 8.0 Command Line Client. I have a database called ds_testdb2 with its tables
structured as follows:
ds_testdb2
-Tables:
asset_sensor_table
device_table
mapped_readings_table
readings_table
sensor_table
I need to execute the following SELECT statement every time a record is inserted in my readings_table (insertion into the readings_table happens approx. every 1 minute). This SELECT statement joins 4 tables and produces a mapping of the device's imei with other device attributes such as device_id, subscription_id,etc from another database named agiletest. This SELECT statement works correctly.
SELECT r.imei, r.send_time, r.latitude, r.longitude, r.temperature, r.ground_velocity,
d.sys_id AS device_id, d.model_id, d.Subscription_ID,
s.id AS sensor_id, s.type_id AS sensor_type,
a_s.asset_id, a_s.start_time, a_s.end_time
FROM
ds_testdb2.readings_table r JOIN agiletest.device d ON r.imei= d.imei
JOIN agiletest.sensor s ON d.sys_id= s.device_id
JOIN agiletest.asset_sensor a_s ON s.id= a_s.sensor_id;
So, I put this SELECT statement in a Stored Procedure. This SP works correctly as well.
DELIMITER $$
CREATE PROCEDURE `ds_testdb2`.`mapping_2`()
BEGIN
INSERT INTO `ds_testdb2`.`mapped_readings_table` -- mapped_readings-table already existed, but was empty.
(`imei`, `send_time`, `latitude`, `longitude`,`temperature`,`ground_velocity`,
`device_id`,`model_id`,`subscription_id`,
`sensor_id`,`sensor_type`,
`asset_id`,`start_time`,`end_time`) -- no 'VALUES' keyword
SELECT r.imei, r.send_time, r.latitude, r.longitude, r.temperature, r.ground_velocity,
d.sys_id AS device_id, d.model_id, d.Subscription_ID,
s.id AS sensor_id, s.type_id AS sensor_type,
a_s.asset_id, a_s.start_time, a_s.end_time
FROM
ds_testdb2.readings_table r JOIN agiletest.device d ON r.imei= d.imei
JOIN agiletest.sensor s ON d.sys_id= s.device_id
JOIN agiletest.asset_sensor a_s ON s.id= a_s.sensor_id;
END $$
I then run the Stored Procedure:
mysql> call mapping_2;
Query OK, 4 rows affected (0.02 sec)
Now, this SP needs to be executed every time a record is inserted in my readings_table. So, I created a trigger that says:
mysql> DELIMITER $$
mysql> CREATE TRIGGER initial_table_mapping_1 AFTER INSERT ON readings_table
-> FOR EACH ROW
-> BEGIN
-> call mapping_2();
-> END $$
Query OK, 0 rows affected (0.04 sec)
Now, To check the execution of the trigger, I am doing the following steps in MySQLWorkbench:
--Step 1. Insert 1 record into the readings_table
INSERT INTO readings_table -- (imei,send_time, latitude, longitude, temperature, ground_velocity)
VALUES
(354721090118251, '2019-04-07 21:26:48',43.63218, -79.51890, 108, 50);
Error Code: 1415. Not allowed to return a result set from a trigger
--Step 2. execute the trigger
-already ran it in the MySQL Command Prompt
-Response was: Query OK, 0 rows affected (0.04 sec)
Step 3. check if the mapped_readings_table was updated(current #rows= 16)
select * from mapped_readings_table;
-still stuck with the Error from Step1.
My Question is: In Step1, how can I rewrite my trigger such that- the result set from my Stored Procedure gets stored in my mapped_readings_table, everytime a record is inserted in my readings_table? And, are my steps (Step 1,2,3) to verify the execution of my trigger, correct?
A similar question is here: Update table in a trigger with values from stored procedure
Here is the solution:
My trigger above worked perfectly, and is the right trigger to do the job I had in mind. The problem was that I had created many dummy-triggers to see if my trigger was working.
So, AFTER INSERT ON readings_table, it was these old, dummy triggers that were getting executed, not the final one. The problem was solved once I deleted the old triggers and only had the right trigger in my system.

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?

How to solve Error in query (1442) MySql

I am trying to insert some records to a table and update its auto incriment id in another table;
I have a trigger
delimiter #
create trigger test AFTER INSERT ON samples_track for each row begin
update sample_details set sampletrack_id = new.sampletrack_id where sample_details.sample_id = new.sample_id; end#
delimiter ;
And I am running a query
INSERT INTO samples_track(sample_name,customer_id,contact_name,added_by,added_date,status,sample_id)
SELECT sample_name,
customer_id,
clinician_id,
999,
'2016-01-23',
1,
sample_id
FROM sample_details
WHERE sampletrack_id IS NULL;
The result is as below
Error in query (1442): Can't update table 'sample_details' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
Well, the reason of this error is that when you insert a record mysql locks the table. you can't insert/update/delete rows of the same table where you insert.. because then the trigger would called again and again.. ending up in a recursion.
To solve your problem, have a look at this thread. Also, in this forum they suggest to use a before insert trigger... Worth a try.

PhpMyAdmin freezes by creating triggers

I try to create a trigger from the SQL console of PhpMyAdmin but the navigator loads infinitely
DELIMITER |
CREATE TRIGGER before_insert_history BEFORE INSERT
ON history FOR EACH ROW
BEGIN
INSERT INTO newsfeed(user_ID, date_action, action, pg_ID, rk_ID, vote)
VALUES (NEW.user_ID, NOW(), "vote", NEW.page_ID, NEW.ranking_ID, SELECT IF(NEW.liked='1','1','-1') AS vote );
END |
DELIMITER ;
After a certain time I have this error : Fatal error: Maximum execution time of 300 seconds exceeded in C:\wamp\apps\phpmyadmin3.2.0.1\libraries\import\sql.php on line 259
Maybe it's a delimiter issue, yet I choosed ";" in the delimiter field on the bottom of console.
delimiter is a MySQL client command, not SQL. I believe you need to choose | as your delimiter from the drop-down box on PHPMyAdmin's SQL tab, then use the same code you posted here without the delimiter commands.
I just tried a simple statement within delimiter commands, and although the first delimiter command didn't seem to do much, the second one causes it to hang. Removing it solved it in my case.
CREATE TRIGGER before_insert_history BEFORE INSERT
ON history FOR EACH ROW
BEGIN
INSERT INTO newsfeed(user_ID, date_action, action, pg_ID, rk_ID, vote)
VALUES (NEW.user_ID, NOW(), "vote", NEW.page_ID, NEW.ranking_ID, SELECT IF(NEW.liked='1','1','-1') AS vote );
END |

Trigger Error , MySQL

Well, I have researched and have not found a solution to the following problem 'SQL Error (1442): Can not update table' messages' in stored function / trigger because it is already used by statement Which invoked this stored function / trigger. `
My trigger is created, only when I run the INSERT on the table of messages, this error is thrown, my trigger is
DELIMITER $$
DROP TRIGGER IF EXISTS `onMessage` ;
CREATE TRIGGER `onMessage` BEFORE INSERT ON `messages` FOR EACH ROW
BEGIN
UPDATE `users` SET `users`.`messages` = ( `users`.`messages` + 1 )
WHERE `users`.`uid` = NEW.uid ;
DELETE FROM `messages` WHERE `date` < ( NOW( ) - INTERVAL 1 MINUTE ) ;
END ;
$$
DELIMITER ;
This is a restriction in the use of triggers.
From the MySql documentations:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
Correct, for MySQL anyway
You can't write to the table associated with a trigger in that trigger. It generates all manner of inconsistencies, not least because MySQL triggers are processed row-by-row (RBAR)
In this case, I'd use a stored procedure to deal with the INSERT on messages
It is not possible to update a table for which the trigger is created in the trigger since Mysql does some locking stuff on that table. Therefore you can't insert/update/delete rows of the same table because then the trigger would called again and again, ending up in a recursion.
If you want to create a trigger on the table which will update itself (perfoming a deletion in this case), make sure you use the NEW.column_name to refer to the row after it’s updated.