How to create an autoupdating postgres timestamp column without using a trigger - sql

On some of my tables I have some columns which contain a creation timestamp column that is supposed to be set on insert and a last update timestamp that is supposed to be updated on every update. I would like to do this without using a trigger is that possible?

For creation time column you can set default yo NOW, but for last update time I think you will have to use trigger.
Btw. why you don't want triggers?

Related

I need to modify default value of some columns

I have to create a schema for Oracle and another one for SQL Server. Is it better to use alter table to modify default value (date), or should I use a trigger, since Oracle uses SYSDATE and SQL Server uses GETDATE()
I think generally it would be better use a default value, without using trigger (my opinion is to use triggers only if strictly necessary). Moreover, trigger difference between MSSQL and ORACLE requests more attention than using DEFAULT SYSDATE rather than DEFAULT GETDATE().
On the other hand, default value will be used ONLY if you don't pass any value with insert command: with trigger you can change values as you want.
I'm facing with similar problem (we have two DB version, MSSSQL and ORACLE, for the same application) and in this case we use DEFAULT value for insert date (eg. column DATA_INS) and trigger for update date (column DATA_UPD) of a record.

Get a reference to insert rows with triggers

I'm trying to get a reference to a set of rows that I'm trying to insert into a table through a multiple insert. For example if I execute:
INSERT INTO T VALUES (0,'A'),(1,'B'),(2,'C')
I would like to get a reference in a before insert trigger to a "table" that contains these 3 rows. Is that possible?
And another question: what does a REFERENCING NEW_TABLE represents in a before trigger (maybe could this be the answer to the first question)?
Thanks
According to documentation:
REFERENCING NEW TABLE AS identifier
Specifies a temporary table name which identifies the affected rows as modified by the triggering SQL operation and by any SET
statement in a BEFORE trigger that has already executed.
Also take a look:
FOR EACH STATEMENT
Specifies that the triggered action is to be applied only once for the whole statement. This type of trigger granularity cannot be
specified for a BEFORE trigger or an INSTEAD OF trigger (SQLSTATE
42613). If specified, an UPDATE or DELETE trigger is activated, even
if no rows are affected by the triggering UPDATE or DELETE statement.
maybe it will suite better your needs (of course you need to go with AFTER trigger)

DBMS_JOB over triggers

I want to know if DBMS_JOB can be used over Trigger.
My requirement is that I want the job to run when Table1 is inserted or updated. I want to know that if I schedule the job every 1 minute to pick the updated/inserted row based on date whether it will put any load on my DB(though the update or insert will not happen frequently). I am using Oracle 11g.
Thanks
The solution is to put trigger that insert data in another table named EVT_Table1 (when you have update or insert) and than let your job execute action on the EVT_Table1.

How to find when a row is inserted or updated in SQL Server table

I have a table in SQL Server but there is no column to show when the value is inserted into the table. And also I don't want to create a one.
Is there a way that I can find the time and date when some one has inserted a row into a SQL Server 2008 table or when it is updated?
If you don't want to modify your existing tables, the only way to go is to create another table to log the activities to. Then you can use ON INSERT / ON UPDATE triggers on the source tables and log the time and date of inserts and updates to the log table.
You can use On Insert and On Update Triggers in sql server 2005/2008 and your required fields are available in inserted and updated tables, but if you need to use this information later need to add a new table and log your information on to that!
I myself for doing such stuff like this always add 2 column to my tables
LastActivityBy relation to user table
and
LastActivityOn datetime
so you don't need to use any other table.
If you dont want to alter your existing schema, create a audit table and create trigger on source table to update audit table.

ALTER SQL statement takes very much time

We are facing problem in the alter table SQL statement. Some time we update our database at client side and the alter table sql taking very much time. I like to know, how alter works? Does alter statement performance correlated to that table data? Means, if table have large data then alter will take much time.
There is also problem with the Oracle 11G R2. Is there any changes which need to incorporate to our code? Our code is very old and working fine till now?
There could be several reasons for this:
If the table is locked by another
query/resource. It would wait for the
lock to be released and then execute
the update...
If the table contains many rows and you have added a new column in the table with a default value, it would execute an update query for whole table after altering the table to update all the existing records with the default value...
If for example you add a new column with a default value in a large table, then it will take time depending the size of the table.