您好,欢迎访问一九零五行业门户网

在MySQL中,如何在向其他列插入NULL值的同时自动插入日期和时间?

在 mysql 中,我们可以通过将该列声明为 default current_timestamp,在其他列中插入 null 值时自动将当前日期和时间插入到该列中。在这种情况下,我们不能声明要在其中插入 null 值的列 not null。
mysql> create table testing1(name varchar(20),regstudent timestamp default current_timestamp);query ok, 0 rows affected (0.15 sec)
以上查询将创建一个表“testing1”,其中有一列名为“name”(未声明为“not null”),其他列名为“regdate”,声明为 default current_timestamp。现在,在插入 null 值“名称”列时,当前日期和时间将自动插入到另一列中。
mysql> insert into testing1(name) values(null);query ok, 1 row affected (0.08 sec)mysql> insert into testing1(name) values(null);query ok, 1 row affected (0.04 sec)mysql> select * from testing1;+------+---------------------+| name | regstudent |+------+---------------------+| null | 2017-10-29 04:46:59 || null | 2017-10-29 04:47:02 |+------+---------------------+2 rows in set (0.05 sec)
从上面的查询中,我们可以看到,在“name”中插入 null 值时,日期和时间也会自动插入。
以上就是在mysql中,如何在向其他列插入null值的同时自动插入日期和时间?的详细内容。
其它类似信息

推荐信息