Timestamp update automatically with current date

Asked

Viewed 2,998 times

3

I wonder if it is possible, when one is made INSERT in a given table, automatically update the value of another column of the TIMESTAMP type called date with the current date and time, similar to the function NOW() mysql?

  • can normally use the function NOW() Mysql in your PHP statement. If you are using bind_param, just don’t pass the parameter $data at INSERT, leaving DBMS with this responsibility.

  • Enter the code that is having problems.

  • You don’t understand the OP problem...

  • Columns type TIMESTAMP (and not DATETIME) do not do this automatically?

  • 1

    @bfavaretto, depends on the Mysql version and how Mysql was installed. If it is a current version, an installation is used standard, you’re right.

1 answer

4


galera got with the simple sql command below. thanks to all

ALTER TABLE `grade_edt` CHANGE `data_ultima_mod` 
`data_ultima_mod` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
  • 1

    It’s an unusual alternative. The most common is to leave these settings in charge of the application, but, it works.

  • 2

    @Fellipesoares because unusual? It is so relevant to be in the domain of the application the last change of the record?

  • @gmsantos, if there are any changes to the database hardware, DBMS version change, or even the DBMS exchange, you may need to reshape the table structure, when if the responsibility was of the application, you would not have this concern. It is more a matter of thinking in the development cycle. Perhaps "usual" is not the right word to express.

  • 2

    Changing hardware or Mysql version will not influence the table’s Constraint. Only concern is if the system is going to suffer a change of Sgdb or need to run in several banks. I’m writing an answer to explain this further. But if sgbd never changes from mysql, I find the use of Constraint even better than managing it in the application

  • I get it. Since the question was not about a specific scenario, an answer taking into account a possible exchange of DBMS may be the most ideal, since this is usually a frequent doubt. I have also had problems with application server in UTC other than database server, for example.

Browser other questions tagged

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