Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 'created_at' and 'updated_at' columns? [closed]

I need to add 'updated_at' and 'created_at' columns to some already existing table in MySQL database. I've added those colums using MySQL Workbench, but what query should I use to make them work properly? Thanks in advance ;)

like image 487
z0idb3rg Avatar asked May 30 '18 11:05

z0idb3rg


People also ask

How to add created_at column in typeorm?

In TypeORM, you can add a created_at column and a updated_at column by making use of the CreateDateColumn and UpdateDateColumn decorators, respectively. These columns will be automatically initialized and updated to the current date and time (that is, the current timestamp).

How do I add a column to a Hasura table?

On the Hasura console, click on the Modify tab of a table. When clicking on the +Frequently used columns button, choose created_at: Click the Add column button. On the Hasura console, click on the Modify tab of a table. When clicking on the +Frequently used columns button, choose updated_at: Click the Add column button.

How do I create and update a datetime entry?

You will need two fields "Created" and "Updated" with type datetime. When a new entry is inserted then insert "Created" with current time stamp. When a update is happening insert "Updated" with the current time stamp, and let the "Created" field remain as it is.

Why is my MySQL column not automatically updating to the current timestamp?

With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp. Note: Link provided refers to MySQL 8.0. The syntax is the same for previous versions as well.


Video Answer


2 Answers

According to the reference manual* you can use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions:

With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.

whereas:

With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp.

So, for example, you could use:

CREATE TABLE t1 (
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

To add the columns to an already existing table you can use:

ALTER TABLE t1
ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Note: Link provided refers to MySQL 8.0. The syntax is the same for previous versions as well. There is some difference though for versions prior to 5.6.5. Just quoting from the manual again:

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table.

like image 86
Giorgos Betsos Avatar answered Sep 19 '22 11:09

Giorgos Betsos


Try to use a Trigger. As example, when creating a new entry, the trigger will be activated and will execute, like an event in c# or an Action listener in Java!

With them, you can update that new entry with a creation date, or an edited date when you manipulated an entry. See this Documentation on how to use MySql Triggers on w3resource.

like image 25
Cataklysim Avatar answered Sep 19 '22 11:09

Cataklysim