SQLite Upsert using trigger - sqlite

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.

Related

Centos 7 and MariaDB - Can't create triggers

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';

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.

pl sql: trigger for insert data from another table

There is the table OLD and a similar one, NEW. I want to insert in the existing process that fills the table OLD a trigger event that for each new inserted row, this event will insert the newly inserted row to table NEW, as well. Inside the body of trigger, i need to include the query BELOW which aggregates values of OLD before inserted in NEW:
insert into NEW
select (select a.id,a.name,a.address,b.jitter,a.packet,a.compo,b.rtd,a.dur from OLD a,
select address,packet,compo, avg(jitter) as jitter, avg(rtd) as rtd from OLD
group by address,packet,compo ) b
where a.address=b.address and a.packet=b.packet and a.compo=b.compo;
can you correct any possible mistakes or suggest other trigger syntax on the statement below?
CREATE OR REPLACE TRIGGER insertion
after update on OLD
for each row
begin
MY select query above
end;
In a for each row trigger you cannot query the table itself. You will get a mutating table error message if you do.
I recommend only to use triggers for the most basic functionality such as handing out ID numbers and very basic checks.
If you do use triggers for more complex tasks you may very easily end up with a system that's very hard to debug and maintain because of all kinds of actions that appear out of knowhere.
Look at this question for another approach: getting rid of Insert trigger
Oracle Streams might also be a good solution. In the apply handler, you can include your own custom PL/SQL code. This procedure will be called after the COMMIT, so you can avoid mutating table errors.
However, Streams requires a large amount of setup to make it work. It might be overkill for what you are doing.

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