Remove trigger from information_schema DB - mysql

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.

Related

SQL: Drop Column Syntax Error

I want to drop a column from my table. I have tried executing the following syntax:
ALTER TABLE user
DROP COLUMN departmentid;
On execution, it hits me with an error stating that the statement could not be prepared. Can anyone tell me why it's doing this? I get the same error when I try renaming a column.
If you are using MySQL, Try using
DROP departmentid;
Use BackTicks 'column name' in the query.
It might not be possible to DELETE, INSERT or ALTER a table in online query builders. because once you drop tables, others cannot play around with same table. So I dont thing you have privileges to do so. If you are learning install xamp which has installed apache server and MySQL.
Hope this info helps!
I solved the problem by executing below query:
I need to remove a column and all the entries from that column to free my DB size
my initial table structure is as shown below:
CREATE TABLE words(_id,word,synonyms,favorite,history,updDate)
And I wanted the table in below form
CREATE TABLE words(_id,word,favorite,history,updDate)
So I executed below query and it removed "synonyms" column
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(_id,word,favorite,history,updDate);
INSERT INTO t1_backup SELECT _id,word,favorite,history,updDate FROM words;
DROP TABLE words;
CREATE TABLE words(_id,word,favorite,history,updDate);
INSERT INTO words SELECT _id,word,favorite,history,updDate FROM t1_backup;
DROP TABLE t1_backup
COMMIT;

MySQL 5.0 can not drop and create trigger

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.

Can you modify an existing mysql trigger after it has been created?

In mysql I can create a trigger, and then show information about it like this:
mysql> show triggers like 'fooTrigger';
This command gives output that looks an awful lot like a select statement, with a row showing the matching trigger. Is it possible to update a column on the row it shows me?
For example, one column is named Statement, and it defines what happens when the trigger is activated. Is it possible to change the Statement field for fooTrigger so the trigger does something different? Or do I need to drop and re-create the trigger?
As of MySQL 5.5, you must drop and re-create the trigger.
It is not possible to update the Statement column without dropping the trigger.
Documentation: CREATE TRIGGER DROP TRIGGER
You may also access information about triggers using the INFORMATION_SCHEMA tables:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
But, as these tables are actually views, you can't use UPDATE on them.
It's just a more convenient way to access and manipulate the information than using SHOW TRIGGERS.
Documentation: INFORMATION_SCHEMA.TRIGGERS
You may require, on Windows OS, Connector/Net Visual Studio Integration to view and edit existing database object. Limitation is that you can only edit trigger body within For each row ... loop.
Otherwise only option one has is drop and re create the trigger.
But make sure before dropping a trigger that the table associated with a trigger is locked and
and unlocked after trigger is re-created.
As #Jocelyn mentioned you can't alter the trigger. But if you're using MySql Workbench it will allow you to alter the trigger. Just right click on your table name and click Alter table option from there you can pick Trigger option and alter it. Although you cannot perform it from query.
Table Name --> Alter Table --> Triggers.

how to create trigger on any change in table

I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END

MySQL trigger to fire on alter or drop

I've encountered an interesting problem. I would like to create two triggers, trigger1 would fire on DROP TABLE and trigger2 on ALTER TABLE. I would like trigger1 and trigger2 to fire if any table is dropped or altered, but, unfortunately I don't know the syntax to do that and I couldn't find a syntax for such triggers either. In MySQL I can only write triggers which fire before/after INSERT, UPDATE, or DELETE, but now I would like to write triggers which would be applicable on database level and would fire on table events. Can somebody help me?
What you are looking for is known as "DDL Triggers". MySQL does not support them
There is this SourceForge request but I'm not sure how serious anyone is about adding it
It's not possible to use a trigger for drop statements.According to the documentation the trigger is activated on
INSERT: The trigger is activated whenever a new row is inserted into the table; for example, through INSERT, LOAD DATA, and REPLACE
statements.
UPDATE: The trigger is activated whenever a row is modified; for example, through UPDATE statements.
DELETE: The trigger is activated whenever a row is deleted from the table; for example, through DELETE and REPLACE statements.
However, DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE.
It doesn't say anything about alter table, but I would expect only DML-statements to be able to fire a trigger.
PROCEDURE `pr_new_type`( IN column_name varchar(10) )
BEGIN
SET #queryText = CONCAT('ALTER TABLE `user_rights` ADD ', column_name, ' BINARY( 9 ) NULL');
PREPARE query FROM #queryText;
EXECUTE query;
DEALLOCATE PREPARE query;
END
test this