MySQL版本区别5.5与5.7之DEFAULT CURRENT_TIMESTAMP

时间:2022-08-13 10:00:02

在5.5到5.6.4版本里,对于DEFAULT CURRENT_TIMESTAMP子句,只能TIMESTAMP类型列上指定。

而从5.6.5开始(也包括5.7),DEFAULT CURRENT_TIMESTAMP子句可以指定到TIMESTAMP或者DATETIME类型列上。

比如在5.5中:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.5.48-log |
+------------+
1 row in set (0.00 sec)


mysql> use test;
Database changed
mysql> create table t1(id int, dt timestamp not null DEFAULT CURRENT_TIMESTAMP, dt1 datetime not null DEFAULT CURRENT_TIMESTAMP);
ERROR 1067 (42000): Invalid default value for 'dt1'
mysql>


而在5.7中:

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.11-log |
+------------+
1 row in set (0.00 sec)


mysql> use test;
Database changed
mysql> create table t1(id int, dt timestamp not null DEFAULT CURRENT_TIMESTAMP, dt1 datetime not null DEFAULT CURRENT_TIMESTAMP);
ERROR 1050 (42S01): Table 't1' already exists
mysql> drop table t1;
Query OK, 0 rows affected (0.38 sec)


mysql> create table t1(id int, dt timestamp not null DEFAULT CURRENT_TIMESTAMP, dt1 datetime not null DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 rows affected (0.32 sec)


mysql> desc t1;
+-------+-----------+------+-----+-------------------+-------+
| Field | Type      | Null | Key | Default           | Extra |
+-------+-----------+------+-----+-------------------+-------+
| id    | int(11)   | YES  |     | NULL              |       |
| dt    | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
| dt1   | datetime  | NO   |     | CURRENT_TIMESTAMP |       |

+-------+-----------+------+-----+-------------------+-------+
3 rows in set (0.10 sec)


mysql>