在 MySQL 中实现 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP
使用 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 时,某列将当前时间戳作为其缺省值,并将自动更新为当前时间戳。
让我们查看一个示例并创建一个表 -
mysql> create table DemoTable737 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAdmissiondate datetime ); Query OK, 0 rows affected (0.68 sec)
以下是在 MySQL 中用于 CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 的查询 -
mysql> alter table DemoTable737 modify column StudentAdmissiondate timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; Query OK, 0 rows affected (2.20 sec) Records: 0 Duplicates: 0 Warnings: 0
让我们再次检查该表的描述 -
mysql> desc DemoTable737;
这将产生以下输出 -
+----------------------+--------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------------+--------------+------+-----+-------------------+-----------------------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(100) | YES | | NULL | | | StudentAdmissiondate | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +----------------------+--------------+------+-----+-------------------+-----------------------------+ 3 rows in set (0.01 sec)
广告