سؤال

I'd like to load some file into MySQL, and I want to assign a single timestamp to all rows that were loaded within the same load operation. To do this, can I simply assign a default value like this?

CREATE TABLE test (
    test TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    something VARCHAR(255) NOT NULL
);

When I tried the timestamps did seem to be identical, but of course I can't be sure if this is guaranteed. I went through the documentation but couldn't find an answer. Also is there a better way to do this?

هل كانت مفيدة؟

المحلول

CURRENT_TIMESTAMPE is a synonym for NOW()

NOW() returns a constant time that indicates the time at which the statement began to execute

Since it's based on statement execution time, you are good to go.

نصائح أخرى

The above SQL statement guarantee that whenever you insert a row in the table you the the instantaneous time, thus can be shown like:

SELECT CURRENT_TIMESTAMP;

If you run this command each second you will notice the you get the most updated time on each instance.

PS: you can also use constant UTC_TIMESTAMP to get current time in UTC timezone.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top