Oracle, trigger update backup table in another schema - oracle11g

I must trigger an update in another schema table, it could be a simple question, but I stuck on this.
The 2 tables are identical, they have the same data and structure,
The trigger should update: ADMSCPM98.OPERADOR.NOMBRE_TRABAJADOR
just after the the
ADMSCPM.OPERADOR.NOMBRE_TRABAJADOR update
That's the trigger:
CREATE OR REPLACE TRIGGER UP_OPERADOR
AFTER UPDATE ON OPERADOR
FOR EACH ROW
BEGIN
UPDATE "ADMSCPM98"."OPERADOR"
SET NOMBRE_TRABAJADOR = :new.NOMBRE_TRABAJADOR
WHERE NRO_TENIENTE = :old.NRO_TENIENTE;
END;
/

Related

How to Update Fields On Insert using Trigger

I've created and worked with Triggers in Oracle for years however I'm unable to wrap my head around how to update a field when inserting data into a sqlite database.
All I want to do is create a trigger that automatically inserts the current DateTime into a column in the sqlite database named 'createdDate' for ANY record that is inserted.
What is the best approach to accomplish this?
Below is what I've attempted without success.
CREATE TRIGGER outputLogDB_Created_Trig
BEFORE INSERT
ON outputLog
WHEN NEW.createdDate IS NULL
BEGIN
SELECT CASE WHEN NEW.createdDate IS NULL THEN NEW.createdDate = datetime('now', 'localtime') END;
END;
The above is almost a replica of how I would implement my triggers in Oracle with some modifications of course for sqlite. The logic is basically identical.
What am I missing?
Later Edit - I can get it to work if I instead use AFTER INSERT and not using FOR EACH ROW
CREATE TRIGGER outputLog_Created_Trig
AFTER INSERT
ON outputLog
WHEN New.createdDate IS NULL
BEGIN
UPDATE outputLog
SET createdDate = datetime('now', 'localtime')
WHERE outputLog_ID = New.rowid;
END;
But why can't I just insert the record using the new value while I'm inserting it? Am I ONLY able to get this in there using an Update AFTER I've already inserted the record?
The issue I have with this is the fact that I'd like to have a NOT NULL constraint on the createdDate column. Perhaps I'm simply used to how I've done it for years in Oracle? I realize the Trigger 'should' take care of any record and force this field to NEVER be NULL. It's just that NOT being able to add the constraint for some reason makes me uneasy. Do I need to let go of this worry?
Thanks to Shawn pointing me toward an easy simple solution to my problem. All that is needed in a SQLite database to insert the current Date/Time for each record being inserted is to set the DEFAULT value on that column to CURRENT_TIMESTAMP.
Since I wanted the timestamp in my own local time see below my create table script that is the solution to my problem.
CREATE TABLE outputLog (
outputLog_ID INTEGER PRIMARY KEY ASC ON CONFLICT ROLLBACK AUTOINCREMENT
NOT NULL ON CONFLICT ROLLBACK,
outputLog TEXT,
created DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP, 'localtime') )
NOT NULL )
;

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

Sqlite: Are updates to two tables within an insert trigger atomic?

I refactored a table that stored both metadata and data into two tables, one for metadata and one for data. This allows metadata to be queried efficiently.
I also created an updatable view with the original table's columns, using sqlite's insert, update and delete triggers. This allows calling code that needs both data and metadata to remain unchanged.
The insert and update triggers write each incoming row as two rows - one in the metadata table and one in the data table, like this:
// View
CREATE VIEW IF NOT EXISTS Item as select n.Id, n.Title, n.Author, c.Content
FROM ItemMetadata n, ItemData c where n.id = c.Id
// Trigger
CREATE TRIGGER IF NOT EXISTS item_update
INSTEAD OF UPDATE OF id, Title, Author, Content ON Item
BEGIN
UPDATE ItemMetadata
SET Title=NEW.Title, Author=NEW.Author
WHERE Id=old.Id;
UPDATE ItemData SET Content=NEW.Content
WHERE Id=old.Id;
END;
Questions:
Are the updates to the ItemMetadata and ItemData tables atomic? Is there a chance that a reader can see the result of the first update before the second update has completed?
Originally I had the WHERE clauses be WHERE rowid=old.rowid but that seemed to cause random problems so I changed them to WHERE Id=old.Id. The original version was based on tutorial code I found. But after thinking about it I wonder how sqlite even comes up with an old rowid - after all, this is a view across multiple tables. What rowid does sqlite pass to an update trigger, and is the WHERE clause the way I first coded it problematic?
The documentation says:
No changes can be made to the database except within a transaction. Any command that changes the database (basically, any SQL command other than SELECT) will automatically start a transaction if one is not already in effect.
Commands in a trigger are considered part of the command that triggered the trigger.
So all commands in a trigger are part of a transaction, and atomic.
Views do not have a (usable) rowid.

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