Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NOW() a stable function?

If I do

INSERT INTO table1 (datetime1, datetime2) VALUES (NOW(),NOW())

Will the two fields always be identical in both columns?

Ditto for

INSERT INTO table1 (datetime1, datetime2) VALUES (NOW(),NOW())
                                                ,(NOW(),NOW()) 

Will all four database entries have the same value, or is it possible that row1 <> row2?

Note this is a theoretical question rather than a work-around question.
I just really want to know the how and why.

like image 869
Johan Avatar asked Sep 01 '11 13:09

Johan


People also ask

What does NOW () do in SQL?

The NOW() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

What is the difference between now () and Current_timestamp?

CURRENT_TIMESTAMP and CURRENT_TIMESTAMP() are synonyms for NOW() . Returns the current time as a value in 'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context. The value is expressed in the session time zone.

How do I get the current timestamp in SQL query?

The CURRENT_TIMESTAMP function returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the GETDATE() function.


1 Answers

With Postgres now() always returns the timestamp which denotes the beginning of the transaction.

So for your second example all four rows will have the same timestamp value.

If you want to have the "real" timestamp you have to use clock_timestamp().

More details are in the manual:

http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

like image 149
a_horse_with_no_name Avatar answered Nov 22 '22 18:11

a_horse_with_no_name