Centos 7 and MariaDB - Can't create triggers - mariadb

I'm using Centos 7 and MariaDB but I have problem creating triggers:
create trigger chu after update on nagios_hoststatus for each row begin replace into events.e select new.host_object_id, now(); end;
create trigger csu after update on nagios_servicestatus for each row begin replace into events.e select new.service_object_id, now(); end;
This error is what I get: multiple triggers with the same action time and event for one table
Any solution ?

I found a solution. I have to FIRST drop trigger in order to create a new one with the same name :) Command is "DROP TRIGGER *name_of_trigger"

Indeed, get sure you've removed it before create again using
DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name
and then
CREATE TRIGGER ....
Now if you have some other trigger you have to merge code from both triggers into one, then drop existing trigger, and then create a new one.
To show the list of existing triggers use SHOW TRIGGERS.
SHOW TRIGGERS WHERE `table` = 'companies';

Related

SQLite Upsert using trigger

Hello I made a trigger for aborting not changed rows
But now I want to make a trigger for Upserting (insert Or update) rows
works with before insert that when a row exists , update that row , else insert
i want to do these using triggers
This is not possible for a table; you cannot abort the INSERT operation without raising an actual error.

SQLite create table on trigger

I want to create a new table for every row that is inserted into an existing table.
As I understand only DML operation is allowed on trigger, is it correct.
If so is there a alternative way of achieving my objective?
SQLite indeed allows only DML in a trigger body.
However, you could do a SELECT with a user-defined function that then executes another SQL command to create the table:
CREATE TRIGGER ...
...
BEGIN
SELECT my_create_table_function(NEW.name);
END;

How to write a trigger in sqlite to delete rows after 1 day?

I am trying to write an sqlite trigger query to delete rows when its created time reached one day old.
Is it possible to delete rows automatically after 1 day without user intervention. Please let me know how to do it?
Triggers need some change to execute the task.
For Trigger demo Trigger Demo
You can try this option:
You can create a local timer to track the time.And accordingly update the value in db.
Now as the update is performed your trigger will be fired & gonna delete the row.
e.g:
CREATE TRIGGER if not exists delete_row
AFTER UPDATE
ON[first]
for each row
BEGIN
delete from second name where sid = old.sid;
END;
Here trigger deletes the row from second when update is performed on the first.
Hope this could give you some idea.

Create a trigger to update time stamp on inserted or updated records?

I am trying to create an Oracle trigger that would update entry_stamp column (type=DATE) on every inserted or updated record for a certain table. Here is my script:
CREATE OR REPLACE TRIGGER mytable_entry_stamp
AFTER INSERT OR UPDATE ON mytable FOR EACH ROW
BEGIN :NEW.entry_stamp := SYSDATE; END;
I am getting this error:
ORA-04084: cannot change NEW values for this trigger type
From Oracle/PLSQL: AFTER UPDATE Trigger:
You can not update the :NEW values.
You can not update the :OLD values.
It appears that Oracle cannot update a record inside AFTER trigger, unlike MSSQL. So it is reserved for logging/audit purposes, i.e. a record can be inserted or updated in another table. After I converted this trigger to BEFORE, it worked flawlessly.

sqlite UPDATE trigger firing on INSERT statements also?

I'm working on setting up a simple SQLite database to access via Python. So far I have one basic table, and a couple of triggers - I want to have one trigger update a field column 'date_added' when a new record is added, and another one to update a column 'date_updated' when a record is later updated. Here is my SQLite syntax for the triggers:
CREATE TRIGGER add_contact AFTER INSERT ON contact_info
BEGIN
UPDATE contact_info SET date_added = DATETIME('NOW') WHERE pkid = new.pkid;
END;
CREATE TRIGGER update_contact AFTER UPDATE ON contact_info
BEGIN
UPDATE contact_info SET date_updated = DATETIME('NOW') WHERE pkid = new.pkid;
END;
The 'add_contact' trigger seems to be working fine... it fires when I add a new record via an sql INSERT command, as planned.
The problem seems to be the 'update_contact' trigger... it fires both when I update a record via an sql UPDATE command (as planned) and when I add a new record also:
i.e. when I add a new record I get this in the 'date_added' and 'date_updated' columns:
2010-07-12 05:00:06|2010-07-12 05:00:06
and when I update that record, it changes like so:
2010-07-12 05:00:06|2010-07-12 05:14:26
I guess I'm not getting why the UPDATE trigger fires on INSERT also?
TIA,
Monte
Edited to add: Any hints on how to make it work as intended?
A better way to avoid the original problem is to use a DEFAULT ( DATETIME('NOW') )
clause for the date_added column in the table definition, instead of using an INSERT trigger.
You have an UPDATE in your INSERT trigger. So the INSERT causes an UPDATE. Which you have hooked with a different trigger.

Resources