如何在 MySQL 的其他列中插入值时自动插入当前日期和时间?

如何在 MySQL 的其他列中插入值时自动插入当前日期和时间?

在 MySQL 中,通过将该列声明为 DEFAULT CURRENT_TIMESTAMP,我们可以在将值插入另一列时自动将当前日期和时间插入到该列中。

示例

mysql> Create table testing -> ( -> StudentName varchar(20) NOT NULL, -> RegDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP -> ); Query OK, 0 rows affected (0.49 sec)登录后复制

mysql> Insert into testing(StudentName) values ('Ram'); Query OK, 1 row affected (0.14 sec) mysql> Insert into testing(StudentName) values ('Shyam'); Query OK, 1 row affected (0.06 sec) mysql> Select * from testing; +-------------+---------------------+ | StudentName | RegDate | +-------------+---------------------+ | Ram | 2017-10-28 21:24:24 | | Shyam | 2017-10-28 21:24:30 | +-------------+---------------------+ 2 rows in set (0.02 sec) mysql> Insert into testing(StudentName) values ('Mohan'); Query OK, 1 row affected (0.06 sec) mysql> Select * from testing; +-------------+---------------------+ | StudentName | RegDate | +-------------+---------------------+ | Ram | 2017-10-28 21:24:24 | | Shyam | 2017-10-28 21:24:30 | | Mohan | 2017-10-28 21:24:47 | +-------------+---------------------+ 3 rows in set (0.00 sec)登录后复制

借助上述概念,我们还可以准确地知道其他列中的值插入的日期和时间。

以上就是如何在 MySQL 的其他列中插入值时自动插入当前日期和时间?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!