Mysql資料型別---TIMESTAMP
阿新 • • 發佈:2019-02-08
標籤(空格分隔): MYSQL
TIMESTAMP 型別使用 4 個位元組,範圍是 ‘1970-01-01 00:00:01.000000’ 到 ‘2038-01-19 03:14:07.999999’ 。
MySQL以’YYYY-MM-DD HH:MM:SS’格式顯示TIMESTAMP值,顯示寬度固定為19個字元。如果想要獲得數字值,應在TIMESTAMP列新增+0。
TIMESTAMP欄位用於 INSERT 或 UPDATE 操作時記錄日期和時間。如果你不分配一個值,表中的第一個TIMESTAMP欄位自動設定為最近操作的日期和時間。也可以通過分配一個NULL值,將 TIMESTAMP 列設定為當前的日期和時間。
TIMESTAMP 欄位定義
你可以使用當前的時間戳去初始化值和自動更新,或者是其中之一,也可以都不是。比如,你在定義的時候可以指定自動更新,但並不初始化。
1. 如果定義時 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 子句都有,列值為預設使用當前的時間戳,並且自動更新
CREATE TABLE tb1(
id INTEGER,
t1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
mysql> DESC tb1;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO tb1(id) values(1);
Query OK, 1 row affected (0.10 sec)
mysql> select * from tb1;
+------+---------------------+
| id | t1 |
+------+---------------------+
| 1 | 2017-08-30 12:07:00 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> UPDATE tb1 SET id=2 WHERE id=1;
Query OK, 1 row affected (0.20 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb1;
+------+---------------------+
| id | t1 |
+------+---------------------+
| 2 | 2017-08-30 12:07:55 |
+------+---------------------+
1 row in set (0.00 sec)
2. 如果不使用 DEFAULT 或 ON UPDATE 子句,那麼它等同於 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
CREATE TABLE tb2(
id INTEGER,
t1 TIMESTAMP
);
mysql> DESC tb2;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
3 如果只有 DEFAULT CURRENT_TIMESTAMP 子句,而沒有 ON UPDATE 子句,列值預設為當前時間戳但不自動更新
CREATE TABLE tb3(
id INTEGER,
t1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
mysql> DESC tb3;
+-------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | NO | | CURRENT_TIMESTAMP | |
+-------+-----------+------+-----+-------------------+-------+
2 rows in set (0.00 sec)
4. 如果沒有 DEFAULT 子句,但是有 ON UPDATE CURRENT_TIMESTAMP 子句, 欄位將自動更新,列預設為 NULL。
timestamp 欄位預設不能為 NULL,必須明確指定該欄位接受 NULL,否則會報錯
mysql> CREATE TABLE tb4(
-> id INTEGER,
-> t1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-> );
ERROR 1067 (42000): Invalid default value for 't1'
mysql> CREATE TABLE tb4(
-> id INTEGER,
-> t1 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.23 sec)
mysql> DESC tb4;
+-------+-----------+------+-----+---------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | YES | | NULL | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+---------+-----------------------------+
2 rows in set (0.00 sec)
mysql> INSERT tb4(id) values(1);
Query OK, 1 row affected (0.07 sec)
mysql> SELECT * FROM tb4;
+------+------+
| id | t1 |
+------+------+
| 1 | NULL |
+------+------+
1 row in set (0.00 sec)
mysql> UPDATE tb4 SET id=2 WHERE id=1;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM tb4;
+------+---------------------+
| id | t1 |
+------+---------------------+
| 2 | 2017-08-30 12:21:13 |
+------+---------------------+
1 row in set (0.00 sec)
5 設定一個常量為預設值
如果有一個常量 DEFAULT,該列會有一個預設值,而且不會自動初始化為當前時間戳。如果該列還有一個 ON UPDATE CURRENT_TIMESTAMP 子句,這個時間戳會自動更新,否則該列有一個預設的常量但不會自動更新。
常量可以為 null,或者 1970-01-01 00:00:00 到 2037-12-31 23:59:59 之間的一個值,其他值都會報錯。
設定預設值為 0,報錯
mysql> CREATE TABLE tb5(
-> id INTEGER,
-> t1 TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
-> );
ERROR 1067 (42000): Invalid default value for 't1'
設定預設值為 0000-00-00 00:00:00,報錯
mysql> CREATE TABLE tb5(
-> id INTEGER,
-> t1 TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP
-> );
ERROR 1067 (42000): Invalid default value for 't1'
設定預設值為 1970-01-02 00:00:00,正確
mysql> CREATE TABLE tb5(
-> id INTEGER,
-> t1 TIMESTAMP NULL DEFAULT '1970-01-02 00:00:00' ON UPDATE CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.34 sec)
mysql> DESC tb5;
+-------+-----------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------------------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | YES | | 1970-01-02 00:00:00 | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+---------------------+-----------------------------+
2 rows in set (0.00 sec)
設定預設值為 NULL,必須明確指定該欄位接受 NULL,否則報錯。
mysql> CREATE TABLE tb5_2(
-> id INTEGER,
-> t1 TIMESTAMP DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
-> );
ERROR 1067 (42000): Invalid default value for 't1'
mysql> CREATE TABLE tb5_2(
-> id INTEGER,
-> t1 TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
-> );
Query OK, 0 rows affected (0.36 sec)
mysql> DESC tb5_2
-> ;
+-------+-----------+------+-----+---------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | YES | | NULL | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+---------+-----------------------------+
2 rows in set (0.00 sec)
mysql> DESC tb5_2;
+-------+-----------+------+-----+---------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-----------------------------+
| id | int(11) | YES | | NULL | |
| t1 | timestamp | YES | | NULL | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+---------+-----------------------------+
2 rows in set (0.00 sec)
應用示例
mysql> ALTER TABLE news MODIFY `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Query OK, 0 rows affected (1.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc news;
+------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | NULL | |
| thumb | varchar(250) | YES | | NULL | |
| content | text | YES | | NULL | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| updated_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.00 sec)
mysql> ALTER TABLE `news` MODIFY `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc news;
+------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-----------------------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(50) | NO | | NULL | |
| thumb | varchar(250) | YES | | NULL | |
| content | text | YES | | NULL | |
| created_at | timestamp | NO | | CURRENT_TIMESTAMP | |
| updated_at | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.00 sec)