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)
);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章