• 1
  • 2
  • 3
  • 4
  • 5
mysql数据库问题 首 页  »  帮助中心  »  数据库  »  mysql数据库问题
关于MySQL中TIMESTAMP的初始化相关摘录
发布日期:2016-4-30 17:4:50

  本文摘自《Automatic Initialization and Updating for TIMESTAMP》

  (1)With both DEFAULT CURRENTTIMESTAMP and ON UPDATE CURRENTTIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.

  CREATE TABLE t1 (

  ts TIMESTAMP DEFAULT CURRENTTIMESTAMP ON UPDATE CURRENTTIMESTAMP

  );

  (2)With neither DEFAULT CURRENTTIMESTAMP nor ON UPDATE CURRENTTIMESTAMP, it is the same as specifying both DEFAULT CURRENTTIMESTAMP and ON UPDATE CURRENTTIMESTAMP.

  CREATE TABLE t1 (

  ts TIMESTAMP

  );

  (3)With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp.

  (4)The default depends on whether the DEFAULT clause specifies CURRENTTIMESTAMP or a constant value. With CURRENTTIMESTAMP, the default is the current timestamp.

  CREATE TABLE t1 (

  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

  );

  (5)With a constant, the default is the given value. In this case, the column has no automatic properties at all.

  CREATE TABLE t1 (

  ts TIMESTAMP DEFAULT 0

  );

  (6)With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the given constant default value.

  CREATE TABLE t1 (

  ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP

  );

  (7)With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, the column is automatically updated to the current timestamp. The default is 0 unless the column is defined with the NULL attribute, in which case the default is NULL.

  CREATE TABLE t1 (

  ts TIMESTAMP ON UPDATE CURRENTTIMESTAMP -- default 0

  );

  CREATE TABLE t2 (

  ts TIMESTAMP NULL ON UPDATE CURRENTTIMESTAMP -- default NULL

  );

  以后会更新更多mysql的相关文章,敬请期待。