總結下mysql的datetime和timestamp兩種類型的相關知識
mysql 的datetime和timestamp的區別
create table test (id int(32) ,time1 timestamp, time2 timestamp);
mysql 建表時,如果不對timestamp做任何限制,則默認就是自動初始化和自動更新的特性
- datetime的取值範圍是1000-01-01 00:00:00 到9999-12-31 23:59:59 而timestamp僅爲1970-01-01 00:00:00 到2037 23:59:59
- timestamp具有自動初始化和自動更新的特性:自動初始化爲當前日期和時間,當發生變化時自動更新爲當前時間
- timestamp可以自動轉換爲客戶端時區
- timestamp 只需要4個字節而datetime需要8個字節
對於timestamp還有以下特性:
- 一張表中可以同時有多個TIMESTAMP類型列,但是只能有一列具有上述特性,其他列都會被初始化爲0測試驗證如下:
create table test (id int(32) ,time1 timestamp, time2 timestamp);
insert test values(1);
insert test values(2);
select * from test;
+------+---------------------+---------------------+
| id | time1 | time2 |
+------+---------------------+---------------------+
| 1 | 2014-03-04 11:51:56 | 0000-00-00 00:00:00 |
| 2 | 2014-03-04 11:51:58 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
+------+---------------------+---------------------+
| id | time1 | time2 |
+------+---------------------+---------------------+
| 1 | 2014-03-04 11:51:56 | 0000-00-00 00:00:00 |
| 2 | 2014-03-04 11:51:58 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
可以看出只有time1被初始化了當前時間
而如果執行
alter table test modify column `time2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
即看出不允許一張表同時有兩個都是自動更新的。
而可以通過設置爲NULL將其值更新爲當前時間
insert test(id,time2) values(3, NULL);
+------+---------------------+---------------------+
| id | time1 | time2 |
+------+---------------------+---------------------+
| 1 | 2014-03-04 11:51:56 | 0000-00-00 00:00:00 |
| 2 | 2014-03-04 11:51:58 | 0000-00-00 00:00:00 |
| 3 | 2014-03-04 11:56:07 | 2014-03-04 11:56:07 |
+------+---------------------+---------------------+
| id | time1 | time2 |
+------+---------------------+---------------------+
| 1 | 2014-03-04 11:51:56 | 0000-00-00 00:00:00 |
| 2 | 2014-03-04 11:51:58 | 0000-00-00 00:00:00 |
| 3 | 2014-03-04 11:56:07 | 2014-03-04 11:56:07 |
+------+---------------------+---------------------+
mysql 建表時,如果不對timestamp做任何限制,則默認就是自動初始化和自動更新的特性
show create table test;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(32) DEFAULT NULL,
`time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(32) DEFAULT NULL,
`time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
同時對於timestamp 的not null,說明其不能保存NULL值,但是仍然可以給該列賦NULL,數據庫會自動將其保存爲當前日期和時間
如果希望timestamp自動初始化但是不自動更新,可以只使用DEFAULT CURRENT_TIMESTAMP