Sam Sam - 4 months ago 22
MySQL Question

getting "only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" error while it's used only once

I am creating a database table with the following sql:

CREATE TABLE `cs3_ds1` (
`ID` INT NOT NULL ,
`TIME` TIMESTAMP NOT NULL ,
`USER` VARCHAR(45) NOT NULL ,
`TIME1` TIMESTAMP NOT NULL ,
`TIME2` TIMESTAMP NOT NULL ,
`INSERT_TIME` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB


Although I am using the current timestamp as default in one column only, But I am getting the following error:

ERROR 1293: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause


why I am getting this error?

Answer

MySQL auto initialises TIMESTAMP Columns with DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, so your first column TIME has the CURRENT_TIMESTAMP Added as default. Therefore by the time you explicitly add a DEFAULT to a column one already exists. You either need to change the order your columns are defined:

CREATE  TABLE `silas`.`cs3_ds1` (
`ID` INT NOT NULL ,
`INSERT_TIME` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
`TIME` TIMESTAMP NOT NULL ,
`USER` VARCHAR(45) NOT NULL ,
`TIME1` TIMESTAMP NOT NULL ,
`TIME2` TIMESTAMP NOT NULL ,
 PRIMARY KEY (`ID`) )
 ENGINE = InnoDB

Or add defaults to your other timestamp columns:

CREATE  TABLE `silas`.`cs3_ds1` (
`ID` INT NOT NULL ,
`TIME` TIMESTAMP NOT NULL DEFAULT 0,
`USER` VARCHAR(45) NOT NULL  DEFAULT 0,
`TIME1` TIMESTAMP NOT NULL DEFAULT 0 ,
`TIME2` TIMESTAMP NOT NULL DEFAULT 0 ,
`INSERT_TIME` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
 PRIMARY KEY (`ID`) )
 ENGINE = InnoDB

See the MySQL Docs for further information.