MySQL 5.0 can not drop and create trigger - mysql

I'm using MySQL 5.0. I try to drop and recreate the trigger.
When I drop the trigger it says:
mysql> drop trigger ads_delete;
ERROR 1360 (HY000): Trigger does not exist
Then I try to create the trigger with the same name. It says:
ERROR 1359 (HY000): Trigger already exists
Here is my trigger:
delimiter //
create TRIGGER ads_delete
BEFORE INSERT ON ads
FOR EACH ROW
BEGIN
update params set ads_count=ads_count-1, freq_weight=freq_weight-NEW.freq;
END//

You need to drop the trigger like this:
USE db5;
DROP TRIGGER ads_delete;
Your trigger is in the db5 schema.
EDIT:
As OP commented that the problem is
BEFORE INSERT ON ads
ie, he is trying to create two triggers with the same instruction. (Probably a copy paste issue)

I found the error. The reason was my inadvertence. I copy/pasted my INSERT trigger, when changed the trigger name from ads_insert into ads_delete, I forgot to change BEFORE INSERT ON ads into BEFORE DELETE ON ads
Thanks to everyone.

Related

How can I keep the DDL for my Trigger in Harmony with my Trigger code?

I'm trying to create a Trigger in Database Workbench (Lite, for MySQL) with this:
delimiter //
CREATE TRIGGER employeesTableInsert AFTER INSERT ON EMPLOYEES FOR EACH ROW
SET NEW.CREATED = CURRENT_TIMESTAMP; END IF;//
When I try to create the new Trigger, though, I get this err msg:
This seems odd; I look at the DDL tab in Database Workbench, and it has this:
Why is it doubling and mangling my Trigger code there in the (read-only) DDL?
As shown at the outset, my Trigger code only contains what is shown above:
So is the problem with my code, or is it because the DDL tab is confused? Or is my code CAUSING the DDL tab to become confused, or what?
If you want to update only the value you need following.
you can't use SET without a BEGIN and END, it is not a query.
I am wondering why there is a END IF in your code
Edit i also changed your TRIGGER to BEFORE INSERT, because on there you can change the values of a NEW column (see Bill Karwin comment)
And actually you don't need a trigger see manual
DELIMITER //
CREATE TRIGGER employeesTableInsert BEFORE INSERT ON EMPLOYEES
FOR EACH ROW
BEGIN
SET NEW.CREATED = CURRENT_TIMESTAMP;
END//

Can I create a trigger in mysql schema in phpmyadming

When I ran the same query in different databases, it works successfully. But in mysql schem it gives error:
#trigger can not be created on system table
My query is:
delimiter //
CREATE TRIGGER `invite` AFTER INSERT ON `Invite_page`
FOR EACH ROW BEGIN
Insert into userpost(userid,url,title,preview,sentiment,time) values(NEW.userid,NEW.url,NEW.title,NEW.preview,NEW.sentiment,NEW.time);
Insert into urlcontent(userid,url,title,preview,sentiment,time) values(NEW.userid,NEW.url,NEW.title,NEW.preview,NEW.sentiment,NEW.time);
END
//
delimiter ;
If I can't, then how can I solve this instead?
UPDATE:
actual error:
#1465 - Triggers can not be created on system tables
Try this
DELIMITER $$
CREATE TRIGGER `invite` AFTER INSERT ON `Invite_page`
FOR EACH ROW
BEGIN
Insert into userpost(userid,url,title,preview,sentiment,time) values(NEW.userid,NEW.url,NEW.title,NEW.preview,NEW.sentiment,NEW.time);
Insert into urlcontent(userid,url,title,preview,sentiment,time) values(NEW.userid,NEW.url,NEW.title,NEW.preview,NEW.sentiment,NEW.time);
END$$
DELIMITER ;
Under the "Restrictions for triggers" section in http://dev.mysql.com/doc/refman/5.5/en/stored-program-restrictions.html you can read:
Triggers are not permitted on tables in the mysql database.
BTW, are you using the mysql schema to store data? This is (usually) a very bad idea and probably you need to rethink your setup.

MYSQL - Simple after insert trigger won't fire

Here I have the following trigger:
USE dbsspf;
DELIMITER $$
CREATE
DEFINER = 'root'#'localhost'
TRIGGER TR_ASSIGN_PAGEINDEX
AFTER INSERT
ON LIB_RECORDS
FOR EACH ROW
BEGIN
UPDATE LIB_RECORDS
SET
PAGE_INDEX = 13;
END
$$
DELIMITER ;
As you can see I'm just updating the table in my trigger. However when I insert a new record the trigger does not fire. Can you please show me what I've missed?
B.5.9: Can triggers access tables?
A trigger can access both old and new data in its own table. A trigger
can also affect other tables, but 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.
http://dev.mysql.com/doc/refman/5.5/en/faqs-triggers.html#qandaitem-B-5-1-9
In other words, a trigger on LIB_RECORDS cannot write other rows on LIB_RECORDS.

Remove trigger from information_schema DB

At school, we are learning to write triggers in SQL and I wrote one, but it's wrong. Therefore, I would like to delete it.
I tried the following:
mysql> use information_schema
Database changed
mysql> drop trigger log;
ERROR 1360 (HY000): Trigger does not exist
But as you see, I get an error because it does not exist.
When I use Sequel Pro and check that table with select * from triggers;, it shows me there IS a trigger called log. See screenshot:
I tried removing it by right-clicking and click on "delete row", but then I get the following error:
Couldn't delete rows.
MySQL said: Access denied for user 'robbert'#'localhost' to database 'information_schema'
How am I supposed to remove that trigger? It's clearly there?
The TRIGGER_SCHEMA column indicates the database in which your trigger is defined: you'll need to USE db5, not INFORMATION_SCHEMA (or else qualify the trigger name in the DROP TRIGGER statement):
USE db5;
DROP TRIGGER log;
Or:
DROP TRIGGER db5.log;
Try using like this:
Syntax:
DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name
In your case:
DROP TRIGGER [IF EXISTS] db5.log;
The error occured because you were using a wrong schema name. The schema name should be db5 not information_schema
Your trigger is in the db5 schema. You need to either use that schema prior to dropping
use db5;
drop trigger log;
or use the fully qualified trigger name in the drop statement
drop trigger db5.log
I had same problem, when I renamed table, I couldn't drop trigger.
You have to script table, then drop it (it will drop all the triggers from information_schema.triggers), finally recreate the table.

SQL - Modify or Delete a Trigger

I created triggers, and I can see them when I do SHOW TRIGGERS. How is it possible to either modify or delete them?
I use MySQL
You cannot alter a trigger, while you have to drop it and then create the new one.
The drop syntax is
DROP TRIGGER trigger_name
create or replace trigger <trigger_name>
using above statement you can modify the trigger .
delete trigger:
drop trigger trigger_name
modify trigger:
drop trigger trigger_name
create trigger trigger_name ...
There is currently no alter trigger at the moment. SO you have to drop and recreate a trigger if you want to modify it.
TRIGGER SYNTAX