Can you tell when a record has been added to the bank?

4

I would like to know the time a record was added to the bank. I'll use this information to show the time a contact was added to my calendar.

    
asked by anonymous 03.12.2015 / 04:36

2 answers

4

Create a column of type TIMESTAMP .

In 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

    
03.12.2015 / 04:39
4

In Skywalker's response , the way to place a column in the database with the type was indicated TIMESTAMP and DEFAULT CURRENT_TIMESTAMP .

However, type TIMESTAMP suffers with the year 2038 bug . In this way, the substitute for it would be DATETIME type. This works from version 5.6 of MySQL.

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

  • Abandon the DEFAULT value and explicitly inform it whenever a INSERT is performed.

  • Use TRIGGER , as suggested in this SOen response .

  • 03.12.2015 / 08:10