字段: 表timestamp字段
timestamp字段属性
1、属性介绍
CURRENT_TIMESTAMP :当我更新这条记录的时候,这条记录的这个字段不会改变。
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP :当我更新这条记录的时候,这条记录的这个字段将会改变,即时间变为了更新时候的时间。
2、修改字段属性值
show create table tbl_ledgerrecord;
alter table tbl_ledgerrecord change intoStorageDate intoStorageDate timestamp DEFAULT CURRENT_TIMESTAMP;TIMESTAMP设置默认值的几个应用实例
1、创建表 dj1,b列有个属性ON UPDATE CURRENT_TIMESTAMP,导致更新数据时,即便未涉及到该列,该列数据也被自动更新。c列为零值,新插入数据时依然是零值不会改变。
CREATE TABLE `dj1` (
`a` char(1) COLLATE utf8_bin DEFAULT NULL,
`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
UNIQUE KEY `dj1_idx_u1` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
mysql> select * from dj1;
+------+---------------------+---------------------+
| a | b | c |
+------+---------------------+---------------------+
| 7 | 2019-05-06 10:42:05 | 0000-00-00 00:00:00 |
| 4 | 2019-05-06 10:42:30 | 0000-00-00 00:00:00 |
| 1 | 2019-05-06 11:02:33 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql> update dj1 set a=8 where a=7;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from dj1;
+------+---------------------+---------------------+
| a | b | c |
+------+---------------------+---------------------+
| 4 | 2019-05-06 10:42:30 | 0000-00-00 00:00:00 |
| 1 | 2019-05-06 11:02:33 | 0000-00-00 00:00:00 |
| 8 | 2019-05-06 11:07:42 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
3 rows in set (0.00 sec)2、创建表 dj2,b 列 c 列不带属性.不带属性默认创建就是这样。
3、创建表 dj3,b列默认值为CURRENT_TIMESTAMP不带自动更新属性,c列为零值,测试可用
4、创建表 dj4,b列默认值为CURRENT_TIMESTAMP,c列默认值为CURRENT_TIMESTAMP带自动更新属性,测试可用。
5、创建表dj5,b列默认值为CURRENT_TIMESTAMP,c列默认值为'0000-00-00 00:00:00'带自动更新属性,测试后可以使用。
6、创建表dj6,b列默认值为CURRENT_TIMESTAMP带自动更新属性,c列默认值为CURRENT_TIMESTAMP,测试可用。
Last updated
Was this helpful?