首页 > 代码库 > there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

创建表的时候报错

[Err] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

创建语句如下:

CREATE TABLE company_news(
    id              VARCHAR(32)     NOT NULL,
    title           VARCHAR(50)     NOT NULL,
    summary         VARCHAR(300),
    content         TEXT,
    company_token   VARCHAR(30)     NOT NULL,
    modify_by       VARCHAR(128),
    published_time  TIMESTAMP,
    modify_time     TIMESTAMP       DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    primary key(id)
);

问题原因:

One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.  (From the mysql 5.5 documentation)


该问题在mysql 5.6中已经不会出现,

Previously, at most one TIMESTAMP column per table could be automatically initialized or updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column definition can have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.  (From the mysql  5.6.5 documentation)


可以考虑以下解决办法:

CREATE TABLE company_news(
    id              VARCHAR(32)     NOT NULL,
    title           VARCHAR(50)     NOT NULL,
    summary         VARCHAR(300),
    content         TEXT,
    company_token   VARCHAR(30)     NOT NULL,
    modify_by       VARCHAR(128),
    published_time  TIMESTAMP       DEFAULT ‘0000-00-00 00:00:00‘,
    modify_time     TIMESTAMP       DEFAULT now() ON UPDATE now(),
    primary key(id)
);



there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause