I use MySQL and I have people
table:
mysql> desc people;
+--------------+--------------+------+-----+-------------------+-----------------------------------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+-------------------+-----------------------------------------------+
| id | int | NO | PRI | NULL | |
| name | varchar(255) | YES | | NULL | |
| updated_time | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+--------------+--------------+------+-----+-------------------+-----------------------------------------------+
3 rows in set (0.00 sec)
I want to add new created_at column. So, that on insert
command, value of this column must be assigned to current timestamp. But, on update
command, value of this column should not be updated to current timestamp. Also, for all old raws of this table, value of this column must be equal to NULL. How I can do that?
I need in this feature, because my table should correctly react to command:
insert into people (id, name) values (7, 'alice');
I have seen some similar questions. For example, this: MySQL create time and update time timestamp
But, all of these answers do not match for my case. Because,
- If we do
add created_at timestamp default current_timestamp
. Then all old values that were existed before the addition of this column, will be assigned to current timestamp. - If we do
add created_at timestamp default null on update current_timestamp;
. Then onupdate
command, value of this column will be assigned to current timestamp, which is not correct.