Is there any way to know when a record was added to the bank?

Asked

Viewed 1,419 times

4

I would like to know the time a record was added to the bank. I will use this information to show the time a contact was added to my schedule.

2 answers

4


Create a column of type TIMESTAMP.

In the Mysql, to save date and time automatically when adding some item just create the table as follows.

CREATE TABLE t1 (
  data_criado TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

SOURCE: Mysql documentation

  • Does this column automatically save the time it was added? Or do I have to implement this in my code and insert it into the database?

  • which database you are using?

  • Mysql, in the xampp.

  • 1

    Beware of the year 2038 bug. The TIMESTAMP data type is used for values that contain Both date and time Parts. TIMESTAMP has a range of ' 1970-01-01 00:00:01' UTC to ' 2038-01-19 03:14:07' UTC. CC @Brunobrito

  • Is there another way to do it? @Victorstafusa

4

In the skywalker’s response, indicated the path to place a column in the database with the type TIMESTAMP and with DEFAULT CURRENT_TIMESTAMP.

Meanwhile, the guy TIMESTAMP suffers from the bug of the year 2038. That way, his replacement would be the guy DATETIME. This works from version 5.6 of Mysql.

For previous versions, the DATETIME cannot be used with DEFAULT CURRENT_TIMESTAMP. In this case, there are two possible solutions:

  1. Drop out DEFAULT and explicitly inform you whenever a INSERT is realized.

  2. Use a TRIGGER, as suggested in this reply by Soen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.